在一个页面中,我有一个事件处理程序,它在一个控件上将“可见”设置为 false,在另一个控件上设置为 true。逐步调试,我看到这些值设置正确,并且标记为可见的控件通过 OnPreRender,而我设置为不可见的控件则没有。因此,所有这些似乎都按预期工作。但是,当请求完成时,页面上的可见性根本没有改变。我尝试将直接父 UpdatePanel 设置为“始终”,并尝试手动调用“Update()”,但没有任何效果。关于这里发生了什么的任何线索?
更新:
我发现它只是在我的用户控件上设置了一个私有属性,导致整个事情不起作用。我已经包含了该控件的示例以及它引用私有字段的所有位置。
例子:
protected override void OnLoad(EventArgs e)
{
if (this.IsPostback)
{
return;
}
this.Control1.Visible = true;
this.Control2.Visible = false;
}
protected void OnButtonClicked (object sender, EventArgs e)
{
this.Control1.Visible = false;
this.Control2.Visible = true;
// this has desired results when it fires
}
protected void OnUserControlEventThatFiresAfterRowCommand (object sender, EventArgs e)
{
this.Control2.SomeProp = this.GetSomeObject();
this.Control1.Visible = false;
this.Control2.Visible = true;
// this does not have desired results, even though it does fire
}
然后在 Control2 中:
private SomeClass privatefield;
public SomeClass SomeProp
{
get
{
return this.privatefield;
}
set
{
this.PopulateFields(value);
this.privatefield = value;
// If I comment out this line it works!
}
}
protected override void LoadViewState(object savedState)
{
object[] state = savedState as object[];
base.LoadViewState(state[0]);
this.Enabled = state[1] as bool? ?? true;
this.SomeProp = state[2] as SomeClass;
this.Visible = state[3] as bool? ?? true;
}
protected override object SaveViewState()
{
return new object[]
{
base.SaveViewState(),
this.Enabled,
this.SomeProp,
this.Visible
};
}