2011-04-15 42 views
-1

我只需要得到selectedvalues以及ID的下拉列表面板中的如何在ASP.NET的面板中循環dropdownlist?

如何循環下拉列表在ASP.NET面板?

For i = 0 To pnl.Controls.Count - 1 
       Dim ddl As DropDownList = CType(pnl.Controls(i)., DropDownList) 
       test = test & "[" & ddl.SelectedValue & "]" 
Next 

感謝

回答

0

假設你的下拉菜單中有一個id,如果它被包含在另一個控制其實並不重要。 C#代碼會是這個樣子:

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()); 
} 
0

也許你可以試試這個:

string selectedText = string.Empty; 
int selectedId = -1; 

if (DropDownList.SelectedItem != null) 
{ 
    selectedText = DropDownList.SelectedItem.Text; 
    selectedId = Convert.ToInt32(DropDownList.SelectedItem.Value); 
} 

通常情況下,你要綁定的idvalueDropDownList