【问题标题】:How to handle or check User Control postback in Parent page?如何处理或检查父页面中的用户控制回发?
【发布时间】:2015-07-24 23:25:08
【问题描述】:

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

【问题讨论】:

    标签: asp.net webforms


    【解决方案1】:

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

    但是,您可以在每个 Event 内部检查回发是由 Parent 还是由 User Control 触发的。

    如果你想检查 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)
            {
    
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-30
      • 2019-02-08
      • 1970-01-01
      • 1970-01-01
      • 2012-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多