2015-11-04 99 views
0

我想动态地将用户控件添加到我的页面。我写了这个代码,每一个DropDownList的变化时,相应的用户控件应该呈现:动态添加用户控件和ViewState

if (!IsPostBack) 
{ 
    BindMainReports(); 
} 
else 
{ 
    PlaceHolderControls.Controls.Clear(); 
    if (drpReportType.SelectedValue != "-" && drpReportType.SelectedValue != "") 
    { 
     Control ctrl = Page.LoadControl("UC/Reports/" + drpReportType.SelectedValue + ".ascx"); 
     PlaceHolderControls.Controls.Add(ctrl); 
    } 
} 

此代码工作正常,第一次,但是当我改变了第二次DropDownList的我得到这个错误:

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.

我该如何解决这个问题。

感谢

+0

http://forums.asp.net/t/1295517.aspx?Failed+to+load+viewstate+The+control+tree+into+which +视图状态是+正在+加载+必须+匹配+的+控制+树+那+是+用+来+保存+视图状态+ +的+先前+请求期间+有关+例如+ +添加+控件时+动态+的+对照+添加+ +一期间后+ + +回 –

回答

0

只需添加EnableViewState="false"PlaceHolderControls