如果要想給 DropDownList 服務(wù)器控件添加客戶端下拉事件,我們可以強(qiáng)制給它添加 onchange 事件,盡管在控件中沒(méi)有這個(gè)方法的提示。添加完這個(gè)事件還不能達(dá)到目的,還要設(shè)置 AutoPostBack 屬性為 false,不讓它回發(fā)后臺(tái)事件。
以下就是為大家分享的代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
< html xmlns = "http://www.w3.org/1999/xhtml" > < head runat = "server" > < title >DropDownList添加客戶端下拉事件</ title > < script type = "text/javascript" > function getDropDownList() { var ddl1 = document.getElementById("<%=ddl1.ClientID%>"); var text = ddl1.options[ddl1.options.selectedIndex].text; //獲取text值 var value = ddl1.value; //獲取value值 alert("Text:" + ddl1.options[ddl1.options.selectedIndex].text + ", Value:" + ddl1.value); } </ script > </ head > < body > < form id = "form1" runat = "server" > < asp:DropDownList ID = "ddl1" runat = "server" AutoPostBack = "false" onchange = "getDropDownList();" > < asp:ListItem Text = "T1" Value = "V1" Selected = "True" ></ asp:ListItem > < asp:ListItem Text = "T2" Value = "V2" ></ asp:ListItem > < asp:ListItem Text = "T3" Value = "V3" ></ asp:ListItem > </ asp:DropDownList > </ form > </ body > </ html > |
總結(jié)一下,也就是說(shuō),要想給DropDownList下拉框添加客戶端下拉事件,必須做兩步工作,一是添加強(qiáng)制onchange事件,二是把 AutoPostBack屬性設(shè)為false,就是這么簡(jiǎn)單!