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

时间:2010-07-04 17:14:38

标签: c# asp.net page-lifecycle

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

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

}

由于

8 个答案:

答案 0 :(得分:114)

我看到已经有一些很好的建议和方法建议如何获得后期控制。但是我找到了另一个网页(Mahesh blog),其中包含一个检索回发控制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;
}

更新(2016-07-22):类型检查ButtonImageButton已更改为查找IButtonControl以允许从第三方控件进行回发被承认。

答案 1 :(得分:16)

这里有一些代码可能适合你(取自Ryan Farley's blog

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

答案 2 :(得分:10)

直接在表格参数或

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

修改:检查控件是否导致回发(手动):

// 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

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

答案 3 :(得分:9)

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

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

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

答案 4 :(得分:4)

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

答案 5 :(得分:3)

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

查看是否点击了某个特定按钮:if (Request["ButtonName"] != null)

答案 6 :(得分:2)

除了之前的答案,要使用Request.Params["__EVENTTARGET"],您必须设置选项:

buttonName.UseSubmitBehavior = false;

答案 7 :(得分:1)

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

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