DropDownList not maintaining viewstate

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
 
B

Bruno Sirianni

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
 
S

sklett

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
 
J

Jim Corey

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
 
S

sklett

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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top