Infolink

 

Search This Blog

Oct 10, 2012

View State Rendering

The problem in earlier versions is that the viewstate is rendered at the bottom of the page.
If the page is posted back before the entire page is loaded, the viewstate that is being submitted back to the server is incomplete and thus invalid.

This was fixed with .NET 3.5 SP1.

But You can Fix it by below Render function in earlier version of .NET

private static string[] aspNetFormElements = new string[]
    {
        "__EVENTTARGET",
        "__EVENTARGUMENT",
        "__VIEWSTATE",
        "__EVENTVALIDATION",
        "__VIEWSTATEENCRYPTED",
    };

    protected override void Render(HtmlTextWriter writer)
    {

        StringWriter stringWriter = new StringWriter();

        HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);

        base.Render(htmlWriter);

        string html = stringWriter.ToString();

        int formStart = html.IndexOf("<form");

        int endForm = -1;

        if (formStart >= 0)

            endForm = html.IndexOf(">", formStart);


        if (endForm >= 0)
        {

            StringBuilder viewStateBuilder = new StringBuilder();

            foreach (string element in aspNetFormElements)
            {

           int startPoint = html.IndexOf("<input type=\"hidden\" name=\"" + element + "\"");

                if (startPoint >= 0 && startPoint > endForm)
                {

                    int endPoint = html.IndexOf("/>", startPoint);

                    if (endPoint >= 0)
                    {

                        endPoint += 2;

                        string viewStateInput = html.Substring(startPoint, endPoint - startPoint);

                        html = html.Remove(startPoint, endPoint - startPoint);

                        viewStateBuilder.Append(viewStateInput).Append("\r\n");

                    }

                }

            }


            if (viewStateBuilder.Length > 0)
            {

                viewStateBuilder.Insert(0, "\r\n");

                html = html.Insert(endForm + 1, viewStateBuilder.ToString());

            }

        }

        writer.Write(html);
    }

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...