Advertisement

06.05.2008 at 10:17AM PDT, ID: 23461093
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

9.0

How can I load/remove a control during postback without running into "Failed to load viewstate" error?

Asked by bemara57 in Programming for ASP.NET, C# Programming Language, .NET Framework 3.x versions

In the Init of one of my pages, I have a condition that either loads one control or another, depending on what the user posted back. The problem is that if a page starts out with one control, then the postback wants to remove that control and add a different control instead, I get the dreaded error of:

"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."

So my question is how do I change the control tree during postback? I tried doing a ViewState.Remove(controlID) during the Init, but that didn't work. Below is an example that I'm trying to do. Basically I have a dropdown menu full of file paths. If the file path is a user control (ends in .ascx), then I load it to the page. Otherwise I load the contents of the chosen file to a custom editor control and add that. But the problem is someone can go from user control to plain file and vice-versa during a postback- which causes the failed viewstate error. Anyone know a way to solve this scenario?Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
protected void Page_Init(object sender, EventArgs e)
{
    if (!Request.Form["__EVENTTARGET"].IsNullOrEmpty())
    {
        if (Request.Form["__EVENTTARGET"] == Globals.FileDropDown.UniqueID)
        {
            Session["file"] = Helpers.GetFormValue(
                Request.Form["__EVENTTARGET"]);                        
        }
    }
 
    if (Path.GetExtension(Session["file"]) == ".ascx")
    {
        Control dynamicContent = LoadControl(Session["file"]);
        dynamicContent.ID = "dynamic_control";
        this.Controls.Add(dynamicContent);
    }
    else
    {
        ContentEditor.Value = Helpers.GetContents(Session["file"]);
        this.Controls.Add(ContentEditor);
    }
}
[+][-]06.05.2008 at 10:28AM PDT, ID: 21721905

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: Programming for ASP.NET, C# Programming Language, .NET Framework 3.x versions
Sign Up Now!
Solution Provided By: SystemExpert
Participating Experts: 2
Solution Grade: A
 
 
[+][-]06.05.2008 at 10:32AM PDT, ID: 21721943

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628