Hello, I have successfully populated a list of items into an asp:DropDownList
using JavaScript. Here is the code snippet:
if (document.getElementById("<%=gdview.ClientID %>")!=null)
{
var rows = document.getElementById("<%=gdview.ClientID %>").getElementsByTagName('tr');
var cells = rows[1].getElementsByTagName('td');
//alert(cells)
i = 2;
while (i < cells.length)
{
document.getElementById("<%=ddl_noofCols.ClientID %>").options[i] = new Option(i + 1, i);
i++;
}
document.getElementById("<%=ddl_noofCols.ClientID %>").options[2].selected =true;
alert(document.getElementById("<%=ddl_noofCols.ClientID %>").options[2].text);
}
In this script, the gridview is represented by gdview, and its number of columns are added to the dropdownlist. By default, the third option (index 2) is selected. However, when trying to retrieve the selected item or value using ddl_noofCols.SelectedValue
, it returns null.
Could someone advise on how to properly obtain the selected value?
Thank you for your help!