如何确定回发是否由UserControl引起

时间:2013-10-07 21:46:00

标签: c# asp.net user-controls

我在帖子Which Control Caused the Postback中尝试了这个建议,但它在我的情况下不起作用。控件名称以UserControl名称为前缀。有没有办法在UserControl上获取对动态添加控件的引用?

3 个答案:

答案 0 :(得分:1)

试试这个:

if (IsPostBack)
{ 
  var targetID = Request.Form["__EVENTTARGET"];
  if (!String.IsNulOrEmpty(targetID)
  {
      var targetControl = this.Page.FindControl(targetID);
  }
}

答案 1 :(得分:0)

您可以让您的用户控件公开events。您的aspx页面代码后面应该有一个方法可以在一个这样的事件上调用。

public delegate void EventFiredHandler(object sender);

public class MyUserControl: UserControl
{
    public event EventFiredHandler EventFired;

    //Let all your button clicks in usercontrol share this event sink
    void Button1_Click(object sender, EventArgs e)
    {
        if(EventFired != null)
        {
            EventFired(sender);
        }
    }
}

在你的aspx标记中你写了类似的东西:

<uc1:MyUserControl runat="server" 
    EventFired="UCControl_EventFired"></uc1:MyUserControl>

在后面的aspx代码中:

protected void UCControl_EventFired(object sender)
{
    //Obtaining reference of control in usercontrol 
    // which caused event
    Button btn = (Button) sender;
}

答案 2 :(得分:0)

到目前为止发布的答案都没有。这个是。我在页面加载事件中使用这些函数来检查按钮点击等,并且根本不使用控件事件,因为它们发生得太晚了,我必须将数据绑定两次。

    // Returns true if postback is from current control or one of child controls of current control.
    // Returns false if postback is from any other controls (siblings or parents of the siblings) or if there is no postback.
    public bool IsPostBackFromCurrentControl()
    {
        return IsPostBackFromControlOrItsChildren(this);
    }

    // Returns true if postback is from the control or one of child controls of the control.
    // Returns false if postback is from any other controls (siblings, parents of the siblings or parent of current control) or if there is no postback.
    public bool IsPostBackFromControlOrItsChildren(Control ctl)
    {
        if (IsPostBack)
        {
            string eventTarget = Request["__EVENTTARGET"];
            if (eventTarget.StartsWith(ctl.UniqueID) || eventTarget.StartsWith(ctl.ClientID))
                return true;
            else
                return false;
        }
        return false;
    }

    // Returns true if postback is from the control.
    // Returns false if postback is from any other controls (siblings, parents of the siblings or parent or child of current control) or if there is no postback.
    public bool IsPostBackFromControl(Control ctl)
    {
        if (IsPostBack)
        {
            string eventTarget = Request["__EVENTTARGET"];
            if (eventTarget == ctl.UniqueID || eventTarget == ctl.ClientID)
                return true;
            else
                return false;
        }
        return false;
    }

使用此辅助函数,查看如何读取原始回发数据:

    public void ShowAllPostBackData()
    {
        if (IsPostBack)
        {
            string[] keys = Request.Form.AllKeys;
            Literal ctlAllPostbackData = new Literal();
            ctlAllPostbackData.Text = "<div class='well well-lg' style='border:1px solid black;z-index:99999;position:absolute;'><h3>All postback data:</h3><br />";
            for (int i = 0; i < keys.Length; i++)
            {
                ctlAllPostbackData.Text += "<b>" + keys[i] + "</b>: " + Request[keys[i]] + "<br />";
            }
            ctlAllPostbackData.Text += "</div>";
            this.Controls.Add(ctlAllPostbackData);
        }
    }