我试图通过扩展现有控件来避免复合控件或使用ASCX.但是,我无法将控件添加到继承的控件中并保持其视图状态/后置完整性.每当我在预渲染期间添加控件时,控件都会显示,但是post-back会抛
实际上,microsoft说你应该覆盖 CreateChildControls方法.The exception is
Sys.WebForms.PageRequestManagerServerErrorException:
Failed to load viewsstate. The control
tree into which viewstate is being
loaded must match the control tree
that was used to save viewstate during
the previous request. For example,
when adding controls dynamically, the
controls added during a post-back must
match the type and position of the
controls added during the initial
request
你可以在添加控件之前或之后调用基类方法,我不确定那里是否有约定.
protected override void CreateChildControls(){ Controls.Add(someControl); base.CreateChildControls(); }
希望有所帮助!