The state information is invalid for this page and might be corrupted. (Only in IE)

前端 未结 4 704
迷失自我
迷失自我 2020-12-09 20:56

Can anybody help me out with this exception. I have tried couple of fixes but nothing worked. I am getting this exception only in IE(7, 8 and 9).

When i load the pa

4条回答
  •  既然无缘
    2020-12-09 21:57

    I know this has been answered but here are a couple of other options:

    1). If you're doing a web service call via jquery .load() you can just remove the viewstate upon return using loads callback parameter

    $('#myDiv').load('/MyPage.aspx', null, function(){ 
         $('.aspNetHidden', this).remove(); // removes viewstate from returned aspx html
    });
    

    2). Using the Html Agility Pack You can do this same thing in a web service before rendering the returned control. Assume you're calling a web service which loads a UserControl.ascx in the service and then renders it's html before returning.

    [WebMethod(EnableSession = true)]
    [System.Web.Script.Services.ScriptMethod]
    public string GetControlHtml()
    {
    
    // do stuff to get the control you want
    
    ....
    
    Page page = new Page();
    HtmlForm form = new HtmlForm();
    var ctl = (MyControlsNameSpace.Controls.MyControl)page.LoadControl("Controls\\MyControl.ascx");
    
    page.Controls.Add(form);
    form.Controls.Add(ctl);
    StringWriter result = new StringWriter();
    HttpContext.Current.Server.Execute(page, result, false);
    
    // Extension Method RemoveViewStateFromControl
    var MyControlsHTML = result.RemoveViewStateFromControl();
    return MyControlsHTML;
    
    }
    
    .....
    
    // In an extensions class....
    public static string RemoveViewStateFromExecuteControl(this StringWriter writer)
        {
            HtmlAgilityPack.HtmlDocument Doc = new HtmlDocument();
            Doc.LoadHtml(writer.ToString());
            var Divs = Doc.DocumentNode.SelectNodes("//div");
            if (Divs != null)
            {
                foreach (var Tag in Divs)
                {
                    if (Tag.Attributes["class"] != null)
                    {
                        if (string.Compare(Tag.Attributes["class"].Value, "aspNetHidden", StringComparison.InvariantCultureIgnoreCase) == 0)
                        {
                            Tag.Remove();
                        }
                    }
                }
            }
    
            return Doc.DocumentNode.OuterHtml;
        }
    

提交回复
热议问题