|
今天打算學(xué)習(xí)下dropdownlist控件的取值,當(dāng)你通過(guò)數(shù)據(jù)庫(kù)控件或dataset綁定值后,但又希望顯示指定的值,這可不是簡(jiǎn)單的值綁定就OK,上網(wǎng)搜了一些資料,想徹底了解哈,后面發(fā)現(xiàn)其中有這么大的奧妙,可以通過(guò)很多種方法解決同樣的問(wèn)題,下面詳說(shuō): 一、dropdownlist控件的值綁定方法:
1、直接輸入item項(xiàng)
<asp:DropDownList ID="DropDownList1" runat="server" >
<asp:ListItem>張三</asp:ListItem>
<asp:ListItem>李四</asp:ListItem>
</asp:DropDownList>
這恐怕是最簡(jiǎn)單的,看下面這種
2、數(shù)據(jù)源控件綁定
<asp:DropDownList ID="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>
這種實(shí)用、方便寫(xiě),再看下面這種
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";
DropDownList1.DataBind();
這種高級(jí)一點(diǎn),或許還有一些方法,發(fā)現(xiàn)中;
二、而實(shí)際應(yīng)用中,很多時(shí)候不是簡(jiǎn)單的一個(gè)綁定值那么簡(jiǎn)單,例如:當(dāng)dropdownlist控件綁定值后,而你又希望指定初始值,就是顯示的值,例子很多就不舉了,下面是自己總結(jié)的幾種方法(只放前后臺(tái)主要代碼):
第一種:
前臺(tái)代碼:
<asp:DropDownList ID="DropDownList1" runat="server" >
<asp:ListItem>張三</asp:ListItem>
<asp:ListItem>李四</asp:ListItem>
</asp:DropDownList>
后臺(tái)代碼:
DropDownList1.Item.Inset(0,"李四");//這是插入第一個(gè)值為李四;
DropDownList.Items.FindByValue("李四").selected = true;//這是調(diào)用findbyvalue方法指定初始值;
第二種:
前臺(tái)代碼:
<asp:DropDownList ID="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>
后臺(tái)代碼:
DropDownList1.SelectedValue = "李四"; //使用item方法貌似不行,會(huì)提示沒(méi)有引入實(shí)例錯(cuò)誤;
第三種:
前臺(tái)代碼:前面2種都可以;
后臺(tái)代碼:
DropDownList1.SelectedIndex = 1;//通過(guò)控件索引來(lái)指定,1代表第二個(gè)值;
其實(shí)還有一種,比較經(jīng)常用到,實(shí)例說(shuō)明:(在此直觀(guān)的說(shuō)明)
實(shí)例問(wèn)題:綁定控件的值為id,但顯示為name,同樣首先指定默認(rèn)值,通過(guò)選項(xiàng),修改id;
區(qū)別:默認(rèn)值是通過(guò)數(shù)據(jù)庫(kù)數(shù)值或傳的數(shù)據(jù)來(lái)指定的,而不是指定一個(gè)默認(rèn)字符串;
解決方法:
1、前臺(tái)代碼:
<asp:DropDownList ID="DropDownList1" runat="server" >
<asp:ListItem>張三</asp:ListItem>
<asp:ListItem>李四</asp:ListItem>
</asp:DropDownList>
后臺(tái)代碼:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string yhid = Request.Params["userid"].ToString();
DropDownList1.Items.FindByValue(yhid).Selected= true;
}
}//這里只是簡(jiǎn)單闡述,如果是從dataset讀出來(lái)的值,是一樣的效果;
2、前臺(tái)代碼:
<asp:DropDownList ID="DropDownList1"runat="server"DataSourceID="SqlDataSource1"DataTextField="name"DataValueField="id"> </asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings: ConnectionString %>"
SelectCommand="SELECT [id] [name] FROM [yh]"></asp:SqlDataSource>
后臺(tái)代碼:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string yhid = Request.Params["userid"].ToString();
DropDownList1.SelectedValue = yhid; }
}
3、或者可以通過(guò)sql語(yǔ)句直接讀取id所對(duì)應(yīng)的name,就可以直接使用賦值了:
三、DropDownList數(shù)據(jù)綁定第一項(xiàng)為空的方法
DropDownList1.DataSource = ds.Tab
les[0]; DropDownList1.DataTextField="name";
DropDownList1.DataValueField = "id";
DropDownList1.Items.Insert(0,new ListItem());
下面為備注說(shuō)明:
selectedindex獲得的是選定項(xiàng)的索引,索引值是從0開(kāi)始.
selectedvalue是所有選擇的值. selecteditem.value是獲取索引值最小的選定項(xiàng).如果是多選的情況下,selectedvalue和selecteditem.value就有這么點(diǎn)差別. selecteditem代表選定項(xiàng),相當(dāng)于一個(gè)對(duì)象,這個(gè)對(duì)象仍然會(huì)有其他的屬性,比如checked,Attributes,value,而selectedvalue就是一個(gè)值,是一個(gè)字符串 |
|
|
來(lái)自: 醉人說(shuō)夢(mèng) > 《我的圖書(shū)館》