Question : Getting Datagrid's DropDownList value in JavaScript

I have a dropdownlist in a datagrid and I need to retrieve the value of the selected item in the dropdownlist using a JavaScript function.

Here is some of my code:














How can I correctly reference the dropdownlist inside the datagrid using the JavaScript function?


Answer : Getting Datagrid's DropDownList value in JavaScript

When a control is placed inside another control (in your example, the dropdown is placed inside the datagrid), the ID that is actually emitted  to the browser is "mangled" to ensure that it is unique by placing the IDs of its parents in front of it, separated by underscores.

For example, you could have this datagrid in a usercontrol, and place it 10 times on a page -- which dropdown does ddlDesc refer to then?

To actually get the new ID, in your server-side code you can use
"ddlDesc.ClientID" to get the mangled ID

So, you could use this in your page to emit the javascript you want: (this is c# for the Code-Behind)

protected override void OnPreRender(EventArgs e)
{
  base.OnPreRender (e);
  Page.RegisterClientScriptBlock("test", " ");
}

I've found that if you want to cheat, you can just look at the source and see the new client ID - if you don't alter the page, the mangled clientID will stay the same (unless you upgrade .net, patch it, or the phase of the moon changes -- use at your own risk)
Random Solutions  
 
programming4us programming4us