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

2 Answers 2

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 %>");
Sign up to request clarification or add additional context in comments.

2 Comments

the problem is method is not being called , it is not showing any alert , how to solve it
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);  
    }

2 Comments

The method hello() is not being executed , what is the problem here ?
is ListBox1_SelectedIndexChanged executed, are you sure?

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.