【发布时间】: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