I have defined two user controls.
Definition1 = (Definition) Page. LoadControl ("ModuleControl/Definition. ascx ");
RightDefine1 = (RightDefine) Page. LoadControl ("RightControl/RightDefine. ascx ");
Select different user controls to load according to the radio on the page,
Private void Page_Load (object sender, System. EventArgs e)
{
// Response. Write (ModuleType. ToString () + "select <br> ");
// Response. Write (ViewType_txt.Text + "select <br> ");
//
If (ModuleType. ToString ()! = ViewType_txt.Text)
{
WebPart. EnableViewState = false;
ViewType_txt.Text = ModuleType. ToString ();
//
// _ PageModule = Request. QueryString ["PageModule"];
// If (_ pageModule! = "")
// Response. Redirect (Request. Url + "? PageModule = "+ (int) ModuleDetail). ToString ());
}
WebPart. Controls. Clear ();
Switch (ModuleType)
{
Case (int) LoadContent. ModuleDetail:
Definition1 = (Definition) Page. LoadControl ("ModuleControl/Definition. ascx ");
Definition1.PostShowCatalog + = new AuthorWeb. Web. ModuleControl. Definition. ShowCatalogEventHandler (definition#postshowcatalog );
ModuleCatalog1.SelectedCatalog + = new AuthorWeb. Web. ModuleControl. ModuleCatalog. SelectCatalogEventHandler (ModuleCatalog1_SelectedCatalog );
Definition1.ID = "ID_Define ";
WebPart. Controls. Add (Definition1 );
// Response. Write ("add define control <br> ");
Break;
Case (int) LoadContent. ModuleRightDetail:
RightDefine1 = (RightDefine) Page. LoadControl ("RightControl/RightDefine. ascx ");
RightDefine1.ID = "ID_RightDefine ";
WebPart. Controls. Add (RightDefine1 );
ModuleCatalog1.SelectedCatalog + = new AuthorWeb. Web. ModuleControl. ModuleCatalog. SelectCatalogEventHandler (ModuleCatalog1_SelectedCatalog_byRight );
Break;
}
}
You may encounter the following problems in several times:
View status cannot be loaded. The Control tree in which the view status is being loaded must match the control tree used to save the view status during the previous request. For example, when a control is added dynamically, the control added during the sending back must match the type and position of the control added during the initial request.
Discussions on this issue include:
Http://www.cnblogs.com/airforce1st/archive/2004/08/27/36973.aspx
Http://blog.joycode.com/ghj/archive/2004/04/16/19768.aspx
I think this problem is caused by ViewState.
When the user tries to load the second control during the sending-back process, the server tries to restore the status (or event) of the existing user control because the ViewState of the first control still exists ), as a result, this problem occurs.
I made some attempts:
For example, when loading different user controls, set EnableViewState of PlaceHolder to false;
Add a defined id to the user control:
Definition1.ID = "ID_Define ";
WebPart. Controls. Add (Definition1 );
However, there is still a problem during running.
Finally, there was no way. With the help of ben, I added a line of code to solve the problem.
We all agree that this is a Microsoft bug.
What is this line of code ??
WebPart. Controls. Add (new Label (); // it is
WebPart. Controls. Add (RightDefine1 );
Continued:
Http://www.cnblogs.com/king_astar/archive/2005/06/01/165763.html