客戶端用JavaScript填充DropDownList控件 服務(wù)器端讀不到值
更新時(shí)間:2010年09月13日 00:52:27 作者:
今天遇到一個(gè)奇怪的問題,某一頁面需要使用三級(jí)級(jí)聯(lián)下拉列表框。為提高用戶體驗(yàn),采用jQuery的cascadingDropDown插件調(diào)用后臺(tái)Web Services來實(shí)現(xiàn)ajax填充。
填充沒有任何問題,但是在服務(wù)器端卻取不出來下拉表中的內(nèi)容。頁面代碼如下。
<form id="form1" runat="server">
<div>
<h3>看看用js填充的dropdownlist控件在服務(wù)器端能讀出來嗎?</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>
用來測(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];
附:頁面中的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ù)器端能讀出來嗎?</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>
用來測(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];
附:頁面中的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)文章
ASP.NET 2.0服務(wù)器控件開發(fā)之復(fù)雜屬性
ASP.NET 2.0服務(wù)器控件開發(fā)之復(fù)雜屬性...2006-09-09
asp.net中GridView編輯,更新,合計(jì)用法示例
這篇文章主要介紹了asp.net中GridView編輯,更新,合計(jì)用法,結(jié)合實(shí)例形式分析了asp.net中GridView常見的操作技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2016-07-07
Discuz!NT 3與asp.net 整合的實(shí)例教程
本次整合只針對(duì)NETSNS中的代碼做了少許修改,完成了基本的和論壇同步注冊(cè),登陸和注銷,信息獲取,信息修改。為的是給各位Discuz!NT API愛好者做一個(gè)簡(jiǎn)單的API事例,供大家參考。2009-11-11
淺析Repeater控件的使用 (原樣導(dǎo)出和動(dòng)態(tài)顯示/隱藏Repeater中的列)
本文主要介紹了淺析Repeater控件的使用 (原樣導(dǎo)出和動(dòng)態(tài)顯示/隱藏Repeater中的列)的具體方法,需要的朋友可以看下2016-12-12
詳解最好的.NET開源免費(fèi)ZIP庫(kù)DotNetZip(.NET組件介紹之三)
本篇文章主要介紹了.NET開源免費(fèi)ZIP庫(kù)DotNetZip組件的介紹,可以實(shí)現(xiàn)對(duì)文件的壓縮和解壓,有興趣的朋友可以了解一下。2016-12-12
.net SMTP發(fā)送Email實(shí)例(可帶附件)
本文為大家詳細(xì)介紹下.net SMTP發(fā)送Email同時(shí)可帶附件的具體實(shí)現(xiàn)思路及代碼,想實(shí)現(xiàn)的朋友可以參考下哈,希望對(duì)大家有所幫助2013-07-07

