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

只愿长相守 提交于 2019-11-28 09:23:07

Ok, so here is the solution/hack i came up with. My problem was that adding a user control dynamically (through ajax request) onto page was changing the view state of the page and was throwing an exception. Upon research I found out that viewstate stores the state of a page (properties and settings). Once you return the controls html from a web service, there is going to be some sort of viewstate stored onto page. And when you post back to the server, it will throw an exception when it decrypt the viewstae to rebuild the page. I have simply removed those controls (which got added dynamically) on page post back using jquery and problem got solved.

//In my case "VendorListDropDownSearchable", causes the page post back.
    $("#VendorListDropDownSearchable").change( function () {
        $("#UserControl1DIV").remove(); //removing the place holder holding control1
        $("#UserControl2DIV").remove(); //same as above
    });

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;
    }

In my case, the problem was having two <form></form> tags.

I have a TextBox in my main page. If the user enters a value in this TextBox, a Script goes to another page and searches that value while user continues to write it. Normally I created this "another" page by clicking Website, Add New Item. So Visual Studio created a new page for me. As usual there was <form></form> tag in this new page, too. So I deleted this tags and the problem was solved.

Below are my codes:

(PS: I use MasterPage. But the below code is only needed in my main page)

    Dim scriptText As String
    scriptText = ""
    scriptText += "function serinogoster(str) {" + Chr(10)
    scriptText += "var xhttp;" + Chr(10)
    scriptText += "if (str == '') {" + Chr(10)
    scriptText += "document.getElementById('ctl00_ContentPlaceHolder1_Label19').innerHTML = '';" + Chr(10)
    scriptText += "return;" + Chr(10)
    scriptText += "}" + Chr(10)
    scriptText += "xhttp = new XMLHttpRequest();" + Chr(10)
    scriptText += "xhttp.onreadystatechange = function() {" + Chr(10)
    scriptText += "if (this.readyState == 4 && this.status == 200) {" + Chr(10)
    scriptText += "document.getElementById('ctl00_ContentPlaceHolder1_Label19').innerHTML = this.responseText;" + Chr(10)
    scriptText += "}" + Chr(10)
    scriptText += "};" + Chr(10)
    scriptText += "xhttp.open('GET', 'serinover.aspx?serino='+str, true);" + Chr(10)
    scriptText += "xhttp.send();" + Chr(10)
    scriptText += "}" + Chr(10)
    Me.ClientScript.RegisterClientScriptBlock(Me.GetType(), "OnKeyUpScript", scriptText, True)
    TextBox6.Attributes.Add("onkeyup", "serinogoster(this.value);")

In the above code serinover.aspx is the second page which should not have <form></form> tag.

Empty the div you loaded "on hiddden". I found that emptying the div or setting it back to the original text (mine was a loading message) both worked.

<a class="popmodal" href="YOUR HREF HERE">View History</a>


    $('.popmodal').on('click', function (e) {
                e.preventDefault();
                $('#modalViewBenefitsHistory').modal('show').find('.modal-content').load($(this).attr('href'));
            });


    $('#modalViewBenefitsHistory').on('hidden.bs.modal', function (e) {
                $('.modal-content').empty();
            })

--OR

$('#modalViewBenefitsHistory').on('hidden.bs.modal', function (e) {
            $('.modal-content').text("&nbsp;Loading your benefits history...please wait a moment.");
        })



<div id="modalViewBenefitsHistory" aria-hidden="true" class="modal fade">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content">
            &nbsp;Loading your benefits history...please wait a moment.
        </div>
    </div>
</div>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!