I just need to get the selectedvalues as well as开发者_JAVA技巧 ID for dropdownlist in a panel
how to loop dropdownlist in a panel in ASP.NET?
For i = 0 To pnl.Controls.Count - 1
Dim ddl As DropDownList = CType(pnl.Controls(i)., DropDownList)
test = test & "[" & ddl.SelectedValue & "]"
Next
Thanks
Maybe you can try this:
string selectedText = string.Empty;
int selectedId = -1;
if (DropDownList.SelectedItem != null)
{
selectedText = DropDownList.SelectedItem.Text;
selectedId = Convert.ToInt32(DropDownList.SelectedItem.Value);
}
usually, you are binding the id
as value
of DropDownList
No need for a loop and since you already have your DropDownList ddl
and can use the following DropDownList
properties:
ddl.SelectedIndex //Index of selected item
ddl.SelectedValue //Value of selected item
ddl.ClientID //Markup control ID
//wasn't sure ID meant Index or markup control ID
Check this out: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.dropdownlist.aspx
Assuming your dropdown has an id it doesn't really matter if it is contained in another control. The C# code would look something like this:
for (int i = 0; i < DropDownList1.Items.Count; i++)
{
// Take whatever u want from the drop down, i.e:
string string_name = string.Format("text: {0}, value: {1}", DropDownList1.Items[i].Text, DropDownList1.Items[i].Value.ToString());
}
精彩评论