ASP.Net 页面未回发 - 而是重新加载
我有一个简单的 ASP.Net 登录页面。单击登录按钮时,页面应该回发,并且事件应该由我的服务器端事件处理程序处理。相反,页面只是重新加载。 Page.IsPostBack
为false
。
我已在 Master 页、ASPX 页和 UserControl (ascx) 的 Page_Load/Init(如果适用)处理程序中放置了断点。当我点击“登录”按钮时,我没有返回帖子并调用事件处理程序,而是简单地加载页面,就好像它是一个新请求一样。
但这还没有结束!登录页面采用单个查询字符串参数: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;
}
}
ASPX 页面上的 Page_Load:
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
}
}
PS:我需要保留访问登录页面的非 URL 重写方式,因为许多用户仍在导航到该 URL:
即使未启用 URL 重写,错误仍然会发生。
I have a simple ASP.Net login page. When the login button is clicked, the page should post back and the even should be handled by my server-side event handler. Instead, the page simply reloads. Page.IsPostBack
is false
.
I've put breakpoints in the Page_Load/Init (where applicable) handlers of the Master page, the ASPX page and the UserControl (ascx). When I hit the Login button, instead of getting a post back and having my event handler called, I simply get the page load as if it was a fresh request.
But that's not the end of it! The login page takes a single query string parameter: Login.aspx?id=123456
. The above failure occurs when using this parameter. However, if I enable URL Rewriting in order to make the query Login/123456
, the error does not occur; I get a post back and my event handler is called in this instance.
So why am I not getting the expected behaviour from my page? What about the rewritten URL is making the problem go away?
Login Button is declared in LoginUserControl.ascx
:
<asp:Button ID="SubmitLinkButton" runat="server" Text='Log In' OnClick="SubmitLinkButton_Click"></asp:Button>
And the handler in the code behind:
protected void SubmitLinkButton_Click(object sender, EventArgs e)
{
Authenticate();
}
SubmitLinkButton_Click
is never called. :(
Edit (more code):
//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 on the ASPX page:
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 on the Login Control:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//honestly, there's no code here
}
}
P.S: I need to keep the non-url-rewrite way of accessing the login page, because many users are still navigating to that URL.
P.P.S: Even if URL rewriting isn't enabled, the error still occurs.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
过去每当我看到这个问题时,通常都会将其归因于 URL 重写。我通常会使用 Fiddler 来跟踪 HTTP 活动。我的直觉告诉我,在您单击按钮并看到 POST 请求后,很快就会出现 302 重定向到登录页面的情况。
您注意到login.aspx页面的“使用此参数时出现上述故障”。您确定没有 url 重写器配置,例如,可能会删除任何查询字符串值并执行重定向?
Whenever I've seen this problem in the past it has usually been attributed to URL re-writing. I would usually reach for Fiddler to trace the HTTP activity. My hunch tells me after you click the button and see the POST request it will quickly be followed up by a 302 redirect to the login page.
You note that "The above failure occurs when using this parameter" of the login.aspx page. Are you certain there is no url-rewriter configuration that for example, may strip out any querystring values and do a redirect?
尝试检查您的缓存策略。可能您的请求已被缓存
Try to check your caching policy. Possibly your request is cached
我清除了 IE 浏览器的历史记录,问题就消失了。
I cleared the history on my IE browser and the problem went away.
您是否尝试过使用其他浏览器?我曾经遇到过类似的问题,重新安装Firefox就解决了!
Did you try to use a different browser? Once I had a similar problem, and it solved by re-installing Firefox!