【问题标题】:ASP.Net Page Not Posting Back - Reloading InsteadASP.Net 页面未回发 - 而是重新加载
【发布时间】:2012-01-16 05:22:06
【问题描述】:

我有一个简单的 ASP.Net 登录页面。单击登录按钮时,页面应该回发,并且应该由我的服务器端事件处理程序处理。相反,页面只是重新加载。 Page.IsPostBackfalse

我在母版页、ASPX 页面和 UserControl (ascx) 的 Page_Load/Init(如果适用)处理程序中放置了断点。当我点击 Login 按钮时,我并没有返回一个帖子并调用我的事件处理程序,而是像一个新的请求一样简单地加载页面。

但这还没有结束!登录页面采用单个查询字符串参数:Login.aspx?id=123456。使用该参数时会出现上述故障。但是,如果我启用URL 重写 以进行查询Login/123456,则错误不会 发生;我收到了回复,在这种情况下调用了我的事件处理程序。

那么为什么我的页面没有得到预期的行为?改写后的 URL 怎么解决问题?

登录按钮声明在LoginUserControl.ascx:

<asp:Button ID="SubmitLinkButton" runat="server" Text='Log In' OnClick="SubmitLinkButton_Click"></asp:Button>

以及后面代码中的处理程序:

protected void SubmitLinkButton_Click(object sender, EventArgs e)
{
    Authenticate();
}

SubmitLinkButton_Click 永远不会被调用。 :(

编辑(更多代码):

//Page_Init on the Master page
protected void Page_Init(object sender, EventArgs e)
{

    if (HttpContext.Current.User.Identity.IsAuthenticated) 
    {
        try
        {

            if (SessionFacade.User != null)
            {
                loginlabel.Text = "Logged in |";
                LoginLink.Visible = true;
            }
        }
        catch
        {
            FormsAuthentication.SignOut();
            CacheFacade.RemoveSessionValues();
            Session.Abandon();
            Session.RemoveAll();
            HttpContext.Current.Response.Redirect("~/Login.aspx");
        }
    }
    else
    {
        loginlabel.Text = "";
        LoginLink.Visible = false;
    }
}

Page_Load 在 ASPX 页面上:

protected void Page_Load(object sender, EventArgs e)
{
    if (this.IsPostBack.Equals(false))
    {
        /* Some business stuff that boils down to this: */
        Session["company"] = Request["company"];
    }
}

Page_Load 在登录控件上:

protected void Page_Load(object sender, EventArgs e)
{

    if (!Page.IsPostBack)
    {
        //honestly, there's no code here
    }

}

P.S:我需要保持非 url-rewrite 方式访问登录页面,因为很多用户还在导航到那个 URL。

P.P.S:即使不启用 URL 重写,错误仍然存​​在。

【问题讨论】:

  • 您没有运行任何 onClientClick 脚本?
  • 不!作为快速更新,我跟踪了 POST 请求和响应。我客户的帖子看起来不错。作为响应,服务器只是重新发送登录页面,这与我看到的服务器端行为一致。
  • 你能告诉我们完整的代码吗?
  • 关于 /* Some business stuff that boils down to this: */ Session["company"] = Request["company"]; 我希望这是伪代码,因为信任来自 HTTP 请求的原始输入是一个危险的安全问题。
  • 所包含的代码不包含我所知道的错误。您的错误可能是由于您的重写、web.config、IIS 设置和/或导致问题的其他交互。我们需要更多代码和配置设置才能为您提供帮助。

标签: asp.net url-rewriting postback


【解决方案1】:

每当我过去看到这个问题时,它通常被归咎于 URL 重写。我通常会联系Fiddler 来跟踪 HTTP 活动。我的直觉告诉我,在您单击按钮并看到 POST 请求后,它会很快被 302 重定向到登录页面。

您注意到login.aspx页面的“使用此参数时出现上述故障”。你确定没有 url-rewriter 配置,例如,可以去除任何查询字符串值并进行重定向吗?

【讨论】:

    【解决方案2】:

    尝试检查您的缓存策略。可能你的请求被缓存了

    【讨论】:

      【解决方案3】:

      我清除了 IE 浏览器的历史记录,问题就消失了。

      【讨论】:

        【解决方案4】:

        您是否尝试使用其他浏览器?有一次我遇到了类似的问题,通过重新安装 Firefox 解决了!

        【讨论】:

          猜你喜欢
          • 2013-01-24
          • 1970-01-01
          • 2014-02-16
          • 2014-11-21
          • 1970-01-01
          • 2011-07-07
          • 2015-01-21
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多