> Manuales > Taller de ASP.NET

Cómo colocar el viewstate al final de las páginas para optimizarlas de cara a los buscadores.

Gracias a esta función podremos ubicar el viewstate al final de las páginas. Colocar el viewstate al final de una página asp.net puede ser útil para optimizaciones para buscadores (para poner al final el viewstate que su contenido para un buscador no tiene sentido), agilizar la carga de la página...

Código en Visual Basic.Net


Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)

Dim stringWriter As System.IO.StringWriter = New System.IO.StringWriter

Dim htmlWriter As HtmlTextWriter = New HtmlTextWriter(stringWriter)

MyBase.Render(htmlWriter)

Dim html As String = stringWriter.toString()

Dim StartPoint As Integer = html.IndexOf("<;input type=""hidden"" name=""__VIEWSTATE""")

If StartPoint >;= 0 Then 'does __viewstate exist?

Dim EndPoint As Integer = html.IndexOf("/>;", StartPoint) + 2

Dim viewstateInput As String = html.Substring(StartPoint, EndPoint - StartPoint)

html = html.Remove(StartPoint, EndPoint - StartPoint)

Dim FormEndStart As Integer = html.IndexOf("<;/form>;") - 1

If FormEndStart >;= 0 Then

html = html.Insert(FormEndStart, viewstateInput)

End If

End If

writer.Write(html)

End Sub



Código en C#

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();

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

if (startPoint >;= 0){

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

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

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

int formEndStart = html.IndexOf("<;/form>;");

if (formEndStart >;= 0){

html = html.Insert(formEndStart, "\n" + viewstateInput);}}

writer.Write(html);}

Pol Salvat

Manual