Set Selecteditem
-
Hello... I want to set the SelectedItem property for a DropDownList in a Template column(ItemTemplate) of a DataGrid..but i dont know how? Is there any one who could help?
See if this helps! Assuming mydropdownlist is the name of your dropdownlist control and MyDatagrid is the name of your datagrid control,and assuming you want to set the dropdownlist to the 5th item (index=4):- dim dgItem as DatagridItem dim ddList as dropdownlist for each dgItem in MyDatagrid.items ddList = ctype(dgItem.findControl("MyDropDownList"),DropDownList) ddList.selectedIndex = 4 next
-
Hello... I want to set the SelectedItem property for a DropDownList in a Template column(ItemTemplate) of a DataGrid..but i dont know how? Is there any one who could help?
<asp:DropDownList Runat="server" DataSource='<%# GetAllOptions() %>'
DataTextField="DisplayName" DataValueField="ID" SelectedIndex='<%#
GetSelectedOption((int)Eval("ID")) %>' ID="ddl"></asp:DropDownList>Then in code behind...
public DataTable GetAllOptions() { DataTable tbl = HoweverYouGetYourOptions(); return tbl; } public int GetSelectedOption(int selectedID) { DataTable tbl = HoweverYouGetYourOptions(); for (int i = 0; i < tbl.Rows.Count; i++) { if ((int)tbl.Rows\[i\]\["ID"\] == selectedID) return i; } return -1; }
Edited: stupid <s
-
<asp:DropDownList Runat="server" DataSource='<%# GetAllOptions() %>'
DataTextField="DisplayName" DataValueField="ID" SelectedIndex='<%#
GetSelectedOption((int)Eval("ID")) %>' ID="ddl"></asp:DropDownList>Then in code behind...
public DataTable GetAllOptions() { DataTable tbl = HoweverYouGetYourOptions(); return tbl; } public int GetSelectedOption(int selectedID) { DataTable tbl = HoweverYouGetYourOptions(); for (int i = 0; i < tbl.Rows.Count; i++) { if ((int)tbl.Rows\[i\]\["ID"\] == selectedID) return i; } return -1; }
Edited: stupid <s
-
See if this helps! Assuming mydropdownlist is the name of your dropdownlist control and MyDatagrid is the name of your datagrid control,and assuming you want to set the dropdownlist to the 5th item (index=4):- dim dgItem as DatagridItem dim ddList as dropdownlist for each dgItem in MyDatagrid.items ddList = ctype(dgItem.findControl("MyDropDownList"),DropDownList) ddList.selectedIndex = 4 next
Thank you for your respond..I tried a code which is similar to your code but it didnt work.I expect that the reason is that the code depends on a copy instance from the original DropDownList instance.So this won't perform any effects.