Click here to Skip to main content
Click here to Skip to main content
Alternative Tip/Trick

Tagged as

Another way to moving ViewState to the bottom of page

, 14 Apr 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
If you move your ViewState from the top of the page to the bottom, you will get better search engine spidering.Step 1Create a class file in App_code folder of your application and name it as PageBase.cs. Copy the following code to the class file.using System.IO;using...
If you move your ViewState from the top of the page to the bottom, you will get better search engine spidering.

Step 1

Create a class file in App_code folder of your application and name it as PageBase.cs. Copy the following code to the class file.
using System.IO;
using System.Web.UI;
 
namespace WebPageBase
{
    public class PageBase : System.Web.UI.Page
    {
    /// 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.
    protected override void Render(System.Web.UI.HtmlTextWriter writer)
    {
        // Obtain the HTML rendered by the instance.
        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);
        base.Render(hw);
        string html = sw.ToString();
        
        // Close the writers we don't need anymore.
        hw.Close();
        sw.Close();
 
        // Find the viewstate. 
        int start = html.IndexOf(@"<input type=""hidden"" name=""__VIEWSTATE""" );
        // If we find it, then move it.
        if (start > -1)
        {
            int end = html.IndexOf("/>", start) + 2;
            
            string strviewstate = html.Substring(start, end - start);
            html = html.Remove(start, end - start);
 
            // Find the end of the form and insert it there.
            int formend = html.IndexOf(@"</form>") - 1;
            html = html.Insert(formend, strviewstate);
        }
 
        // Send the results back into the writer provided.
        writer.Write(html);
    }
}
}

Step 2


Inherit your aspx pages from the base class.
public partial class Page : WebPageBase.PageBase
{
	// Your code here
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Mike Thom
Software Developer (Senior)
India India
No Biography provided

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web03 | 2.8.141022.2 | Last Updated 14 Apr 2011
Article Copyright 2011 by Mike Thom
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid