如何处理或检查父页面中的用户控件回发?

How to handle or check User Control postback in Parent page?

我的用户控件中有一个复选框。当用户控件的复选框的 CheckedChanged 事件被触发时,如何禁用父控件的事件?

您不能禁用它的事件 ASP.Net 生命周期。

但是,您可以在每个事件中检查回发是由父控件还是用户控件触发的。

如果你想查看Parent页面中哪个控件触发事件 -

public partial class Default : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            string id = Request.Form["__EVENTTARGET"];
            if (!string.IsNullOrWhiteSpace(id) && id.Contains("WebUserControl11"))
            {
            }
        }
    }
}

如果你想检查这个事件是否被我的控件之一触发 UserControl -

public partial class WebUserControl1 : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack) // *** This IsPostBack is not same as Parent's IsPostBack ***
        {

        }
    }

    protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
    {
        if (IsPostBack)
        {

        }
    }
}