Abhi
Abhi

Reputation: 2013

how to get selected value of Disabled dropdown in c#

In my form there are two DropDownList controls.

1st is enabled and 2nd is disabled.

After selecting 1st dropdown I am changing selected value of 2nd dropdown using javascript.

Its working fine. But when I am trying to get selected value of 2nd dropdown, it will return value of first element (i.e. '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 code:

$(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?

Upvotes: 6

Views: 16862

Answers (3)

Mohit
Mohit

Reputation: 39

Just Add disabled dropdownlist's value in hidden field on change then read value from hidden field rather than dropdownlist.may be this will help you.

Upvotes: 0

Gabriel GM
Gabriel GM

Reputation: 6639

You would need to add another input that is hidden. 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.

Server-side, you're not looking at the value of the 2nd DropDownList, but at the value of your hidden input. Make sure the hidden value is always sync with the 2nd DDL when you post your form.

Upvotes: 1

Pavan
Pavan

Reputation: 4329

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.

Upvotes: 12

Related Questions