當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
客户端用javascript填充Dropdownlist,服务器端获取不到Dropdownlist的值
生活随笔
收集整理的這篇文章主要介紹了
客户端用javascript填充Dropdownlist,服务器端获取不到Dropdownlist的值
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
2019獨角獸企業重金招聘Python工程師標準>>>
今天遇到一個奇怪的問題,某一頁面需要使用三級級聯下拉列表框。為提高用戶體驗,采用jQuery的cascadingDropDown插件調用后臺Web Services來實現ajax填充。
填充沒有任何問題,但是在服務器端卻取不出來下拉表中的內容。頁面代碼如下。?
<form id="form1" runat="server">?
<div>?
<h3>看看用js填充的dropdownlist控件在服務器端能讀出來嗎?</h3>?
三個級聯下拉列表框:?
<asp:DropDownList runat="server" id="bigTypeList" Width="150">?
</asp:DropDownList>?
<asp:DropDownList runat="server" id="typeList" Width="150">?
</asp:DropDownList>?
<asp:DropDownList runat="server" id="smalltypeList" Width="150">?
</asp:DropDownList>?
<br />?
<asp:Button runat="server" Text="讀取下拉表" ID="OK" οnclick="OK_Click" /><br />?
你選的是:<asp:Label runat="server" Text="Label" ID="label1"></asp:Label>?
</div>?
</form>?
用來測試的后臺代碼如下。?
protected void OK_Click(object sender, EventArgs e)?
{?
ListItem[] array = new ListItem[3];?
array[0] = bigTypeList.SelectedItem; //為null?
array[1] = typeList.SelectedItem; //為null?
array[2] = smalltypeList.SelectedItem; //為null?
}?
事實證明,在服務器端讀取客戶端填充的DropDownList控件的值時,根本讀不到任何內容。DropDownList.Items.Count為0,DropDownList.SelectedItem為null。?
那么,怎么得到這個值呢,只好使用Request.Form["控件的客戶端ID"]了。如下代碼所示。?
string s=Request.Form[typeList.ClientID];?
附:頁面中的JavaScript文件。?
<script language="javascript" type="text/javascript">?
$(function () {?
var bigId = '#<%=bigTypeList.ClientID%>';?
var mediumId = '#<%=typeList.ClientID%>';?
var smallId = '#<%=smalltypeList.ClientID%>';?
$(bigId).cascadingDropDown(mediumId,?
'../Services/AutoTypeService.asmx/getAutoType',?
{ valueMember: 'id', displayMember: 'name', cascadingArgName: 'parent' });?
$(mediumId).cascadingDropDown(smallId,?
'../Services/AutoTypeService.asmx/getSubAutoType',?
{ valueMember: 'id', displayMember: 'name', cascadingArgName: 'parent' });?
});?
</script>?
轉載于:https://my.oschina.net/u/1775160/blog/678261
總結
以上是生活随笔為你收集整理的客户端用javascript填充Dropdownlist,服务器端获取不到Dropdownlist的值的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: tornado总结7-文件切片异步非阻塞
- 下一篇: JavaScript权威设计--事件冒泡