0

I am loading the value of string myvalue (global string) in array arr in javascript by

function hello()
{
   alert("hi");
   var arr=[<% myvalue %>];
   alert(arr);
}

protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
   myvalue="1234";
   Page.ClientScript.RegisterStartupScript(GetType(), "whatiskey", "hello();", true);  
}

and updating myvalue on listbox1.item select and calling method which updates value of arr, but javascript arr does not load the new value

Niki van Stein
  • 10,564
  • 3
  • 29
  • 62
phpnet
  • 903
  • 1
  • 8
  • 23

2 Answers2

1

you have to put double quotes and write it like:

var arr=["<%=myvalue %>"];

or more better way :

var arr= new Array();
arr.push("<%=myvalue %>");
Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
0

Register ListBox1 as partial post back element could also be one of the reason.

are you able debug, that ListBox1_SelectedIndexChanged is being called.

You need to something like below to stop this from multiple time registered, this can one of the reason not to call hello(). Use F12 to investigate the rendered HTML.

// Check to see if the client script is already registered.
    if (!Page.ClientScript.IsClientScriptBlockRegistered(cstype, csname2))
    {
           Page.ClientScript.RegisterStartupScript(GetType(), "aNewKey",  "hello();", true);  
    }
Anil
  • 3,722
  • 2
  • 24
  • 49