代码之家  ›  专栏  ›  技术社区  ›  Muhammad Akhtar

在回发时,如何检查在页面初始化事件中引起回发的控件

  •  59
  • Muhammad Akhtar  · 技术社区  · 16 年前

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

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

    谢谢

    8 回复  |  直到 9 年前
        1
  •  114
  •   Marc Lopez    10 年前

    我看到已经有一些很好的建议和方法建议如何得到回岗控制。但是我找到了另一个网页( 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): 类型检查 Button ImageButton 改为寻找 IButtonControl 允许识别来自第三方控件的回发。

        2
  •  16
  •   silvo    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;
    }
    
        3
  •  10
  •   Jaroslav Jandek    16 年前

    直接以表单参数或

    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
    

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

        4
  •  9
  •   tyriker    13 年前

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

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

    这假设你只是在比较 GetPostBackControl(...) 无论如何,扩展方法。它可能不能处理所有的情况,但如果它起作用的话,就简单了。另外,您也不会在页面上搜索您不关心的控件。

        5
  •  4
  •   Eduardo    12 年前
    if (Request.Params["__EVENTTARGET"] != null)
    {
      if (Request.Params["__EVENTTARGET"].ToString().Contains("myControlID"))
      {
        DoWhateverYouWant();
      }
    }
    
        6
  •  3
  •   DOK    16 年前

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

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

        7
  •  2
  •   dwitvliet Javier Cañon    12 年前

    对先前答案的补充,用于 Request.Params["__EVENTTARGET"] 您必须设置选项:

    buttonName.UseSubmitBehavior = false;
    
        8
  •  1
  •   Nuno Ribeiro    11 年前

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

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