To keep this simple, I have a dropdownlist and a button in an ASP.NET form. The dropdownlist has an autopostback function that calls DropDownList1_SelectedIndexChanged and the page is redirected somewhere (for this example www.google.com) and the button has an onclick that goes to Button1_Click1 and the page gets redirected to www.yahoo.com.
The Problem: If I click the button, I go to Yahoo, which is what you'd expect. If I click back button in my browser and select the dropdownlist I go to Google which is also correct but if I click the back button and then click the button I get redirected to Google. Uh? Why doesn't it go to Yahoo?
Here's my code:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test.aspx.cs" Inherits="test" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Testing Auto-Postback</title>
</head>
<body>
<form id="form1" runat="server">
<asp:DropDownList ID="DropDownList1" runat="server" onselectedindexchanged="DropDownList1_SelectedIndexChanged" AutoPostBack="true" ValidationGroup="form1">
<asp:ListItem>Please select</asp:ListItem>
<asp:ListItem>Go to Google</asp:ListItem>
</asp:DropDownList>
<hr />
<asp:Button ID="Button1" runat="server" Text="Go to Yahoo"
ValidationGroup="form2" onclick="Button1_Click1" />
</form>
</body>
</html>
Code Behind:
using System;
public partial class test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
Response.Redirect("http://www.google.com");
}
protected void Button1_Click1(object sender, EventArgs e)
{
Response.Redirect("http://www.yahoo.com");
}
}
If anyone could help me, it would be most appreciated.
Well, after some digging, i found the following:
When we click the
Button
, the page life cycle till theButton1_Click1
event is raised happens like this:Now, when we change the
DropDownList
, the page life cycle till theDropDownList1_SelectedIndexChanged
event is raised happens like this:Analising both page life cycles, we see that the
DropDownList1_SelectedIndexChanged
event is raised on the Page 'ChangedEvents' procedure, this method happens earlier than the Page 'PostBackEvent' procedure that raises theButton1_Click1
event.Now, when you change the
DropDownList
SelectedIndex you are redirected to Google. When you hit the back button, the browser retrieves the last state of that page, meaning that theDropDownList
will remain with the value you changed before. If, at that stage you click the button, both theDropDownList
and the Button are being sent on the request values. As theDropDownList
event is raised first, the page get redirected to Google again.Update:
One work arround could be implement the following on the code behind:
On the OnInit event, the code identifies the events that will be raised by asp.net. When both events are present, we apply the button click behavior as it has the priority in this case (it was clicked).
If you don't mind, you could also do it simpler: