dropdown list maintains selection when back button is used.

M

Mark

I have two dropdown lists. Both have autopostback set to true. In both
dropdowns, when you select an item from the list, it redirects to the Value
property of the dropdown. Nothing fancy.

Let's say you select 1 of the items, and are properly redirected. You press
the back button. I have three servers providing two different
functionalities:

1. After pressing the back button, the item you selected in the dropdown is
still selected.
2. After pressing the back button, the default item in the dropdown is
selected again.

In the scenario number 1, if you postback again (let's say you now select
the OTHER dropdown list), *both* dropdown lists selected indexed changed
events fire. Or more precisely, one fires but the other does not because
the first one redirects before the second can fire.

I have copied the .dll and .aspx pages several times to confirm I am using
the same code.

Any ideas??? I would love the second scenario to be the default behavior in
all cases.

Thanks in advance.

Mark
 
M

Mark

Drat. For good or for bad, you can easily recreate this on your own. I'd
LOVE to find a work around for this.

All you need to do is create a new web form, and add 2 dropdowns and a
checkbox. Make the id of the checkbox be "ckRedirect":

private void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
DropDownList1.Items.Add(new ListItem("1","a"));
DropDownList1.Items.Add(new ListItem("2","b"));
DropDownList1.Items.Add(new ListItem("3","c"));

DropDownList2.Items.Add(new ListItem("4","d"));
DropDownList2.Items.Add(new ListItem("5","e"));
DropDownList2.Items.Add(new ListItem("6","f"));

DropDownList1.AutoPostBack = true;
DropDownList2.AutoPostBack = true;
}
}
private void DropDownList1_SelectedIndexChanged(object sender,
System.EventArgs e)
{
Response.Write("<br>DDL1 selected index fired.");
if (ckRedirect.Checked)
Response.Redirect("http://www.cnn.com/");
}

private void DropDownList2_SelectedIndexChanged(object sender,
System.EventArgs e)
{
Response.Write("<br>DDL2 selected index fired.");
if (ckRedirect.Checked)
Response.Redirect("http://www.aol.com/");
}
 
M

Mark

Doubly interesting ... if you turn tracing ON and display it on the page
using your web.config file, this behavior goes away!

????

Ideas?

Thanks in advance.

Mark
 
K

Ken Dopierala Jr.

Hi Mark,

When a user hits the back button the page does not go back to the server.
It is shown in the same state as the user left it. Internet Explorer even
returns them to the same position on the page they had scrolled to before
leaving. In order to make this work you need to find out if an event fires
client-side when the page is re-displayed using the back button. If there
is one, then put your client-side code in there to reset your dropdowns. I
don't know of one off the top of my head but if I get a chance I'll look
into it. This should set you on the right path though. Good luck! Ken.
 
K

Ken Dopierala Jr.

Hi Mark,

It appears that the onload() event fires after the back button takes you to
the previous page. Add the onload() event to your <body> tag and when it
fires reset your drop down lists with client-side javascript. Then you
should be all set. Good luck! Ken.
 
M

Mark

i like this idea a lot. thanks! HOWEVER, my gut wonders if the onload()
event would cause problems when you select an item from the Dropdown each
time. When the page posts back, the onload() would fire and wipe out what
the user selected. I'm nervous the dropdown code would then fire, but use
the incorrect (default) value??

Just typing theoretically here ... thanks again.

mark
 
K

Ken Dopierala Jr.

Hi Mark,

If your page posts back to the server and instead of redirecting to another
page displays itself again, then this would cause a problem. You will need
to keep some type of state to know when to actually run the code in your
onload event. The easiest way would be to create a hidden HTML input field
with the runat="server" attribute set. If your page posts back to itself
and needs to keep the current drop down state then set this value to 1 on
the server side code. In the function you call from the onload event check
this field. If it is 0 then reset your list boxes. If it is 1 then ignore
the listboxes and instead reset this variable to 0. You need to do the
reset to 0 client-side because if you did it server side and then to a
redirect it would never get back to your page and when they hit the back
button the value would still be 1 and the list boxes wouldn't reset. Good
luck! Ken.
 

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

Top