ASP.NET的AutoPostBack然后后退按钮出现怪异 [英] ASP.NET AutoPostBack and then Back Button weird occurrence

查看:108
本文介绍了ASP.NET的AutoPostBack然后后退按钮出现怪异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要保持这个简单,我有一个DropDownList,并在ASP.NET表单按钮。 DropDownList中有调用DropDownList1_SelectedIndexChanged的自动回功能和页面重定向的地方(在这个例子中www.google.com)和按钮有去Button1_Click1一个onclick和页面被重定向到www.yahoo.com。

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.

问题:如果我按一下按钮,我去雅虎,这是你所期望的东西。如果我在浏览器中单击后退按钮并选择DropDownList的我去谷歌这也是正确的,但如果我点击后退按钮,然后单击该按钮我重定向到谷歌。呃?为什么它不进入雅虎?

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?

下面是我的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背后:

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");
    }
}

如果有人可以帮助我,这将是最AP preciated。

If anyone could help me, it would be most appreciated.

推荐答案

好了,一些挖后,我发现以下内容:

Well, after some digging, i found the following:

当我们点击按钮,页面生命周期,直到 Button1_Click1 引发事件发生这样的:

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

现在,当我们改变的DropDownList ,页面生命周期,直到 DropDownList1_SelectedIndexChanged 引发事件发生这样的

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两个页面的生命周期中,我们看到 DropDownList1_SelectedIndexChanged 事件上的PageChangedEvents程序提出,这种方法恰好比页'PostBackEvent程序早些时候提高了 Button1_Click1 事件。

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.

现在,当您更改的DropDownList 的SelectedIndex会被重定向到谷歌。当你点击后退按钮,浏览器检索页面的最后一个状态,这意味着的DropDownList 将继续留在你之前更改了该值。如果,在这个阶段,你按一下按钮,无论是的DropDownList 和巴顿正在对请求发送的值。由于的DropDownList 事件被首次提出,页面重定向到谷歌试。

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.

更新:

一个角落找寻工作可能实施的背后code以下内容:

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");
    }
}

在OnInit的情况下,code标识将由asp.net将引发的事件。当两个事件是present,我们应用按钮点击行为,因为它在这种情况下,优先级(它被点击)。

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();
        }
    }
}

这篇关于ASP.NET的AutoPostBack然后后退按钮出现怪异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