在回发时,如何检查Page_Init事件中哪个控件导致回发

时间:2021-10-19 00:05:50

On postback, how can I check which control cause postback in Page_Init event.

在回发时,如何检查Page_Init事件中哪个控件导致回发。

protected void Page_Init(object sender, EventArgs e)
{
//need to check here which control cause postback?

}

Thanks

谢谢

8 个解决方案

#1


106  

I see that there is already some great advice and methods suggest for how to get the post back control. However I found another web page (Mahesh blog) with a method to retrieve post back control ID.

我发现已经有一些很好的建议和方法可以帮助你重新获得职位控制。然而,我发现了另一个网页(Mahesh blog),它具有检索post - back control ID的方法。

I will post it here with a little modification, including making it an extension class. Hopefully it is more useful in that way.

我将在这里发布一些修改,包括将其作为扩展类。希望这样更有用。

/// <summary>
/// Gets the ID of the post back control.
/// 
/// See: http://geekswithblogs.net/mahesh/archive/2006/06/27/83264.aspx
/// </summary>
/// <param name = "page">The page.</param>
/// <returns></returns>
public static string GetPostBackControlId(this Page page)
{
    if (!page.IsPostBack)
        return string.Empty;

    Control control = null;
    // first we will check the "__EVENTTARGET" because if post back made by the controls
    // which used "_doPostBack" function also available in Request.Form collection.
    string controlName = page.Request.Params["__EVENTTARGET"];
    if (!String.IsNullOrEmpty(controlName))
    {
        control = page.FindControl(controlName);
    }
    else
    {
        // if __EVENTTARGET is null, the control is a button type and we need to
        // iterate over the form collection to find it

        // ReSharper disable TooWideLocalVariableScope
        string controlId;
        Control foundControl;
        // ReSharper restore TooWideLocalVariableScope

        foreach (string ctl in page.Request.Form)
        {
            // handle ImageButton they having an additional "quasi-property" 
            // in their Id which identifies mouse x and y coordinates
            if (ctl.EndsWith(".x") || ctl.EndsWith(".y"))
            {
                controlId = ctl.Substring(0, ctl.Length - 2);
                foundControl = page.FindControl(controlId);
            }
            else
            {
                foundControl = page.FindControl(ctl);
            }

            if (!(foundControl is IButtonControl)) continue;

            control = foundControl;
            break;
        }
    }

    return control == null ? String.Empty : control.ID;
}

Update (2016-07-22): Type check for Button and ImageButton changed to look for IButtonControl to allow postbacks from third party controls to be recognized.

更新(2016-07-22):按钮和ImageButton的类型检查更改为查找IButtonControl,以允许识别来自第三方控件的回发。

#2


15  

Here's some code that might do the trick for you (taken from Ryan Farley's blog)

这里有一些代码可能对您有用(取自Ryan Farley的博客)

public static Control GetPostBackControl(Page page)
{
    Control control = null;

    string ctrlname = page.Request.Params.Get("__EVENTTARGET");
    if (ctrlname != null && ctrlname != string.Empty)
    {
        control = page.FindControl(ctrlname);
    }
    else
    {
        foreach (string ctl in page.Request.Form)
        {
            Control c = page.FindControl(ctl);
            if (c is System.Web.UI.WebControls.Button)
            {
                control = c;
                break;
            }
        }
    }
    return control;
}

#3


10  

Either directly in form parameters or

直接的形式参数或

string controlName = this.Request.Params.Get("__EVENTTARGET");

Edit: To check if a control caused a postback (manually):

编辑:检查控件是否导致回发(手动):

// input Image with name="imageName"
if (this.Request["imageName"+".x"] != null) ...;//caused postBack

// Other input with name="name"
if (this.Request["name"] != null) ...;//caused postBack

You could also iterate through all the controls and check if one of them caused a postBack using the above code.

您还可以遍历所有控件,并检查其中一个是否使用上述代码导致回发。

#4


9  

If you need to check which control caused the postback, then you could just directly compare ["__EVENTTARGET"] to the control you are interested in:

如果您需要检查是哪个控件导致了回发,那么您可以直接将[“__EVENTTARGET”]与您感兴趣的控件进行比较:

if (specialControl.UniqueID == Page.Request.Params["__EVENTTARGET"])
{
    /*do special stuff*/
}

This assumes you're just going to be comparing the result from any GetPostBackControl(...) extension method anyway. It may not handle EVERY situation, but if it works it is simpler. Plus, you won't scour the page looking for a control you didn't care about to begin with.

这假定您只是在比较任何GetPostBackControl(…)扩展方法的结果。它可能不能处理所有的情况,但如果它有效,它就会更简单。另外,您不会在页面中搜索一开始不关心的控件。

#5


4  

if (Request.Params["__EVENTTARGET"] != null)
{
  if (Request.Params["__EVENTTARGET"].ToString().Contains("myControlID"))
  {
    DoWhateverYouWant();
  }
}

#6


3  

Assuming it's a server control, you can use Request["ButtonName"]

假设它是一个服务器控件,您可以使用Request["ButtonName"]

To see if a specific button was clicked: if (Request["ButtonName"] != null)

查看是否单击了特定的按钮:if(请求["ButtonName"] != null)

#7


1  

An addition to previous answers, to use Request.Params["__EVENTTARGET"] you have to set the option:

除了前面的答案之外,还要使用请求。Params["__EVENTTARGET"]你必须设置选项:

buttonName.UseSubmitBehavior = false;

