C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Interview   Jobs   Projects   Offshore Development    
Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Revenue Sharing | Talk to Us |



My Profile

Gifts

Active Members
TodayLast 7 Days more...







Move ViewState to the bottom of the page.


Posted Date: 03 Feb 2005    Resource Type: Articles    Category: .NET Framework

Posted By: Dolph Larson       Member Level: Bronze
Rating:     Points: 10


The resource has not been reviewed by Editors yet. Readers are adviced to use their best judgement before accessing this resource.
This resource will be reviewed shortly.
If you think this resource contain inappropriate content, please report to webmaster.


The following code sniplet will do the trick.

Example:

First:
I created an assembly called WebPageBase and a class in the assembly called PageBase. My class inherits from
System.Web.UI.Page like a normal aspx page does.
namespace WebPageBase
{
///
/// Main Base for RealtorNavigator Web
///

///[Designer(typeof(System.Web.UI.Page))]
public class PageBase : System.Web.UI.Page
{
}

Next: Add this method in the PageBase class:
///
/// 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();

// Hose the writers we don't need anymore.
hw.Close();
sw.Close();

// Find the viewstate. Hope M$ doesn't decide to change the case of these tags anytime soon.
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;

// Strip out the viewstate.
string viewstate = html.Substring( start, end - start );
html = html.Remove( start, end - start );

// Find the end of the form and insert it there, since we can't put it any lower in the response stream.
int formend = html.IndexOf("") - 1;
html = html.Insert( formend, viewstate );
}

// Send the results back into the writer provided.
writer.Write( html );
}

Finally:
Inherit your aspx pages from the base class.
Example:
public class Default : WebPageBase.PageBase
{
// Your code here
}
Note: I put my PageBase instead of the VisualStudio System.Web.UI.Page








Responses

Author: Mary Mathew    06 Feb 2005Member Level: Bronze   Points : 0
That's a cool one, Dolph. We are looking forward to see more from you.....


Author: Anil Rajan    21 Feb 2005Member Level: Gold   Points : 0
kudos,
hey that's very good , the basic of aspx page is all end up being rendered as html ,using this concept a lot of user control manipulation is possible and you have thought another good things.



Feedbacks      
Popular Tags   What are tags ?   Search Tags  
(No tags found.)
You must Sign In to post a response.
Next Resource: Difference between class and struct in C#
Previous Resource: How to start and stop windows services programmatically
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!

Related Resources



Read TATA Nano reviews.
dotNet Slackers   BizTalk Adaptors    Web Design   Scripts
Are you waiting for engineering entrance result? Watch here for engineering entrance results.
internet fax

Contact Us    Privacy Policy    Terms Of Use