Wednesday, 28 August 2013

Store values in C# and populate in Javascript

Store values in C# and populate in Javascript

I am working in asp.net 1.1 site.
My requirement is : On post back I have some data, which I want to store
in some variable. And in javascript i want to add those info to a drop
down list.
Ex :
The data coming from post back call is :
Text Value
One 1
two 2
three 3
I want to store them in some variable and assign them to a drop down list
in JAVASCRIPT based on a condidtion.
The code I am using to keep the value in C# is:
StringBuilder sb = new StringBuilder();
sb.Append("<script>");
sb.Append("var test = [];");
for(int i=0; i < Engine.Length; i++)
{
sb.Append("var obj = {text:" + Engine[i][0] + "," + "value:" +
Engine[i][1] +"};");
sb.Append("test.push(obj);");
}
sb.Append("</" + "script>");
this.RegisterStartupScript("ownHtml", sb.ToString());
And adding the value to drop down list in JAVASCRIPT as:
for (var count = 0; count < test.length; count++)
{
dlEngine.options[count] = new Option(test[count].text,
test[count].value);
}
But it is not working.

No comments:

Post a Comment