开发者

How to check if the MasterPage logout button is clicked in the content page

开发者 https://www.devze.com 2023-03-24 00:39 出处:网络
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 s

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.


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.


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;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消