如何检查内容页面中是否单击了 MasterPage 注销按钮

发布于 2024-11-27 09:36:26 字数 131 浏览 1 评论 0原文

我有一个要求,我需要检查用户是否单击母版页中的注销按钮并跳过内容页中的方法。因为该方法具有 Response.End ,这会导致响应停止在该方法中方法。因此,用户无法注销应用程序。

我感谢任何帮助。

I have a requirement where I need to check if the user clicks the logout button in the Master page and skip a method in content page.Because that method has Response.End which causes the Response to stop in that method. Due to this the user is not getting logout of the application.

I Appreciate any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

2024-12-04 09:36:26

我认为解决您的问题的一个非常简单的解决方案是,当用户单击注销按钮时,您将他重定向到注销页面,该页面处理页面加载中的注销并再次重定向到主页。

I think one of the very simple solution for your problem is when the user clicks logout button you redirect him to a logout page that handles the logout in the page load and redirect to the home page again.

梦中楼上月下 2024-12-04 09:36:26

假设您的注销按钮是 html 提交按钮,您可以通过检查请求罐数据来检查。例如,

if (null != Request.Form[YourButton.UniqueID])
{
   // YourButton is clicked
}

您可以在母版页中添加一个公共方法来执行此类检查并在页面中访问该方法。例如,在主代码后面

public partial class YourMasterPage : System.Web.UI.MasterPage
{
   public bool IsLogoutClicked()
   {
     return null != Request.Form[LogoutButton.UniqueID];
   }

   ...
}

然后在您的内容页面中,

if (((YourMasterPage)this.Master).IsLogoutClicked())
{
    ...
}

如果您使用锚点或图像作为注销按钮,那么最好的方法是使用js设置一些隐藏字段,然后检查页面中隐藏字段的值。

编辑:这是一个实用程序方法,用于检查请求数据以确定是否由于某些服务器控件而发生回发(无论控件类型或按钮的 UseSubmitBehavior 属性如何) 。

public const string POST_DATA_EVENT_TARGET = "__EVENTTARGET";
public const string POST_DATA_EVENT_ARGUMENT = "__EVENTARGUMENT";

/// <summary>
/// Returns wheather postback has happened due to the given control or not.
/// </summary>
public static bool IsPostBackDueToControl(Control control)
{
    var postData = HttpContext.Current.Request.Form;
    string postBackControlName = postData[POST_DATA_EVENT_TARGET];
    if (control.UniqueID == postBackControlName)
    {
        // This is control that has caused postback
        return true;
    }
    if (control is Button ||
        control is System.Web.UI.HtmlControls.HtmlInputButton)
    {
        // Check for button control, button name will be present in post data
        if (postData[control.UniqueID] != null)
        {
            return true;
        }
    }
    else if (control is ImageButton ||
        control is System.Web.UI.HtmlControls.HtmlInputImage)
    {
        // Check for image button, name.x & name.y are returned in post data
        if (postData[control.UniqueID + ".x"] != null)
        {
            return true;
        }
    }
    return false;
}

Assuming your logout button is html submit button, you can check that by inspecting request pot data. For example,

if (null != Request.Form[YourButton.UniqueID])
{
   // YourButton is clicked
}

You can add a public method in your master page to do such check and access that in your page. For example, in master code behind

public partial class YourMasterPage : System.Web.UI.MasterPage
{
   public bool IsLogoutClicked()
   {
     return null != Request.Form[LogoutButton.UniqueID];
   }

   ...
}

And then in your content page,

if (((YourMasterPage)this.Master).IsLogoutClicked())
{
    ...
}

In case, you are using anchor or image for logout button then the best way is to set some hidden field using js and then check the value of hidden field in the page.

EDIT: Here's a utility method that checks the request data to decide if post-back has happened due to some server control (regardless of control type or regardless of button's UseSubmitBehavior property).

public const string POST_DATA_EVENT_TARGET = "__EVENTTARGET";
public const string POST_DATA_EVENT_ARGUMENT = "__EVENTARGUMENT";

/// <summary>
/// Returns wheather postback has happened due to the given control or not.
/// </summary>
public static bool IsPostBackDueToControl(Control control)
{
    var postData = HttpContext.Current.Request.Form;
    string postBackControlName = postData[POST_DATA_EVENT_TARGET];
    if (control.UniqueID == postBackControlName)
    {
        // This is control that has caused postback
        return true;
    }
    if (control is Button ||
        control is System.Web.UI.HtmlControls.HtmlInputButton)
    {
        // Check for button control, button name will be present in post data
        if (postData[control.UniqueID] != null)
        {
            return true;
        }
    }
    else if (control is ImageButton ||
        control is System.Web.UI.HtmlControls.HtmlInputImage)
    {
        // Check for image button, name.x & name.y are returned in post data
        if (postData[control.UniqueID + ".x"] != null)
        {
            return true;
        }
    }
    return false;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文