客戶端用JavaScript填充DropDownList控件 服務(wù)器端讀不到值
更新時(shí)間:2010年09月13日 00:52:27 作者:
今天遇到一個(gè)奇怪的問(wèn)題,某一頁(yè)面需要使用三級(jí)級(jí)聯(lián)下拉列表框。為提高用戶體驗(yàn),采用jQuery的cascadingDropDown插件調(diào)用后臺(tái)Web Services來(lái)實(shí)現(xiàn)ajax填充。
填充沒(méi)有任何問(wèn)題,但是在服務(wù)器端卻取不出來(lái)下拉表中的內(nèi)容。頁(yè)面代碼如下。
<form id="form1" runat="server">
<div>
<h3>看看用js填充的dropdownlist控件在服務(wù)器端能讀出來(lái)嗎?</h3>
三個(gè)級(jí)聯(lián)下拉列表框:
<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" onclick="OK_Click" /><br />
你選的是:<asp:Label runat="server" Text="Label" ID="label1"></asp:Label>
</div>
</form>
用來(lái)測(cè)試的后臺(tái)代碼如下。
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
}
事實(shí)證明,在服務(wù)器端讀取客戶端填充的DropDownList控件的值時(shí),根本讀不到任何內(nèi)容。DropDownList.Items.Count為0,DropDownList.SelectedItem為null。
那么,怎么得到這個(gè)值呢,只好使用Request.Form["控件的客戶端ID"]了。如下代碼所示。
string s=Request.Form[typeList.ClientID];
附:頁(yè)面中的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>
結(jié)束。
復(fù)制代碼 代碼如下:
<form id="form1" runat="server">
<div>
<h3>看看用js填充的dropdownlist控件在服務(wù)器端能讀出來(lái)嗎?</h3>
三個(gè)級(jí)聯(lián)下拉列表框:
<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" onclick="OK_Click" /><br />
你選的是:<asp:Label runat="server" Text="Label" ID="label1"></asp:Label>
</div>
</form>
用來(lái)測(cè)試的后臺(tái)代碼如下。
復(fù)制代碼 代碼如下:
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
}
事實(shí)證明,在服務(wù)器端讀取客戶端填充的DropDownList控件的值時(shí),根本讀不到任何內(nèi)容。DropDownList.Items.Count為0,DropDownList.SelectedItem為null。
那么,怎么得到這個(gè)值呢,只好使用Request.Form["控件的客戶端ID"]了。如下代碼所示。
復(fù)制代碼 代碼如下:
string s=Request.Form[typeList.ClientID];
附:頁(yè)面中的JavaScript文件。
復(fù)制代碼 代碼如下:
<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>
結(jié)束。
相關(guān)文章
SqlCommandBuilder如何實(shí)現(xiàn)批量更新
這篇文章主要介紹了SqlCommandBuilder如何實(shí)現(xiàn)批量更新,需要的朋友可以參考下2015-10-10
asp.net中利用ajax獲取動(dòng)態(tài)創(chuàng)建表中文本框的值
通常在做主從表的數(shù)據(jù)錄入中,會(huì)碰到在一個(gè)頁(yè)面上同時(shí)錄入主表數(shù)據(jù)和從表數(shù)據(jù),主表的數(shù)據(jù)只有一條,從表的數(shù)據(jù)有一條到多條,這樣就要?jiǎng)討B(tài)創(chuàng)建從表數(shù)據(jù)錄入入口。2010-03-03
asp.net下String.prototype.split()的兼容問(wèn)題
IE下的String.prototype.split()函數(shù)bug2012-12-12
c#生成圖片縮略圖的類(2種實(shí)現(xiàn)思路)
4個(gè)重載方法,有直接返回Image對(duì)象的,有生成縮略圖,并且保存到指定目錄的,具體祥看下文2013-05-05
淺談ASP.NET Core中間件實(shí)現(xiàn)分布式 Session
這篇文章主要介紹了淺談ASP.NET Core中間件實(shí)現(xiàn)分布式 Session,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-11-11

