Disabling Viewstate Causes selected dropdown values to default to -1 - asp.net

I don't get this. So I disable viewstate on my .aspx page. I then select options from 3 dropdown menus and then on submit check the values and they are all -1. I put ViewState back on and do the same, select values and now in the submit event they're all set to a valid value for the SelectedIndex.
I don't get this. I don't see how viewstate has anything to do with selecting values in a dropdown and then calling a server-side handler to pick up those selected values in order to do something with it and in order to get the actual valid SelectedIndex values that the user selected. I don't see why it would give me a -1 if I disable viewstate for the SelectedIndex for each of those 3 dropdownlist controls.

Either move your code that binds the dropdowns into Page_Init or surround them with
if (!IsPostPack) { ... }

Related

disabled dropdownlist does not keep selected value on postback

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

DropDownList.SelectedItem sometimes null, sometimes not after postback, EnableViewState=False

I have two very similar .aspx pages. Both of them contain a DropDownList control. Both DropDownList controls' EnableViewState is set to False:
<asp:DropDownList ID="ddl" runat="server" EnableViewState="False" />
There is a LinkButton on both pages. In the btn_Click handler, if I try to access ddl.SelectedItem property (which, as far as I know, should be null because of the EnableViewState=False, am I correct?), it is null on one page, but not null (it has a correct value) on the other page. Can you give me any pointers how can that be possible?
string txt = ddl.SelectedItem.Text; // SelectedItem sometimes null, other times not
The difference between those two pages's dropdowns is, that in one case, the dropdown is filled using AjaxToolkit's CascadingDropDown control (in that case, SelectedItem is not null, despite having EnableViewState property set to false), in the other case, dropdown's items are being filled in the Page_Load property, within and if-block, checking whether IsPostBack is false (items are only filled on the first request, I won't need them after the postback).
Thank you.
CascadingDropDown doesn't honor the enable viewstate setting. If you need it null, you should reset it to null on page load or a similar event.
If you have something populating the drop-down through JavaScript then ASP.NET will see that value, irrespective of how it got there.
Looks like the selected index gets posted back to the server when you change the value; the selected index property gets applied once items are applied in the list, so if you have no items, I don't believe it gets applied until some items get added to the list. So, if you bind the list again, and a value got selected from the list, and you rebind the drop down, it would apply the selected index. That could be it possibly.
HTH

prevent DropDownList from going to the original value when refreshing the page

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

DropDownList Setting Default Value

How should I get a value displayed in the DropDownList when redirected through a cancel buttton. The value in the page where cancel button is there in a TextBox should be caught in the DropDownList.
This DropDownList has SelectedIndexChanged event also fired for which on selection of a country in the list we get a ListView displayed in the same page. In that ListView we have an add button which will redirect us to another page called addcountry in which we have few controls. In those controls one TextBox I am getting value through QueryString in an enabled False state. Now again I need that value displayed in the DropDownList when I click cancel button.
How can I solve this problem?
Using $_POST and $_GET Variables you should be able to retreive almost any value from a form and put it in the right place on a new page, this right place can be one of your drop down list element...
To make absolutly sure your Cancel Button will send the good value to the right page put it in an independant form that has your new page (where you have yout dropdown list) as its action, and in this seperate form put an hidden field that holds the specific value you wanna see in this dd list ...
Hope I am clear but starting with your very "fuzzy" and blur question It is hard to help you.

ASP.net listbox control return -1

I am trying to put a listbox control on my ASP.net page, and when I click it, the selectedindex value is always -1. Why is it doing that? I set AutoPostBack to true. Why is it always returning -1?
Please let me know.
Thanks
There could be many reasons why but I am guessing that you are loading the contents of the ListBox on every load of the page.
Wrap your data binding code in an if statement like this to allow the control to retain what index you selected:
if (!this.IsPostBack)
{
// data binding code here
}
It does depend on what your doing, but -1 normally means nothing is selected when a PostBack is occurring, or that the list of items is empty that the control is being databound to.

Resources