Find the Datalist control row width using javascript

peter liles 556 Reputation points
2022-02-14T23:36:31.603+00:00

Since i added labels inside a div container the javascript calculation divWidth is incorrect. Before Datalist only contained a Image which was same size for all items so the row size could easily be calculated using formula _Items.Count * (_ImageWidth + 1).
How do you change the javascript code to calculate the total row size of Datalist now that i have added a label and a div who's text value is of different length for every item in the row?

        Dim JS As StringBuilder = New StringBuilder()

    JS.Append("<script type=""text/javascript"">\n")          
JS.Append("var pix = 0;\n")

    **JS.AppendFormat("var divWidth = {0};\n", _Items.Count * (_ImageWidth + 1))

    JS.AppendFormat("var divVirtualWidth = {0};\n", _NoOfVisibleImages * (_ImageWidth + 1))**

JS.Append("function MoveLeft()\n")
    JS.Append("{\n")
Developer technologies ASP.NET Other
0 comments No comments
{count} votes

4 answers

Sort by: Most helpful
  1. Lan Huang-MSFT 30,186 Reputation points Microsoft External Staff
    2022-02-15T05:17:47.023+00:00

    Hi @peter liles ,
    I think you can get the length of the labels and add them up one by one.

    Dim _labelWidth = document.getElementById("Label1").innerHTML.length  
    

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  2. peter liles 556 Reputation points
    2022-02-15T17:43:09.597+00:00

    Why does the AppendFormat always return a unidentified response. It does not seem to return the variable value when i apply IT to subsequent code in the script ? Is it the correct syntax for javascript?


  3. peter liles 556 Reputation points
    2022-02-16T15:25:20.263+00:00

    The application works in C# language! I took it from the internet and converted to VB instead. But having difficulty getting it to work. If i remove the AppendFormat line it works. It just this code line that does not return a value from the variable and the rest of the script depends upon the return variable value.


  4. AgaveJoe 30,126 Reputation points
    2022-02-16T16:40:06.4+00:00

    I hope you do not mind but fixed a few bugs and cleaned up your code. I had to do a screenshot because the forum thinks the content is a script. But you should get the idea.

    175010-capture.png

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.