客戶端用JavaScript填充DropDownList控件 服務(wù)器端讀不到值
更新時間:2010年09月13日 00:52:27 作者:
今天遇到一個奇怪的問題,某一頁面需要使用三級級聯(lián)下拉列表框。為提高用戶體驗,采用jQuery的cascadingDropDown插件調(diào)用后臺Web Services來實現(xiàn)ajax填充。
填充沒有任何問題,但是在服務(wù)器端卻取不出來下拉表中的內(nèi)容。頁面代碼如下。
<form id="form1" runat="server">
<div>
<h3>看看用js填充的dropdownlist控件在服務(wù)器端能讀出來嗎?</h3>
三個級聯(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>
用來測試的后臺代碼如下。
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
}
事實證明,在服務(wù)器端讀取客戶端填充的DropDownList控件的值時,根本讀不到任何內(nèi)容。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>
結(jié)束。
復(fù)制代碼 代碼如下:
<form id="form1" runat="server">
<div>
<h3>看看用js填充的dropdownlist控件在服務(wù)器端能讀出來嗎?</h3>
三個級聯(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>
用來測試的后臺代碼如下。
復(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
}
事實證明,在服務(wù)器端讀取客戶端填充的DropDownList控件的值時,根本讀不到任何內(nèi)容。DropDownList.Items.Count為0,DropDownList.SelectedItem為null。
那么,怎么得到這個值呢,只好使用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
淺析Repeater控件的使用 (原樣導(dǎo)出和動態(tài)顯示/隱藏Repeater中的列)
本文主要介紹了淺析Repeater控件的使用 (原樣導(dǎo)出和動態(tài)顯示/隱藏Repeater中的列)的具體方法,需要的朋友可以看下2016-12-12
詳解最好的.NET開源免費ZIP庫DotNetZip(.NET組件介紹之三)
本篇文章主要介紹了.NET開源免費ZIP庫DotNetZip組件的介紹,可以實現(xiàn)對文件的壓縮和解壓,有興趣的朋友可以了解一下。2016-12-12

