DropDownList 詳解
DropDownList 控件用于創(chuàng)建下拉列表。DropDownList 控件中的每個可選項都是由 ListItem元素定義的!
提示:該控件支持數據綁定!
DropDownList 控件是一個下拉式的選單,功能和 RadioButtonListWeb控件很類似,提供用戶在一群選項中選擇一個;不過RadioButtonListWeb控件適合使用在較少量的選項群組項目,而DropDownList Web 控件則適合用來管理大量的選項群組項目。
一、DropDownList 控件屬性
1、AutoPostBack屬性:用于設置當改變選項內容時,,是否自動回送到服務器。True表示回送;False(默認)表示不回送。
2、DataSource屬性:用于指定填充列表控件的數據源。
3、DataTextField屬性:用于指定DataSource中的一個字段,該字段的值對應于列表項的Text屬性。
4、 DataValueField屬性:用于指定DataSource中的一個字段,該字段的值對應于列表項的Value屬性。
5、Items屬性:表示列表中各個選項的集合,如DropDownList.Items(i)
表示第i個選項,i從0開始。每個選項都有以下3個基本屬性:
Text 屬性:表示每個選項的文本。
Value屬性:表示每個選項的選項值。
Selected屬性:表示該選項是否被選中。
Count屬性:通過Items.Count屬性可獲得DropDownList控件的選項數;
Add方法:通過items.Add方法可以向 DropDownList控件添加選項;
Remove方法:通過items.Remove方法,可從DropDownList控件中刪除指定的選項;
Insert方法:通過items.insert方法,可將一個新的選項插入到DropDownList控件中;
Clear方法:通過items.clear方法可以清空DropDownList控件中的選項。
6、SelectedIndex屬性:用于獲取下拉列表中選項的索引值。如果未選定任何項,則返回值-1(負1)。
7、SelectedItem屬性:用于獲取列表中的選定項。通過該屬性可獲得選定項的Text 和Value屬性值。
8、SelectedValue屬性:用于獲取下拉列表中選定項的值。
9、SelectedIndexchanged事件:當用戶選擇了下拉列表中的任意選項時,都將引發(fā)SelectedIndexChanged事件。
二、使用語法
代碼<ASP:DropDownList
Id="控件名稱"
Runat="Server"
False"
DataSource="<%數據源%>"
DataTextField="數據源的字段"
DataValueField="數據源的字段"
OnSelectedIndexChanged="事件程序名稱"
>
<ASP:ListItem/>
</ASP:DropDownList>
一次性在同一個table綁定多個DropDownlist,并且去掉重復項(即代替distinct),從而提高性能。收藏代碼private voidBindDropDownList(DropDownList ddl2, DropDownList ddl3, DropDownListddl4, DropDownList ddl5)
{
stringconn = cs.GetDsn();
SqlConnectioncn = new SqlConnection(conn);
stringstrSqlDDL = "select Country,CustName,Substring(CONVERT(varchar(100), ActInstDate, 101),1,2)ActInstDate,ResellerNamefromtbcalicoinfo wheresurveystatus='Completed'";
DataSetds = new DataSet();
SqlDataAdapterda = new SqlDataAdapter(strSqlDDL, cn);
cn.Open();
da.Fill(ds,"tbcalicoinfo");
DataTabledt = newDataTable();
ds.Tables["tbcalicoinfo"].DefaultView.RowFilter= "country is not null";//RowFilter 過濾
ds.Tables["tbcalicoinfo"].DefaultView.Sort= "country asc";//Sort 排序
ddl2.DataSource= ds.Tables["tbcalicoinfo"].DefaultView.ToTable(true, "country");//ToTable去重復
ddl2.DataTextField= "country";
ddl2.DataBind();
ddl2.Items.Insert(0,new ListItem("--All--", "All"));
ds.Tables["tbcalicoinfo"].DefaultView.RowFilter= "ActInstDate is not null";
ds.Tables["tbcalicoinfo"].DefaultView.Sort= "ActInstDate asc";
ddl3.DataSource= ds.Tables["tbcalicoinfo"].DefaultView.ToTable(true,"ActInstDate");;
ddl3.DataTextField= "ActInstDate";
ddl3.DataBind();
ddl3.Items.Insert(0,new ListItem("--All--", "All"));
//DataRow[]dr2 = dt.Select("ResellerName is not null","ResellerNameasc");
ds.Tables["tbcalicoinfo"].DefaultView.RowFilter= "ResellerName is not null";
ds.Tables["tbcalicoinfo"].DefaultView.Sort= "ResellerName asc";
ddl4.DataSource= ds.Tables["tbcalicoinfo"].DefaultView.ToTable(true,"ResellerName");
ddl4.DataTextField= "ResellerName";
ddl4.DataBind();
ddl4.Items.Insert(0,new ListItem("--All--", "All"));

ds.Tables["tbcalicoinfo"].DefaultView.RowFilter= "CustName is not null";
ds.Tables["tbcalicoinfo"].DefaultView.Sort= "CustName asc";
ddl5.DataSource= ds.Tables["tbcalicoinfo"].DefaultView.ToTable(true,"CustName");
ddl5.DataTextField= "CustName";
ddl5.DataBind();
ddl5.Items.Insert(0,new ListItem("--All--", "All"));
cn.Close();
}
實現DropDownList無刷新二級聯動
一、數據庫設計:
字段名 數據類型 說明
ClassID 自動編號 類編號
ClassName varchar(8) 類名
UpClassID int(4) 上級類編號
ClassLevel int(4) 類級別,1為大類,2為小類
二涉及代碼
1、首先,我們新建一個頁面DropTest.aspx,在其中放入兩個DropDownList控件:DropDownList1和DropDownList2,其完整代碼如下:
頁面設計代碼代碼<%@ Pagelanguage="c#" Codebehind="DropTest.aspx.cs" Inherits="studyWEB.DropTest" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm2</title>
<meta content="Microsoft Visual Studio .NET 7.1"name="GENERATOR">
<meta content="C#"name="CODE_LANGUAGE">
<meta content="JavaScript"name="vs_defaultClientScript">
<metacontent="http://schemas.microsoft.com/intellisense/ie5";name="vs_targetSchema">
<script>
function load(ClassID){ //ClassID為接收傳遞的大類編號
var drp2 = document.getElementByIdx_x("DropDownList2");
function RemoveAll(oElem) { //清除DropDownList2的所有項
var i = 0;
for (i = oElem.length; i >= 0; i--){
oElem.options.remove(i);
}
}
RemoveAll(drp2)
var oHttpReq = new ActiveXObject("MSXML2.XMLHTTP");
var oDoc = new ActiveXObject("MSXML2.DOMDocument");
oHttpReq.open("POST", "DropChild.aspx?ClassID="+ClassID, false);//調用讀取小類數據的頁面,將大類
// 編號值傳遞過去
oHttpReq.send("");
result = oHttpReq.responseText;
oDoc.loadXML(result);
items1 = oDoc.selectNodes("http://CLASSNAME/Table/ClassName");//讀取所有請求大類所屬小類的類名
items2 = oDoc.selectNodes("http://CLASSNAME/Table/ClassID");//讀取所有請求大類所屬小類的編號
var itemsLength=items1.length;
for(i=0;i<itemsLength;i++)//將小類的類名和編號賦予DropDownList2
{
var newOption = document.createElement_x("OPTION");
newOption.text=items1[i].text;
newOption.value=items2[i].text;
drp2.options.add(newOption);
}
}
</script>
</HEAD>
<bodyMS_POSITIONING="flowLayout">
<form id="Form1" method="post"runat="server">
<asp:DropDownList id="DropDownList1"runat="server"></asp:DropDownList>
<asp:DropDownList id="DropDownList2"runat="server"></asp:DropDownList>
<asp:TextBox id="TH" runat="server"BorderStyle="None" ForeC olor="White"BorderColor="White"></asp:TextBox>
<asp:Label id="Label1"runat="server"></asp:Label>
<asp:Button id="Button1" runat="server"Text="Button"></asp:Button>
</form>
</body>
</HTML>
該頁面的后臺文件(DropDownList1.aspx.cs)中 Page_Load內的代碼如下:代碼
pageload代碼
if(!this.IsPostBack)
{
SqlConnection con = newSqlConnection("server=localhost;database=gswebDB;uid=sa;pwd=;");
SqlDataAdapter da = new SqlDataAdapter("select ClassName,ClassIDfrom classname where ClassLevel=1",con);
DataSet ds = new DataSet();
da.Fill(ds);
this.DropDownList1.DataSource=ds.Tables[0].DefaultView;
this.DropDownList1.DataTextField = "ClassName";
this.DropDownList1.DataValueField = "ClassID";
this.DropDownList1.DataBind();
this.DropDownList1.Attributes.Add("onchange","load(this.options[this.selectedIndex].value)");//將ClassID作為參數傳遞給腳本函數load(ClassID),如果要傳遞的是ClassName,應將value改為innerText,但如果大類為中文,則調用小類時出現無法顯示的問題
//this.DropDownList2.Attributes.Add("onChange","javascript:document.Form1.TH.value=this.options[this.selectedIndex].value;");//讀取DropDownList2的值,將其賦給一個TextBox控件TH,以獲取DropDownList2的值,
此頁面實現如下功能:首先從數據庫內讀取所有類級別為1(即大類)的類名和類編號,綁定到DropDownList1控件上;然后通過DropDownList1的Attributes屬性調用javascript函數load(ClassID);load()函數通過調用DropChild.aspx頁面,讀取XML流,得到大類所屬小類的ClassName和ClassID。
2、新建DropChild.aspx頁面文件,其中不插入任何控件和文本,只在其后臺文件(DropChild.aspx.cs)中的Page_Load中加入以下代碼:
代碼if(this.Request["ClassID"]!=null)
{
int state = Convert.ToInt32(this.Request["ClassID"]);
SqlConnection con = newSqlConnection("server=localhost;database=gswebDB;uid=sa;pwd=;");
SqlDataAdapter da = new SqlDataAdapter("select ClassName,ClassIDfrom classname where UpClassID='"+state+"'",con);
DataSet ds = new DataSet("CLASSNAME");
da.Fill(ds);
XmlTextWriter writer = new XmlTextWriter(Response.OutputStream,Response.ContentEncoding);
writer.Formatting = Formatting.Indented;
writer.Indentation = 4;
writer.IndentChar = ' ';
ds.WriteXml(writer);
writer.Flush();
Response.End();
writer.Close();
該方法得到用戶選擇的大類的編號,通過查詢以后得到一個DataSet對象,使用該對象的WriteXML方法直接將內容寫到Response.OutputStream里面然后傳遞到客戶端,客戶端的load方法通過result=oHttpReq.responseText;句話得到一個XML字符串,最后解析此串。
另外,測試獲取DropDownList2值,添加了TextBox控件TH,當點擊Button時,處理事件代碼如下:代碼
private void Button1_Click(object sender, System.EventArgs e)
{
Label1.Text=TH.Text;
}
次方法為轉載,對我有所指點,分享給大家
愛華網



