Advertuse

Search This Blog

Your Ad Here

Monday, 13 June 2011

move ViewState to bottom of Page in ASP.NET

on last Saturday, in Community tech days Tejash Shah has discussed  about  viewstate's  prons and crons.  in his wonderful session and also give hint  that if you want to move your viewstate at bottom of  page to  load  page faster in ur browser then you can do it .
so question is   how to move  viewstate in bottom.....??...  for that we have to override render event of  page and put below code you can move your page's  viewstate  at bottom of your page.

       protected override void Render(System.Web.UI.HtmlTextWriter strwriter)
        {
        
                System.IO.StringWriter strWriter = new System.IO.StringWriter();
                HtmlTextWriter objhtmlWriter = new HtmlTextWriter(strWriter );
                base.Render(objhtmlWriter);
                string newhtml = strWriter .ToString();

                  int SPt = newhtml.IndexOf("<input type=\"hidden\" name=\"__VIEWSTATE\"");
                 if (SP >= 0)
                {
                    int EP = newhtml.IndexOf("/>", SP) + 2;
                    string viewstateInput = newhtml.Substring(SP, EP - SP);
                    newhtml = newhtml.Remove(SP, EP - SP);
                    //int FormEndStart = newhtml.IndexOf("") - 1;
                    int FormEndStart = newhtml.IndexOf("");
                    if (FormEndStart >= 0)
                    {
                        newhtml = newhtml.Insert(FormEndStart, viewstateInput);
                    }
                }
                writer.Write(newhtml);
          

        } 

and  if you want to see demo  application you can download from here which made using  scottes blog

File name: ViewstateDemo.zip
File size:20.79 KB




compile by Divyang Panchasara Sr. Programmer Analyst Hitech OutSourcing

4 comments:

Priyank Tamboli said...

nice

Lalit Singh said...

Thanks, i had read from http://www.hanselman.com/blog/MovingViewStateToTheBottomOfThePage.aspx

Priyank Tamboli said...

nice post

divyang4481 said...

@lalit
ya that link I have shared with all SDU member in skype