dropdownlist控制元件如何繫結動態資料
阿新 • • 發佈:2019-02-11
一、dropdownlist控制元件的值繫結方法:
1、直接輸入item項
<asp:DropDownList ID="DropDownList1" runat="server" >
<asp:ListItem>張三</asp:ListItem>
<asp:ListItem>李四</asp:ListItem>
</asp:DropDownList>
這恐怕是最簡單的,看下面這種
2、資料來源控制元件繫結
<asp:DropDownListID="DropDownList1"runat="server"DataSourceID="SqlDataSource1" DataTextField="name"DataValueField="name"> </asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$
ConnectionStrings: ConnectionString %>"
SelectCommand="SELECT [name] FROM [yh]"></asp:SqlDataSource>
這種實用、方便寫,再看下面這種
3、使用dataset或datareader繫結控制元件(以dataset為例)
SqlDataAdapter da = new SqlDataAdapter("select id,name from hy",conn);
DataSet ds = new DataSet();
da.Fill(ds);
conn.Close();
DropDownList1.DataSource = ds.Tables[0];
DropDownList1.DataTextField="name";
DropDownList1.DataValueField = "id";