未触发ASp.Net链接按钮单击事件,

时间:2013-05-08 11:30:33

标签: c# asp.net

这是一个非常奇怪的场景,

假设我以这种方式浏览我的网站“http://web.site.com”。我的网站完美显示我的主页,在此页面顶部,我使用一个用户控件,此用户控件显示注销链接按钮。

案例1: 当我在这个场景中单击此按钮时,它不会被触发,

案例2: 但是,如果我打开我的网站,如“http://web.site.com/default.aspx”,那么它的工作正常并解雇。

任何人都可以建议我吗?

以下控件正在用户控件中使用

<asp:linkbutton id="logoutLinkButton" runat="server" 
         onclick="logoutLinkButton_Click1">logout</asp:linkbutton>

,链接按钮事件代码为

protected void logoutLinkButton_Click1(object sender, EventArgs e)
         {
             var url = this.Request.RawUrl;
             Authentication.Logout();
             Response.Redirect(url); 
         }

2 个答案:

答案 0 :(得分:1)

问题是您使用的是Request.RawUrl

  

原始URL定义为域后面的URL部分   信息。在URL字符串中   http://www.contoso.com/articles/recent.aspx,原始网址是   /articles/recent.aspx。原始URL包括查询字符串if   本。

因此,对于您的案例"http://web.site.com" RawUrl将为空,因此无法执行任何操作。相反,您可以使用Request.Url.GetLeftPart(UriPartial.Authority)之类的

protected void logoutLinkButton_Click1(object sender, EventArgs e)
{
   var url = this.Request.Url.GetLeftPart(UriPartial.Authority);
   Authentication.Logout();
   Response.Redirect(url); 
}

答案 1 :(得分:0)

尝试Request.URL' instead of Request.RawUrl`