Running Javascript after control’s selected value has been set

Question :

Running Javascript after control’s selected value has been set,

Answer :

Simple ASP.NET application.

I have two drop-down controls. On the first-drop down I have a JavaScript onChange event. The JavaScript enables the second drop-down and removes a value from it (the value selected in the first drop-down). If they click the blank first value of the drop-down, then the second drop-down will be disabled (and the options reset).

I also have code in the OnPreRender method that will enable or disable the second drop-down based on the value of the first drop-down. This is so that the value of the first drop-down can be selected in code (loading user settings).

Read More  Filling a DataSet or a DataTable from a LINQ query result set

My problem is:

  1. The user selects something in the first drop-down. The second drop-down will become enabled through JavaScript.
  2. They then change a third drop-down that initiates a post back. After the post back the drop-downs are in the correct state (first value selected, second drop-down enabled).
  3. If they then click the back button, the second drop-down will no longer be enabled although it should be since there’s something selected in the first drop-down.

I’ve tried adding a startup script (that will set the correct state of the second-drop down) through ClientScript.RegisterStartupScript, however when this gets called the first drop-down has a selectedIndex of , not what it actually is. My guess is that the value of the selection gets set after my start script (but still doesn’t call the onChange script).

Read More  Running Javascript after control’s selected value has been set

Any ideas on what to try?


If the second dropdown is initially enabled through javascript (I’m assuming this is during a javascript onchange, since you didn’t specify), then clicking the back button to reload the previous postback will never enable it.

Mixing ASP.NET with classic javascript can be hairy. You might want to have a look at ASP.NET’s Ajax implementation (or the third-party AjaxPanel control if you’re forced to use an older ASP.NET version). Those will give you the behaviour that you want through pure C#, without forcing you to resort to javascript hackery-pokery.

Read More  I/O permission settings using .net installer

That’s the answer Running Javascript after control’s selected value has been set, Hope this helps those looking for an answer. Then we suggest to do a search for the next question and find the answer only on our site.

Disclaimer :

The answers provided above are only to be used to guide the learning process. The questions above are open-ended questions, meaning that many answers are not fixed as above. I hope this article can be useful, Thank you