0

I have a list of a class created in c# that I have to send its values to a javascript file. I have created a string in c# and put the values of the list in it:

count = 0;
JString = "[";
for(i=0; i<x; i++)
{
    JString += "{Source:" + A[i] + ", Number:" + 3 + ", Target:" + B[i] + "},";
    count++;
}
JString = JString.Remove(JString.Length - 1, 1); //to remove the last ,
JString += "]";
GraphData.Text = "" + "var JString =" + JString + " ;" + "var count =" + count + " ;";

GraphData is a label to save the string.

In the JavaScript file, I added:

 $("#GraphData").val(); //to get the string sent

But it's not working this way. Am I doing something wrong?

Thanks in advance:)

9
  • Is that label visible? Is the Visible property set to true? Commented Sep 7, 2012 at 11:41
  • 2
    What's the markup look like for GraphData when it's rendered on your browser? I think an asp:Label gets rendered as a <span>, in which case you'd want to call $('#GraphData').text() to get the inner text Commented Sep 7, 2012 at 11:43
  • Yes it is. First it wasn't but later I have set it to true but still not working Commented Sep 7, 2012 at 11:44
  • 1
    Is is infinite loop ? This may be source of your problem. Remove 'count++' and it should work for(i=0; i<count; i++) { JString += "{Source:" + A[i] + ", Number:" + 3 + ", Target:" + B[i] + "},"; count++; } Commented Sep 7, 2012 at 11:44
  • EOG's right, you have an infinite loop there. the count variable is being incremented inside the for loop. i won't be able to ever catch up. Commented Sep 7, 2012 at 11:48

3 Answers 3

1
for(i=0; i<count; i++)
{
    JString += "{Source:" + A[i] + ", Number:" + 3 + ", Target:" + B[i] + "},";
    count++;
}

This looks like an infinte loop. You're increasing both i and count by 1 in every cycle, so i will always be less than count

Sign up to request clarification or add additional context in comments.

Comments

0

After you fix the problem with the infite loop, you need to call $('#GraphData').text(); because Label in asp.net is rendered as a span element.

Comments

0

I think here GraphData is the server control. to get this element use its ClientID

try this

$("# <%= GraphData.ClientID%> ").val();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.