简体   繁体   English

如何在c#中获取禁用下拉列表的选定值

[英]how to get selected value of Disabled dropdown in c#

In my form there are two DropDownList controls. 在我的表单中有两个DropDownList控件。

1st is enabled and 2nd is disabled. 第1个启用,第2个禁用。

After selecting 1st dropdown I am changing selected value of 2nd dropdown using javascript. 选择第一个下拉列表后,我正在使用javascript更改第二个下拉列表的选定值。

Its working fine. 它的工作正常。 But when I am trying to get selected value of 2nd dropdown, it will return value of first element (ie 'select'). 但是当我试图获得第二个下拉列表的选定值时,它将返回第一个元素的值(即'select')。

Please refer my code 请参考我的代码

<asp:DropDownList ID="ddlStartTime1" runat="server" AutoPostBack="false" 
 Width="70" Enabled="false"></asp:DropDownList>

NOTE: I am using javascript to change selected value of 2nd(disabled) dropdown. 注意:我使用javascript更改第二(禁用)下拉列表的选定值。

Javascript code: Javascript代码:

$(document).ready(function() {
    $('#<%= ddlStartTime1.ClientID %>').change(function() {
        $('#<%= ddlEndTime1.ClientID %>').val($('#<%= ddlStartTime1.ClientID%>').val());
    })
});

Is there any alternate way to get changed value of disabled DropDownList? 是否有任何替代方法来获取已禁用的DropDownList的值?

If you are trying to read the value of 2nd dropdown (disabled one) at server, you will never be able to read the updated value, becuase data in disabled controls will not be posted back to server from client. 如果您尝试在服务器上读取第二个下拉列表(禁用一个)的值,您将永远无法读取更新的值,因为禁用控件中的数据将不会从客户端发回服务器。

You should either enable the dropdown before posting your data to server or use hidden controls to hold data of your disabled dropdown. 您应该在将数据发布到服务器之前启用下拉列表,或者使用隐藏控件来保存禁用下拉列表的数据。

You would need to add another input that is hidden . 您需要添加另一个hidden input Whenever you change the value of your 1st DropDownList , you'd change the value of your 2nd DropDownList AND the value of the hidden input. 每当您更改第一个DropDownList的值时,您将更改第二个DropDownList的值和隐藏输入的值。

Server-side, you're not looking at the value of the 2nd DropDownList , but at the value of your hidden input. 在服务器端,您不是在查看第二个DropDownList的值,而是查看隐藏输入的值。 Make sure the hidden value is always sync with the 2nd DDL when you post your form. 发布表单时,请确保隐藏值始终与第二个DDL同步。

只需在隐藏字段中添加禁用的下拉列表值,然后从隐藏字段读取值而不是dropdownlist。可能这会对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM