I'm getting a weird behavior with a dropdownlist when I trigger a postback.
If the dropdownlist is enabled, the selected value remains the same after a postback.
However, if the dropdownlist is disabled (via a javascript when user ticks a checkbox), then the selected value is reset to the first item in the list.
How come ?
you need to look up the value manually (probably store it in another field or so), and then set it yourself in the code behind. This is because if a control is disabled, the value would not be posted back to the sever.
Check out this question, and refer to the first answer by Pavan
how to get selected value of Disabled dropdown in c#
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
Related
I have a page where The Dropdownbox (selUsers) is populated from a database on pageload.
On LoadComplete I am setting the value based on a value from the maser page.
selUsers.SelectedValue = master.intUserID
This appears to work properly, as the correct text is displayed in the dropdownbox.
I have a button that calls a sub. In that sub, hen I call selUsers.SelectedItem.Value or selUsers.SelectedValue I get the value of the first item in the dropdownbox not the selected one.
I have been reading and I am seeing that the code to set the value has to be in ispostback, but I am not posting the page back, nor do I want to. Is it truly the case that even if you set the value of the dropdown, it isn't really set until you post the page back? If that isnt the case, why am I not getting the correct value.
Before you ask, I have scoured the code, and at no place else is the dropdown being rebound, or the value being reset.
the whole page life cycle can be a tad confusing.
I'd advise you to read this (article: ASP.NET Page Life Cycle Overview) https://msdn.microsoft.com/en-us/library/ms178472.aspx
But to answer your question:
If you click that button , the following takes place in this specific order (relevant to your problem):
1) Page load event gets raised: this is where you get your data and databind, thus 'resetting' the selected value on the dropdownlist as well.
2) The button click event gets raised (this is an item event): You read out the value you want to read out
BUT since you haven't reached LoadComplete yet (where you'll set the new selected value) you'll see the first value in the list.
3) Load complete event gets raised: here you set the correct selected value.
So yes, if you click on an asp web form's button (by default), it will cause a postback and go through the whole page life cycle to build the 'response' to your action. You'll have to take this into account while building your pages.
You can see this in action while debugging: place breakpoints on those 3 points in your code behind file and then click the button.
To solve your problem, you'll need to set the selected value after you databind your dropdownlist AND before you read out the selected value.
Hope this helps.
when I am disable dropdownlist using js, and then I access from .cs page.
It gives o value even there is one item is selected..
I want to disable dropdownlist but at the same I want to access its selected value as well
You cant access the disabled items from Code behind.
Disabled form values will not be sent in post data.
Check this post
Disabled form fields not submitting data
If you need add some css techniques like opacity and do.
Try to add selected value of dropdown in hidden field, and access value from hidden field.
In our project we use ASP.net server-side controls (not my idea).
We have some client-side code that might disable some of those controls.
However, I'm unable to retrieve the SelectedValue of listboxes which have been disabled:
If ListboxA is disabled on the client side, then on postback, ListboxA.SelectedValue is empty (even though there is a selected value). if the listbox is not disabled- the selected value is returned correctly.
I found a workaround for this by simply enabling all the controls before postback, but that's... kinda stupid. anybody got a better idea?
P.S: example scenario: if a user chooses 'administrator' in the 'user type' listbox, the 'permissions' listbox has the value 'all' set, and becomes disabled, so that administrator can only be created with all permissions.
When the control is disabled, the client will not post the value back to the server, so you cannot retrieve the value that way. This would be better than the client sending data for disabled controls when we don't need it, so the workaround is not "stupid".
However, you have two (probably more) workarounds:
Re-enable the control just before Postback.
Save the value in a hidden field.
Put a textbox, a checkbox and a button on a website.
Set the "EnableViewState" property of textbox and checkbox to false.
Write something into textbox and check the checkbox.
Click the button.
Why is the textbox still written and the checkbox checked after response?
Some things aren't totally dependent on ViewState. In the controls you listed, those values are available in the POST sent to the server, so they're gotten out of there and the controls restore their state that way.
Other things, like the text in a <asp:Label> for instance aren't sent back in any way, and they'll lose their data without ViewState. The same is true for other properties, like the styling of the textbox, etc...only it's value will be restored, because that's all that's sent back and as a result, all it's coded to grab and restore. If you were to say make it red, that would be lost on postback.
As a general rule, what a control can restore strictly from posted data will be restored on postback, everything else is lost.
Because HTML Controls are Stateless control. Therefore Microsoft provide a feature of ViewState that help when a user sends the data into server or after post back the value remain same. Therefore you have to set the property "EnableViewState" to True. By default, all the ASP.NET controls have their EnableViewState set to True
I have a DropDownList on an ASP.NET master page and I want to change some values and refresh the page when I select a different item from the list. I enabled the post back property in the DropDownList but it still gets back always to the first value whenever I select. Any advice?
Please post some more code to show where you are setting the value of the dropdown (both on load and anywhere else).
Usually, this is just a case of not understanding the event model. Try setting breakpoints on all of those points where you set the value and step through the code.
The most obvious case would be if you're setting the value in the page_load event handler and not wrapping it in a check for !Page.IsPostback