DropDownList not maintaining viewstate

  • Thread starter Thread starter sklett
  • Start date Start date
S

sklett

I have 2 dropdownlist on a page. They have ViewState = true. After I
submit to the page, the selection in the DropDownList is lost. I set a
break in the Page_Load event to make sure that I wasn't re-binding them
somehow... I'm not. So shouldn't they maintain their selection?

Anything you can think of to check?

Thanks-
Steve
 
DropDownList must remember their selection!
I have this example and it works fine...
This page when selected value change make a postback.
Selected value in DropDownList never change.

[C# - CodeBehind]
if (!IsPostBack)
{
ArrayList al = new ArrayList();
al.Add("aa");
al.Add("bb");
al.Add("cc");
DropDownList1.DataSource = al;
DropDownList1.DataBind();

al.Add("dd");
DropDownList2.DataSource = al;
DropDownList2.DataBind();
}

[ASPX page]
<asp:DropDownList AutoPostBack="True" id="DropDownList1"
runat="server"></asp:DropDownList>
<asp:DropDownList AutoPostBack="True" id="DropDownList2"
runat="server"></asp:DropDownList>

Brun
 
That is really odd, I have 2-3 pages that all have the opposite behavior.

If I have a dropdownlist with 3 items, a, b, and c

Auto postback set to true

if I select 'b', when the page is rendered after the post back, 'a' is
selected.

I don't know what to do differently...


Thanks for the response though, looks like I have gremlins somewhere ;(


Bruno Sirianni said:
DropDownList must remember their selection!
I have this example and it works fine...
This page when selected value change make a postback.
Selected value in DropDownList never change.

[C# - CodeBehind]
if (!IsPostBack)
{
ArrayList al = new ArrayList();
al.Add("aa");
al.Add("bb");
al.Add("cc");
DropDownList1.DataSource = al;
DropDownList1.DataBind();

al.Add("dd");
DropDownList2.DataSource = al;
DropDownList2.DataBind();
}

[ASPX page]
<asp:DropDownList AutoPostBack="True" id="DropDownList1"
runat="server"></asp:DropDownList>
<asp:DropDownList AutoPostBack="True" id="DropDownList2"
runat="server"></asp:DropDownList>

Brun


sklett said:
I have 2 dropdownlist on a page. They have ViewState = true. After I
submit to the page, the selection in the DropDownList is lost. I set a
break in the Page_Load event to make sure that I wasn't re-binding them
somehow... I'm not. So shouldn't they maintain their selection?

Anything you can think of to check?

Thanks-
Steve
 
I got stumped for awhile on a similar situation when
I set the item values to something other than the item text.

So for example if the resulting html looked like this:
<option value="2">Abel</option>
<option value="3">Baker</option>
<option value="2">Charlie</option>

and I selected 'Charlie', when the form posted back
the first item with the selected value (2 in this case)
would display, so 'Abel' would be displayed.

HTH,
Jim
 
Jim,

Yeah, that would be a fun one to find ;)
however, that is not the case for me. I set a breakpoint in the page_load
to check what the dropdown list's selected index was and it was correct IE:
if I had selected the 3rd item before postback, the SelectedIndex value was
2

but once the page is rendered... it's back to selecting the first item.

so strange.

Thanks for sharing your situation though, will come in handy some day!
 
Back
Top