Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8749439
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:41:43+00:00 2026-06-13T12:41:43+00:00

To keep this simple, I have a dropdownlist and a button in an ASP.NET

  • 0

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 http://www.google.com) and the button has an onclick that goes to Button1_Click1 and the page gets redirected to http://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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T12:41:44+00:00Added an answer on June 13, 2026 at 12:41 pm

    Well, after some digging, i found the following:

    When we click the Button, the page life cycle till the Button1_Click1 event is raised happens like this:

    Begin PreInit
    End PreInit
    Begin Init
    End Init
    Begin InitComplete
    End InitComplete
    Begin LoadState
    End LoadState
    Begin ProcessPostData
    End ProcessPostData
    Begin PreLoad
    End PreLoad
    Begin Load
    End Load
    Begin ProcessPostData Second Try
    End ProcessPostData Second Try
    Begin Raise ChangedEvents
    End Raise ChangedEvents
    Begin Raise PostBackEvent
    Raised Button1_Click1 // Button event here
    

    Now, when we change the DropDownList, the page life cycle till the DropDownList1_SelectedIndexChanged event is raised happens like this:

    Begin PreInit
    End PreInit
    Begin Init
    End Init
    Begin InitComplete
    End InitComplete
    Begin LoadState
    End LoadState
    Begin ProcessPostData
    End ProcessPostData
    Begin PreLoad
    End PreLoad
    Begin Load
    End Load
    Begin ProcessPostData Second Try
    End ProcessPostData Second Try
    Begin Raise ChangedEvents
    Raised DropDownList1_SelectedIndexChanged // DropDownList event here
    

    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 the Button1_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 the DropDownList will remain with the value you changed before. If, at that stage you click the button, both the DropDownList and the Button are being sent on the request values. As the DropDownList event is raised first, the page get redirected to Google again.

    Update:

    One work arround could be implement the following on the code behind:

    public partial class test : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
    
        }
    
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
    
            if (IsPostBack)
            {
                //If the form is posting the button, it means you clicked it
                bool isButtonPostBackEvent = Request.Form.AllKeys.Contains(Button1.UniqueID);
    
                //Gets the posted value of the DropDownList
                string selectedValue = Request.Form[DropDownList1.UniqueID];
    
                //Retrieves the index of the DropDownList postedValue
                int valueIndex = DropDownList1.Items.IndexOf(DropDownList1.Items.FindByValue(selectedValue));
    
                //Verify if posted value of the dropdownlist is different from the server (will raise the SelectedIndexChangedEvent event)
                bool willRaiseSelectedIndexChangedEvent = DropDownList1.SelectedIndex != valueIndex;
    
                //Verifies if both events will be fired, so apply the button
                //behavior, otherwise let the asp.net do its 
                //magic and raise the events automatically
                if (isButtonPostBackEvent && willRaiseSelectedIndexChangedEvent)
                {
                    RedirectToYahoo();
                }
            }
        }
    
        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            RedirectToGoogle();
        }
    
        protected void Button1_Click1(object sender, EventArgs e)
        {
            RedirectToYahoo();
        }
    
        private void RedirectToGoogle()
        { 
            Response.Redirect("http://www.google.com");
        }
    
        private void RedirectToYahoo()
        { 
            Response.Redirect("http://www.yahoo.com");
        }
    }
    

    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:

    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
    
        if (IsPostBack)
        {
            bool isButtonPostBackEvent = Request.Form.AllKeys.Contains(Button1.UniqueID);
    
            if (isButtonPostBackEvent)
            {
                RedirectToYahoo();
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a dropdownlist on an ASP.NET web form. It is set to autopostback
I will try to keep this as simple as possible. I have a rather
Is there any way to keep this simple JQuery animation from flashing? http://jsfiddle.net/v3DVf/6/
I currently have an ASP.NET MVC application that exists. I want to add a
To keep this simple, lets say I have two tables. The first is called
I have 31 separate tables (actually I have 365, but lets keep this simple)
I will try to keep this as simple as possible. Basically I have a
I'll try and keep this simple, I have worked on projects in the past
Ill keep this simple, I have a mediaelementcontrol playing full screen using the following
I have a simple wizard generated query in a LinqDataSource like this: <asp:LinqDataSource ID=EvaluationsData

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.