【问题标题】:Page_Load not firing after Response.Redirect in ASP.NETPage_Load 在 ASP.NET 中的 Response.Redirect 后未触发
【发布时间】:2011-12-22 21:58:31
【问题描述】:

在 asp.net 应用程序中,我有一个表单,在用户单击按钮后,我使用 Response.Redirect 转到另一个页面。但是在第二页中,Page_Load 事件没有触发。我尝试设置 AutoEventWireup="false。我还尝试清除浏览器的缓存,最后我尝试使用 this.Load 事件处理程序,但结果相同。感谢任何帮助。

提前致谢,圣诞快乐!

/添加代码/

protected void btnSubmit_Click(object sender, EventArgs e)
    {
        CreateXML();
        PostToWebApplicationB();
        Response.Redirect(url);
    }

/这是 PostToWebApplicationB 方法/

private void PostToWebApplicationB()
    {
        try
        {
            request = WebRequest.Create(url);
            request.Method = "POST";
            request.ContentType = "text/xml";

            StreamWriter writer = new StreamWriter(request.GetRequestStream());
            writer.WriteLine(this.GetTextFromXmlFile(filepath));
            writer.Close();
            response = request.GetResponse();
        }
        catch (Exception ex)
        {
            errorLabel.Text = ex.Message;
        }
        finally
        {
            if (request != null)
            {
                request.GetRequestStream().Close();
            }
            if (response != null)
            {
                response.GetResponseStream().Close();
            }
        }
    }

/目标页面指令/

<%@ Page Language="C#" AutoEventWireup="false" CodeBehind="Intermediate.aspx.cs"
Inherits="WebApplicationB.Intermediate" %>

【问题讨论】:

  • 你能告诉我们你重定向的代码和目标页面的directive吗?

标签: asp.net pageload


【解决方案1】:

设置 AutoEventWireup="true" 并确保页面上有与Page_Load(object sender, EventArgs e) 签名匹配的方法

-或-

保留 AutoEventWireup="false",但确保您随后手动将 OnLoad 事件绑定到方法。

C# 示例:

override protected void OnInit(EventArgs e)
{
    this.Load += new System.EventHandler(this.Page_Load);
}

【讨论】:

  • 在 aspx 文件中我有 AutoEventWireup="true",当然在 aspx.cs 中有 Page_Load 处理程序。处理程序仍然没有触发。正如您在上面的代码中看到的,我在稍后重定向的页面中创建了 i web 请求。您认为这会影响不触发 Page_Load 事件吗?
  • 使用 fiddler 检查您的 http 流量。看看你是否确实收到了 302 响应等。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-17
相关资源
最近更新 更多