DropDownList 綁定(轉載)

一:DropDownList 
1.1 DropDownList綁定數據 
1.1.1 DropDownList 固定綁定 
這種方式適合那些已經固定的數據綁定到DropDownList上。 
例 javascript

複製代碼代碼以下:

<asp:DropDownList runat="server" ID="ddlArea" Width="120px" > 
<asp:Listitem value="0">選擇性別</asp:Listitem> 
<asp:Listitem value="1">男</asp:Listitem> 
<asp:Listitem value="2">女</asp:Listitem> 
</asp:DropDownList> 


1.1.2 DropDownList 動態綁定 
前臺: 
後臺:兩種方法:(注意,每次綁定都要清除一下原來的記錄,例:ddlArea.Items.Clear();) 
第一種: java

複製代碼代碼以下:

SqlConnection conn = new SqlConnection("server=.;uid=sa;database=pubs"); 
SqlDataAdapter dap = new SqlDataAdapter("select * from jobs", conn); 
DataTable dt = new DataTable(); 
dap.Fill(dt); 
DropDownList1.Items.Clear(); 
DropDownList1.DataSource = dt; 
DropDownList1.DataTextField = "job_desc"; 
DropDownList1.DataValueField = "job_id"; 
DropDownList1.DataBind(); 
DropDownList1.Items.Insert(0, new ListItem("選擇數據", "隨機綁定"));//插入默認項,此舉必須放到數據綁定以後效果: 


第二種: 數據庫

複製代碼代碼以下:

SqlConnection conn = new SqlConnection("server=.;uid=sa;database=pubs"); 
SqlDataAdapter dap = new SqlDataAdapter("select * from jobs", conn); 
DataTable dt = new DataTable(); 
dap.Fill(dt); 
if (dt.Rows.Count != 0) 

DropDownList1.Items.Clear(); 
for (int i = 0; i < dt.Rows.Count; i++) 

DropDownList1.Items.Add(new ListItem(dt.Rows[i]["顯示值"].ToString(), dt.Rows[i]["usbkey"].ToString())); 

DropDownList1.Items.Insert(0, "選擇網吧"); 
DropDownList1.Items[0].Value = "0"; 或 
// DropDownList1.Items.Insert(0, new ListItem("選擇數據", "隨機綁定"));//插入默認項,此舉必須放到數據綁定之 

else 

DropDownList1.Items.Insert(0, "無網吧記錄"); 
DropDownList1.Items[0].Value = "0"; 


二:DropDownList1的取值問題: 
2.1 取DropDownList1的索引值,也就是選擇 value 值<asp:Listitem value="1">男</asp:Listitem> 取1 
.net中 DropDownList1.SelectedValue.ToString() 
javascirpt var ddl1=document.getElementByIdx_x("DropDownList1").selectedIndex; 
2.2 取DropDownList1的選項,也就是選擇item值<asp:Listitem value="1">男</asp:Listitem> 取 男 
.net 中DropDownList1.SelectedItem.ToString(); 
javascript document.getElementByIdx_x("DropDownList1").options[document.getElement("selectID").selectedIndex].value 
三:DropDownList1事件問題: 
重點:使用OnTextChanged,OnSelectedIndexChanged事件時,必須設置 測試

複製代碼代碼以下:

<asp:DropDownList runat="server" OnTextChanged="DropDownList1_TextChanged"OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged1"> 


OnTextChanged,OnSelectedIndexChanged這兩個事件具體有什麼區別,我也沒測試出來,只知道OnSelectedIndexChanged這個事件要比OnTextChanged執行的早,也就是若是這兩個事件都存在,會首先執行OnSelectedIndexChanged這個事件,而後才執行OnTextChanged. 
四:如何避免DropDownList下拉框中的值重複添加? 
AppendDataBoundItems是否填加劇復值。真爲添加,假爲不填加 
緣由:DropDownList控件AppendDataBoundItems屬性設置爲"True"了的,改成False便可。 
例如:若是專業後的DropDownList控件AppendDataBoundItems屬性設置爲"True",那麼選擇院系後專業裏的值會不斷添加。 
五:區別 ui

複製代碼代碼以下:

depart_ddl.Items.Insert(0,new ListItem("不選該項","0")); 這是在首項添加數據。 
Items.Add是在最後添加 
DropDownList1.Items.Add(new ListItem("Text","value")); 是在最後添加 
DropDownList1.Items.Insert(Index,new ListItem("Text","value"));這是在首項添加數據。 


六:從數據庫中讀取數據,並綁定到DropDownList中 spa

複製代碼代碼以下:
if (ds.Tables[0].Rows[0]["State"].ToString ()=="True")  {  DropDownListState.Items.FindByValue("1").Selected =true;  }  else  {  DropDownListState.Items.FindByValue("0").Selected =true;  } 
相關文章
相關標籤/搜索