用戶端用JavaScript填充DropDownList控制項 伺服器端讀不到值

來源:互聯網
上載者:User

標籤:items   級聯   nbsp   []   pdo   tar   pes   client   內容   

填充沒有任何問題,但是在伺服器端卻取不出來下拉表中的內容。頁面代碼如下。

<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" 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 
} 

事實證明,在伺服器端讀取用戶端填充的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> 

用戶端用JavaScript填充DropDownList控制項 伺服器端讀不到值

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.