Why "view status cannot be loaded" occurs when the user control is dynamically loaded on the Asp.net page

Source: Internet
Author: User

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

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.