繁体   English   中英

从ASP.NET DropDownList绑定选定的值

[英]Bind selected value from ASP.NET DropDownList

当我在.aspx页中绑定TextBox的值时,我在ListView中执行以下操作:

<asp:TextBox ID="NameTextBox" runat="server" Text='<%# Bind("Name") %>' />

如何以相同方式绑定DropDownList中的值?

<asp:DropDownList ID="TestDropDownList" runat="server">
   <asp:ListItem Value="Test 1">Test 1</asp:ListItem>
   <asp:ListItem Value="Test 2">Test 2</asp:ListItem>
</asp:DropDownList>

要绑定到DropDownList,您需要将数据源绑定到DropDownList控件,并在该阶段指定Text和Value,例如:

cmd SqlCommand = new SqlCommand("Your SQL Command Here", conn);

TestDropDownList.DataSource = cmd.ExecuteReader();
TestDropDownList.DataTextField = "Name";
TestDropDownList.DataValueField = "Value";
TestDropDownList.DataBind();

这相当于您尝试执行以下操作:

<asp:DropDownList ID="TestDropDownList" runat="server">
   <asp:ListItem Value="<%# Bind("Value") %>"><%# Bind("Name") %></asp:ListItem>
</asp:DropDownList>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM