【问题标题】:Changing inner control as result of a event由于事件而改变内部控制
【发布时间】:2019-07-13 21:33:33
【问题描述】:

我有一个复合控件,例如:

class MyControl : CompositeControl {
   private Control _control1;
   private Control _control2;

   public bool RenderControl2 { get; set; }

   /* Constructor to initialize controls*/

    protected override void CreateChildControls(){
       if(RenderControl2){
           Controls.Add(_control2);
       }else{
           Controls.Add(_control1);
       }           
    }        
}

这适用于在 Page_Init() 期间设置 RenderControl2 的值的情况。

 protected void Page_Init(object sender, EventArgs e){
    if (!Page.IsPostBack){
       myControl.RenderControl2 = MyMagicFucntion();
    }
    /* Works also when in Postback, but not required since the control keeps it state and only need to change state in the scenario below.*/
}

但是,现在我们想将值设置为事件的结果

protected void otherDropDow_SelectedIndexChanged(object sender, EventArgs e) {
    myControl.RenderControl2 = otherDropDown.SelectedValue == "My Magic String";
}

这不起作用,因为在事件触发时控件已经执行了 CreateChildControls。 (嗯,它在下一次回发期间确实有效...... :()

我已尝试将逻辑移至控件的 OnDataBinding 事件。但这似乎对控件在页面上的实际显示方式没有影响。

/* DOES NOT RESOLVE THE ISSUE */
protected override void OnDataBinding(EventArgs e){  
   base.OnDataBinding(e);
   /* _renderControl2HasChanged is set when RenderControl2 changes value          
   */
   if(_renderControl2HasChanged)
      if(RenderControl2){
          Controls.Remove(_control1);
          Controls.Add(_control2);
      }else{
          Controls.Remove(_control2);
          Controls.Add(_control1);
      }
   }      

【问题讨论】:

    标签: c# asp.net data-binding controls page-lifecycle


    【解决方案1】:

    您可以评估OnPreRender 中的标志并仅更改子控件的可见性,而不是决定在CreateChildControls 中显示哪个控件,例如:

    protected override void CreateChildControls()
    {
       Controls.Add(_control1);
       Controls.Add(_control2);
    }     
    
    protected override void OnPreRender(EventArgs e)
    {
       _control1.Visible = !RenderControl2;
       _control2.Visible = RenderControl2;
    }
    

    此外,您应该将RenderControl2 的值保存在控制状态中,如there 所述。这样,它将在回发中持续存在。

    【讨论】:

      猜你喜欢
      • 2011-11-21
      • 1970-01-01
      • 2013-02-28
      • 2020-12-11
      • 2016-04-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多