Geeks With Blogs
AngelEyes on .Net Tips, tricks and WTFs about Asp .Net, SQL Server and the likes.

I was looking for the code I once used to put the viewstate tag at the bottom of the form tag, instead of the top. I found some code, used an automated VB-to-C# converter, and the results were not good.

I'm not sure if this is a difference between C# and VB or just a bug posted on Scott's blog, but the insertion place was wrong. So I changed that (removed the -1) and also changed the way he checked if the viewstate object was found, so that if not, we'd still have the original html, before removing the tag.

 Edit: I now just saw that someone commented on the -1 issue on that page. Anyway, here's my code:

        /// <summary>
        /// This method overrides the Render() method for the page and moves the ViewState
        /// from its default location at the top of the page to the bottom of the page. This
        /// results in better search engine spidering.
        /// </summary>
        /// <param name="writer"></param>
        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            System.IO.StringWriter stringWriter = new System.IO.StringWriter();
            HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);
            base.Render(htmlWriter);
            string html = stringWriter.ToString();
            string newHtml = string.Empty;

            int StartPoint = html.IndexOf("<input type=\"hidden\" name=\"__VIEWSTATE\"");
            if ((StartPoint >= 0))
            {
                // does __VIEWSTATE exist?               
                int EndPoint = (html.IndexOf("/>", StartPoint) + 2);
                string ViewStateInput = html.Substring(StartPoint, (EndPoint - StartPoint));

                //I'm using newHtml so that if we cannot find the viewsource's right location, we still have the original html
                newHtml = html.Remove(StartPoint, (EndPoint - StartPoint));
                int FormEndStart = (newHtml.IndexOf("</form>"));
                if ((FormEndStart >= 0))
                {
                    //success - so replace the string, putting the viewstate at the end.
                    html = newHtml.Insert(FormEndStart, ViewStateInput);
                }
            }
            writer.Write(html);
        }

Posted on Friday, June 1, 2007 12:48 PM | Back to top


Comments on this post: Moving the viewstate to the bottom

# re: Moving the viewstate to the bottom
Requesting Gravatar...
Hı :) Thank you post... good :P

SMF Destek

Left by SMF Destek on Mar 26, 2009 10:27 PM

# re: Moving the viewstate to the bottom
Requesting Gravatar...
Worked perfectly for me! Thanks a lot!
Left by Joe on Apr 21, 2009 4:34 AM

# re: Moving the viewstate to the bottom
Requesting Gravatar...
I'm glad I could help :-)
Left by angel eyes on Apr 21, 2009 10:08 AM

# re: Moving the viewstate to the bottom
Requesting Gravatar...
It worked , you saved my time
Left by web development company on Aug 11, 2009 1:45 PM

# re: Moving the viewstate to the bottom
Requesting Gravatar...
hi.. very useful post.. i was finding it since last 3 hrs. Thank you vry much.
Left by Avkash on Oct 08, 2011 6:23 AM

Your comment:
 (will show your gravatar)


Copyright © AngelEyes | Powered by: GeeksWithBlogs.net