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

asp.net

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.

Best Answer

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.

Related Topic