how to get selected value of Disabled dropdown in c# - 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?

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. 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.

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.

Related

Set caption for dropdown list

I have a dropdownlist on a page for which I want to set a text so that whether or not user makes any selection, it should always be displayed to the user and not the selected value.
For example:
Here Select Language should always be visible to user even if I select any value from the list i.e. Option 1 or Option 2.
I guess it is more of a CSS task which I am not very much comfortable with.
If you are talking about a simple asp:DropDownList, you just need to insert an item in it, with an index value of 0 (to deal with server-side checks).
myDropDownList.Items.Insert(0, "Select");
dropdownlist1.Text="SomeText";
Dropdownlist need to have the data-source.
Here Select Language should always be visible to user even if I select
any value from the list i.e. Option 1 or Option 2.
Solution:
if your dropdownlist isn't autopost back enabled then
Maintain a hidden field where on change event of the dropdownlist place the selected value or selected index or selected text there in the hidden field to remember the selection.
lets says the id of your dropdownlist is sel and hidden field is hdSel, the jquery code for that look likes below
$('#sel').change(function(){
var val = $("#sel option:selected").text();
$('#hdSel').val(val);
$("#sel").prop('selectedIndex', 0);
}
);
example on Jsfiddle for your understanding
If your dropdownlist is autopostback enable then you can assign the selected value or selected text or selected index of the dropdownlist to the hidden field and reselect the first item again
Hope this help

Change value of same dropdown in other rows of radgrid when one dropdown value is changed

I have a drop down with yes/no as data inside a rad grid. When the value of dropdown in row is changed, values of dropdown in other rows excluding this should be set with second item. This must be done with javascript.
You could open this link.
In addition, I would suggest you changing this line:
GridEditFormItem item = DropDownList1.NamingContainer as GridEditFormItem;
to
GridEditableItem item = DropDownList1.NamingContainer as GridEditableItem;
in order to support both edit and insert scenario.
it would require 2 steps
right click on the dropdown a pop up will appear then check the postback option
after that simply double click on the dropdownlist as a result function will get created in your filename.cs file now put the code for adding the option for the other dropdown list there and u r done

Html.DropownlistFor with editablefield

I'm using a dropdownlistfor that populates some data from the database, the idea is that i want that the default value of the dropdown, has to be editable.
So the user can submit one of the options that are populated from the database, or the default one modified
How can i make this?
You could use an onclick event on the first value that triggers a jquery pop-up with a textfield which could rewrite your first value of the dropdown on submit.

How to disable dropdown list value in the current form that was selected in the previous page

I am having 2 web formsand will have a drop down list on those web forms. If i select a value from drop down and click on ok i will get tranfer to next page. In that page i will have a drop down with the same values in the previous form . What i need is i would like to disable the selected value in the previos form and would like to display the remaining normal
I dont know your exact scenario but i would suggest that you look at whether you can maybe achieve your needs using a single form instead of two.
If that is not an option :-
If you are POSTING the form on click of OK, you can retrieve the value of the original page using PreviousPage. Refer eg below.
I am not quite sure what you mean by "disable the selected value in the previos form and would like to display the remaining normal" but once you retrieve this value, then you can manipulate the data in your current page any way you want.
DropDownList oldValue = (DropDownList)PreviousPage.FindControl("DropDownOldValue");
oldValue.SelectedValue - should then give you the value selected on the previous page
Got the answer
DropDownList oldvalue = (DropDownList)PreviousPage.FindControl("DropDownList1");
string str = oldvalue.SelectedValue.ToString();
ListItem i = DropDownList1.Items.FindByValue(str);
i.Attributes.Add("style", "color:gray;");
i.Attributes.Add("disabled", "true");

On selected index change in Dropdownlist1 should select same value in Dropdownlist2?

I Have a page where there are two Dropdownlist and both have the same data in it but when user selects the value in Dropdownlist1 ,same value should be selected automatically in Dropdownlist2?
Thanks
Smartdev
You can do that a couple of ways. If you're using the built in asp.net postback approach then you can set the value of the dropdown box server side.
If you're wanting to do this on the front end you can use jQuery (or standard Javascript) to set the selectedIndex of the second dropdown.
I assume that's the kind of answer you are after and not just for someone to write the code for you. :)
so in your code behind OnSelectedIndexChanged, you need to rebind both DropDownList1 and DropDownList2 to the same datasource.

Categories