2015-11-04 97 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