现在我的问题是,每当用户按下menuitem或treeitem时,它会将wuc加载到链接到该对象的maincontent.
但是我有一些观点错误,我现在已经四处寻找,并且所解释的解决方案都没有适用于我的项目.
我所有的wuc都必须启用viewstate.
周期是 – >
页面(控件A)使用变量进行回发以将控件更改为wucPanel(UpdatePanel)中的ControlB.
OnLoad LoadRequested Wuc.
目前的代码是
protected void Load_Page(object sender,EventArgs e) { //Code to decide which wuc to load. UserControl wucc = (UserControl)Page.LoadControl(sFilePath); ParentControl.ContentTemplateContainer.Controls.Add(wucc); }
我已经尝试了几个修复,比如向wuc添加不同的id,但这要么像处理程序一样摒弃控制的内部功能,要么生成相同的viewstate错误.
我找到的一个解决方案是加载ControlA然后只删除它然后加载ControlB.但这会禁用我的第三方控制器(Telerik)的脚本.
我也读过关于每种类型的不同PlaceHolders,但由于我希望有多达50种不同的控件,我觉得这不会对我有所帮助.
从Page_Load移动 – > Page_Init生成了相同的错误.
错误:
Failed to load viewstate. 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.
解决方法
Control _oldControl = null; protected void Init_Page(object sender,EventArgs e) { //Code to decide which wuc to load. UserControl wucc = (UserControl)Page.LoadControl(sFilePath); ParentControl.ContentTemplateContainer.Controls.Add(wucc); _oldControl = wucc as Control; //Now add the new control here. } //override the LoadViewState method and remove the control from the control's collection once you page's viewstate has been loaded protected override void LoadViewState(object savedState) { base.LoadViewState(savedState); ParentControl.ContentTemplateContainer.Controls.Remove(_oldControl); }
希望这可以帮助.如果确实如此,请选中此答案旁边的复选框以接受它,如果您愿意,请将其投票:)