我有一个复合控件,如:
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);
}
}
您可以在CreateChildControls
中评估该标志,而不是更改子控件的可见性,例如:
OnPreRender
此外,您应该将protected override void CreateChildControls()
{
Controls.Add(_control1);
Controls.Add(_control2);
}
protected override void OnPreRender(EventArgs e)
{
_control1.Visible = !RenderControl2;
_control2.Visible = RenderControl2;
}
的值保存在控制状态,如RenderControl2
所述。这样它将在回发中持续存在。