Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何检查内容页中是否单击了母版页注销按钮_C#_Asp.net_Master Pages - Fatal编程技术网

C# 如何检查内容页中是否单击了母版页注销按钮

C# 如何检查内容页中是否单击了母版页注销按钮,c#,asp.net,master-pages,C#,Asp.net,Master Pages,我有一个要求,我需要检查用户是否单击母版页中的注销按钮并跳过内容页中的某个方法。因为该方法具有响应。End,这会导致响应在该方法中停止。因此,用户无法注销应用程序 非常感谢您的帮助。假设您的注销按钮是html提交按钮,您可以通过检查请求pot数据来检查这一点。比如说, if (null != Request.Form[YourButton.UniqueID]) { // YourButton is clicked } 您可以在母版页中添加一个公共方法来执行此类检查并访问页面中的方法。例如

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


非常感谢您的帮助。

假设您的注销按钮是html提交按钮,您可以通过检查请求pot数据来检查这一点。比如说,

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”;
/// 
///返回是否由于给定控件而发生回发。
/// 
公共静态bool因控制(控制)而延迟
{
var postData=HttpContext.Current.Request.Form;
字符串postBackControlName=postData[POST_DATA_EVENT_TARGET];
if(control.UniqueID==postBackControlName)
{
//这是导致回发的控件
返回true;
}
如果(控制按钮)处于关闭状态||
控件是System.Web.UI.HtmlControls.HtmlInputButton)
{
//检查按钮控制,按钮名称将出现在post数据中
if(postData[control.UniqueID]!=null)
{
返回true;
}
}
else if(控件为ImageButton||
控件是System.Web.UI.HtmlControls.HtmlInputImage)
{
//检查图像按钮,在post数据中返回name.x和name.y
if(postData[control.UniqueID+.x”!=null)
{
返回true;
}
}
返回false;
}

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

感谢您的回复。注销按钮是ASP按钮,而不是HTML按钮。ASP.NET按钮始终转换为HTML提交按钮-因此上述按钮适用-注意服务器控件使用了
UniqueID
属性,对于HTML按钮,您需要使用
name
属性。您好,我尝试了IsLogoutClicked()方法。虽然我正在单击注销按钮请求。表单[LogoutButton.UniqueID]始终返回NULL。嗨,VinayC,你能告诉我哪里做错了吗。我遵循与上面相同的程序。但是Request.Form[LogoutButton.UniqueID]总是作为空值出现。@karthik,从诸如fiddler之类的工具中检查请求数据。或者您可以使用VS调试器检查请求。窗体集合可查看是否存在注销按钮值。确保按钮的
UseSubmitBehavior
属性为true。顺便说一句,请参阅我的编辑以了解是否由于某个control.WTF而发生回发。为什么您的代码背后甚至有响应。从结尾开始?
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;
}