如果要想給 DropDownList 服務(wù)器控件添加客戶端下拉事件,我們可以強制給它添加 onchange 事件,盡管在控件中沒有這個方法的提示。添加完這個事件還不能達到目的,還要設(shè)置 AutoPostBack 屬性為 false,不讓它回發(fā)后臺事件。
以下就是為大家分享的代碼:
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>