How to get selected value of disabled drop down list in C #

There are two DropDownList controls in my form.

1st is on and the second is off.

After selecting the 1st drop-down list, I change the selected value of the 2nd drop-down list using javascript.

Works great. But when I try to get the selected value of the second drop-down list, it will return the value of the first element (ie, "Select").

Please write my code

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

NOTE: I use javascript to change the selected value of the 2nd (disabled) drop-down list.

Javascript Code:

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

Is there an alternative way to get the modified value of a disabled DropDownList?

+6
source share
3 answers

If you try to read the value of the 2nd drop-down list (disabled) on the server, you can never read the updated value because the data in the disabled controls will not be sent back to the server from the client.

You must either enable the drop-down list before sending your data to the server, or use hidden controls to store the data in your drop-down list.

+11
source

You need to add another input that is hidden . Whenever you change the value of the 1st DropDownList , you must change the value of your second DropDownList and the value of the hidden input.

On the server side, you are not looking at the value of the 2nd DropDownList , but at the value of your hidden input. Make sure the hidden value is always in sync with the second DDL when submitting your form.

+1
source

Just add the disabled dropdownlist value to the hidden field when changing, and then read the value from the hidden field, not the dropdownlist. Perhaps this will help you.

0
source

Source: https://habr.com/ru/post/904799/


All Articles