#8


1  

To get exact name of control, use:

要获得确切的控制名,请使用:

    string controlName = Page.FindControl(Page.Request.Params["__EVENTTARGET"]).ID;

#1


106  

I see that there is already some great advice and methods suggest for how to get the post back control. However I found another web page (Mahesh blog) with a method to retrieve post back control ID.

我发现已经有一些很好的建议和方法可以帮助你重新获得职位控制。然而,我发现了另一个网页(Mahesh blog),它具有检索post - back control ID的方法。

I will post it here with a little modification, including making it an extension class. Hopefully it is more useful in that way.

我将在这里发布一些修改,包括将其作为扩展类。希望这样更有用。

/// <summary>
/// Gets the ID of the post back control.
/// 
/// See: http://geekswithblogs.net/mahesh/archive/2006/06/27/83264.aspx
/// </summary>
/// <param name = "page">The page.</param>
/// <returns></returns>
public static string GetPostBackControlId(this Page page)
{
    if (!page.IsPostBack)
        return string.Empty;

    Control control = null;
    // first we will check the "__EVENTTARGET" because if post back made by the controls
    // which used "_doPostBack" function also available in Request.Form collection.
    string controlName = page.Request.Params["__EVENTTARGET"];
    if (!String.IsNullOrEmpty(controlName))
    {
        control = page.FindControl(controlName);
    }
    else
    {
        // if __EVENTTARGET is null, the control is a button type and we need to
        // iterate over the form collection to find it

        // ReSharper disable TooWideLocalVariableScope
        string controlId;
        Control foundControl;
        // ReSharper restore TooWideLocalVariableScope

        foreach (string ctl in page.Request.Form)
        {
            // handle ImageButton they having an additional "quasi-property" 
            // in their Id which identifies mouse x and y coordinates
            if (ctl.EndsWith(".x") || ctl.EndsWith(".y"))
            {
                controlId = ctl.Substring(0, ctl.Length - 2);
                foundControl = page.FindControl(controlId);
            }
            else
            {
                foundControl = page.FindControl(ctl);
            }

            if (!(foundControl is IButtonControl)) continue;

            control = foundControl;
            break;
        }
    }

    return control == null ? String.Empty : control.ID;
}

Update (2016-07-22): Type check for Button and ImageButton changed to look for IButtonControl to allow postbacks from third party controls to be recognized.

更新(2016-07-22):按钮和ImageButton的类型检查更改为查找IButtonControl,以允许识别来自第三方控件的回发。

#2


15  

Here's some code that might do the trick for you (taken from Ryan Farley's blog)

这里有一些代码可能对您有用(取自Ryan Farley的博客)

public static Control GetPostBackControl(Page page)
{
    Control control = null;

    string ctrlname = page.Request.Params.Get("__EVENTTARGET");
    if (ctrlname != null && ctrlname != string.Empty)
    {
        control = page.FindControl(ctrlname);
    }
    else
    {
        foreach (string ctl in page.Request.Form)
        {
            Control c = page.FindControl(ctl);
            if (c is System.Web.UI.WebControls.Button)
            {
                control = c;
                break;
            }
        }
    }
    return control;
}

#3


10  

Either directly in form parameters or

直接的形式参数或

string controlName = this.Request.Params.Get("__EVENTTARGET");

Edit: To check if a control caused a postback (manually):

编辑:检查控件是否导致回发(手动):

// input Image with name="imageName"
if (this.Request["imageName"+".x"] != null) ...;//caused postBack

// Other input with name="name"
if (this.Request["name"] != null) ...;//caused postBack

You could also iterate through all the controls and check if one of them caused a postBack using the above code.

您还可以遍历所有控件,并检查其中一个是否使用上述代码导致回发。

#4


9  

If you need to check which control caused the postback, then you could just directly compare ["__EVENTTARGET"] to the control you are interested in:

如果您需要检查是哪个控件导致了回发,那么您可以直接将[“__EVENTTARGET”]与您感兴趣的控件进行比较:

if (specialControl.UniqueID == Page.Request.Params["__EVENTTARGET"])
{
    /*do special stuff*/
}

This assumes you're just going to be comparing the result from any GetPostBackControl(...) extension method anyway. It may not handle EVERY situation, but if it works it is simpler. Plus, you won't scour the page looking for a control you didn't care about to begin with.

这假定您只是在比较任何GetPostBackControl(…)扩展方法的结果。它可能不能处理所有的情况,但如果它有效,它就会更简单。另外,您不会在页面中搜索一开始不关心的控件。

#5


4  

if (Request.Params["__EVENTTARGET"] != null)
{
  if (Request.Params["__EVENTTARGET"].ToString().Contains("myControlID"))
  {
    DoWhateverYouWant();
  }
}

#6


3  

Assuming it's a server control, you can use Request["ButtonName"]

假设它是一个服务器控件,您可以使用Request["ButtonName"]

To see if a specific button was clicked: if (Request["ButtonName"] != null)

查看是否单击了特定的按钮:if(请求["ButtonName"] != null)

#7


1  

An addition to previous answers, to use Request.Params["__EVENTTARGET"] you have to set the option:

除了前面的答案之外,还要使用请求。Params["__EVENTTARGET"]你必须设置选项:

buttonName.UseSubmitBehavior = false;

#8


1  

To get exact name of control, use:

要获得确切的控制名,请使用:

    string controlName = Page.FindControl(Page.Request.Params["__EVENTTARGET"]).ID;