2014-02-06 35 views
1

对于我们一个标签绑定使用数据绑定到asp.net中的dropdownlist?

<asp:Label ID="Label2" runat="server" Text='<%#Eval("address") %>'></asp:Label> 

如何将数据绑定到下拉列表这样的数据?

asp:DropDownList ID="droplist" runat="server" > 
    <asp:ListItem Text="admin"></asp:ListItem> 
    <asp:ListItem Text="manager"></asp:ListItem> 
</asp:DropDownList> 

回答

5

像这样....

<asp:DropDownList ID="droplist" runat="server" SelectedValue='<%#Eval("fieldname")%>'> 
    <asp:ListItem Text="admin"></asp:ListItem> 
    <asp:ListItem Text="manager"></asp:ListItem> 
</asp:DropDownList> 

注意,智能感知不会接的SelectedValue出来。当然,你需要填充数据的下拉......可以使用适合

+0

This works,t.y.注意到IntelliSense没有选择这个。这是窃听我:( – Willmore

0

要么把你的数据源在DropDownList宣言喜欢这里的任何方法:populate dropdownlist

或使用代码隐藏这样的:

例如:在Page_Load()内:

List<string> ItemsToGoInDropDown = new List<string>{"manager", "admin", "etc"}; 
droplist.DataSource = ItemsToGoInDropDown; 
droplist.DataBind(); 
0

将数据放在隐藏字段中。然后Asigen在Gridview Rowdatabound Event下拉下来。像这样。

 if (e.Row.RowType == DataControlRowType.DataRow) 
     { 
      HiddenField hf = (HiddenField)e.Row.FindControl("hf"); 
      DropDownList ddl = (DropDownList)e.Row.FindControl("ddl"); 
      ddl.SelectedValue = hf.Value; 
     } 
相关问题