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

Muh*_*tar 60 c# asp.net page-lifecycle

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

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

}
Run Code Online (Sandbox Code Playgroud)

谢谢

J P*_*ack 116

我看到已经有一些很好的建议和方法建议如何获得后期控制.但是我找到了另一个网页(Mahesh博客),其中包含一个检索回发控制ID的方法.

我将在这里发布一些修改,包括使其成为扩展类.希望它以这种方式更有用.

/// <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;
}
Run Code Online (Sandbox Code Playgroud)

更新(2016-07-22):键入检查ButtonImageButton更改为查找IButtonControl以允许识别来自第三方控件的回发.

  • 如果有多个按钮并且导致回发的按钮具有UseSubmitBehavior ="True",则只有此按钮将包含在HttpRequest.Form.AllKeys中.如果UseSubmitBehavior ="False",那么__EVENTTARGET将完成这项工作. (7认同)
  • 如果有多个按钮或图像按钮,这不会失败吗?看起来它只会返回它找到的第一个. (5认同)
  • 谢谢你......完美的重复使用片段. (3认同)
  • 如果导致回发的控件埋在容器中,则将无法使用页面对象上的FindControl方法来查找它,而无需递归搜索子控件。 (2认同)

sil*_*lvo 16

这里有一些代码可能适合你(来自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;
}
Run Code Online (Sandbox Code Playgroud)


Jar*_*dek 10

直接在表单参数或

string controlName = this.Request.Params.Get("__EVENTTARGET");
Run Code Online (Sandbox Code Playgroud)

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

// 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
Run Code Online (Sandbox Code Playgroud)

您还可以遍历所有控件并检查其中一个是否使用上面的代码导致了postBack.


tyr*_*ker 9

如果你需要检查哪个控件导致了回发,那么你可以直接比较["__EVENTTARGET"]你感兴趣的控件:

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

这假设您只是要比较任何GetPostBackControl(...)扩展方法的结果.它可能无法处理每种情况,但如果它有效则更简单.另外,您不会在页面上搜索您不关心的控件.