DropDownList添加客戶端下拉事件操作
更新時間:2015年09月29日 09:05:34 投稿:lijiao
我們知道,DropDownList下拉框是一個服務器控件,有時候,有些朋友為了方便綁定DropDownList下拉框的選項,但又想在DropDownList實現(xiàn)客戶端的下拉事件,那該怎么實現(xiàn)呢?
如果要想給 DropDownList 服務器控件添加客戶端下拉事件,我們可以強制給它添加 onchange 事件,盡管在控件中沒有這個方法的提示。添加完這個事件還不能達到目的,還要設置 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>
總結(jié)一下,也就是說,要想給DropDownList下拉框添加客戶端下拉事件,必須做兩步工作,一是添加強制onchange事件,二是把 AutoPostBack屬性設為false,就是這么簡單!
您可能感興趣的文章:
- 在dropDownList中實現(xiàn)既能輸入一個新值又能實現(xiàn)下拉選的代碼
- DropDownList 下拉框選擇改變促發(fā)事件和防全局刷新(推薦)
- DropDownList綁定選擇數(shù)據(jù)報錯提示異常解決方案
- 用javascript為DropDownList控件下拉式選擇添加一個Item至定義索引位置
- asp.net中不能在DropDownList中選擇多個項 原因分析及解決方法
- ASP.NET中DropDownList下拉框列表控件綁定數(shù)據(jù)的4種方法
- C#使用DropDownList綁定添加新數(shù)據(jù)的方法匯總
- asp.net DropDownList實現(xiàn)二級聯(lián)動效果
- DropDownList實現(xiàn)可輸入可選擇(兩種版本可選)
相關(guān)文章
微信公眾平臺開發(fā)教程(二) 基本原理及消息接口總結(jié)
本篇文章主要介紹了微信公眾平臺開發(fā)教程(二) 基本原理及消息接口,具有一定的參考價值,有興趣的朋友可以了解一下。2016-12-12
ASP.NET MVC中解析淘寶網(wǎng)頁出現(xiàn)亂碼問題的解決方法
最近在使用MVC解析淘寶網(wǎng)頁出現(xiàn)亂碼問題,原因就是中文字符格式出現(xiàn)沖突,ASP.NET MVC 默認采用utf-8,但是淘寶網(wǎng)頁采用gbk。在網(wǎng)上找了一下,最常用的解決方法,特分享下2013-04-04

