簡體   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