可能看起来像一个愚蠢的标题,因为如果你想维护,你不应该在 Page_Init 之后添加动态控件ViewState
,但我想不出更好的方法来解释这个问题。
我有一个类似于以下的类:
public class WebCustomForm : WebControl, IScriptControl
{
internal CustomRender Content
{
get
{
object content = this.Page.Session[this.SESSION_CONTENT_TRACKER];
return content as CustomRender;
}
private set
{
this.Page.Session[this.SESSION_CONTENT_TRACKER] = value;
}
}
}
CustomRender
是一个实现 ITemplate 的抽象类,我用它来自包含我正在编写的 CustomForms 模块。
在包含Page_Init
的页面上WebCustomForm
,我通过将相关 ID 传递给控件来初始化控件。然后在当前活动的控件上调用 Instantiate的重写OnInit
方法:WebCustomForm
CustomRender
if (this.Content != null)
{
this.Content.InstantiateIn(this);
}
问题是我的控件CustomRender
需要能够更改. 但是当在 CustomRender 上触发的事件触发时,Page_Init 事件显然已经消失了。所以,我的问题是,如何从其中动态添加的控件中更改其内容?CustomRender
WebCustomForm
WebCustomForm
在我看来,我有三个选择:
- 我将
CustomRender
控件分离到它们自己的独立控件中,基本上每个控件都有一个 aspx 页面,并自己处理页面上的事件(尽管我希望只是制作一个我放在页面上并忘记的控件) - 我不使用事件,只是继续请求当前页面,但使用不同的请求参数
- 我回到绘图板上,提出任何人都可以给我的更好建议。