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

发布于 2024-11-01 10:23:43 字数 338 浏览 1 评论 0原文

我只需要获取面板中下拉列表的 selectedvalues 以及 ID

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

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

谢谢

I just need to get the selectedvalues as well as 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

☆獨立☆ 2024-11-08 10:23:44

也许你可以尝试这个:

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

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

通常,你将 id 绑定为 DropDownListvalue

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

变身佩奇 2024-11-08 10:23:44

不需要循环,因为您已经有了 DropDownList ddl 并且可以使用以下 DropDownList 属性:

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

请查看:http://msdn.microsoft.com/en-us/library/system.web.ui。 webcontrols.dropdownlist.aspx

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

咋地 2024-11-08 10:23:44

假设你的下拉菜单有一个 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());
}

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());
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文