Welcome to Carlos Zanini Sign in | Join | Help

Carlos Zanini

I'm a GOD (Google-Oriented Developer)

Como quitar correctamente el Viewstate en ASP.NET 2.0

Para un proyecto ASP.NET 2.0 que será visto desde celulares fue necesario quitar el VIEWSTATE debido a que daba errores en algunos Nokia. Lo primero, fue editar el Web.Config:

<pages enableViewState="false" />

Con esto, se logra achicar el Viewstate pero no resuelve el problema por completo, ya que en el xHtml generado aparece lo siguiente:

<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKLTU0MzE2NzU0MGRk" />

Incluso con <%@ Page Language="C#" EnableViewState="false" ... %> por cada página tampoco logramos ningún cambio.

Otra opción fue eliminar el control __VIEWSTATE dinámicamente con el siguiente código:

protected override void Render(HtmlTextWriter writer)
{
   System.IO.
StringWriter stringWriter = new System.IO.StringWriter();
  
HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);
  
base.Render(htmlWriter);
  
string html = stringWriter.ToString();
  
int endPoint;
  
int startPoint = html.IndexOf("<input type=\"hidden\" name=\"__VIEWSTATE\"");
  
if (startPoint >= 0)
   {
      endPoint = html.IndexOf(
"/>", startPoint) + 2;
      html = html.Remove(startPoint, endPoint - startPoint);
   }
   writer.Write(html);
}


Sí, no es lo más elegante pero cumplió su cometido y quitó completamente el Viewstate. Eso si.. dejo de funcionar todo el sitio :-(
ASP.NET "necesita" que exista un control hidden llamado VIEWSTATE por lo que la solución pasa por agregar el siguiente código en el Form

protected override object LoadPageStateFromPersistenceMedium()
{
  
return Session["ViewState"];
}

protected override void SavePageStateToPersistenceMedium(object viewState)
{
   Session[
"ViewState"] = viewState;
   
RegisterHiddenField("__VIEWSTATE", "");
}


Listo!! ahora el sitio funciona correctamente, el Viewstate quedó de la siguiente manera:

<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="" />

Y puedo continuar utilizando las propiedades de mis controles TextBox1.Text, Label1.SkinID, etc etc ya que estos objetos los crea ASP.NET sin necesidad del Viewstate sino simplemente con el stream que viaja en cada HTTP POST.

Lo ideal sería que tanto el override de Render, LoadPageStateFromPersistenceMedium y SavePageStateToPersistenceMedium estén dentro de una clase que herede de System.Web.UI.Page y los demás Forms hereden de esa nueva clase creada.

Saludos.
Published 28-06-2006 11:12 by carloszanini
Filed under , , ,

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

 

JJ said:

Prueba tu código antes de usarlo. Yo uso algo parecido, pero mi primera aproximación fue muy similar a lo que estás programando ahora... pero todo falló estrepitosamente. Ahora no tengo tiempo, pero este fin de semana prometo escribir algo sobre esto. (Es curioso, porque incluso encontré un bug en la implementación del framework de microsoft)
junio 29, 2006 04:47 a.m.
 

carloszanini said:

El sitio funciona bien con esto :)
Que fue lo que te falló, en tu caso?
junio 29, 2006 11:50 a.m.
 

JJ said:

Ya me acuerdo ;)

El principal problema aparece cuando el usuario abre los enlaces de la aplicación en ventanas nuevas. Ya que con ese código solo se guarda 1 único viewstate. El framework tiene una opción similar, pero ellos guardan 20 copias... lo que en mis pruebas fue insuficiente (imaginate las risas que tuvimos cuando detectamos que la aplicación fallaba cuando se usaba 20 veces) ;)
junio 30, 2006 06:43 a.m.
 

carloszanini said:

Ahh claro, eso lo leí pero en este caso es una aplicación que es ejecutada desde un móvil por lo que no se dá que un mismo usuario abra 2 instancias del mismo sitio. De todas maneras es buena la aclaración, gracias.
junio 30, 2006 04:17 p.m.
 

Enrique said:

hola carlos tengo un problema, estoy desarrollando una pagina aspx en, el problema es q en algunos celulares nokia no funciona en otros si, ya intente con lo del view state pero sigue sin mostrarme la pagina, alguna sugerencia?

septiembre 9, 2008 09:57 a.m.
 

XD said:

En verdad eres dios, jamas se me habria ocurrido algo tan simple y tan efectivo.

Aparte de lo ya comentado por algun compañero el sistema no tiene ni un fallo.

Gracias

septiembre 18, 2008 04:55 a.m.
 

FicyAcesy said:

Hello.

:)  

"The birth took place at the couple's home (just outside Oslo)," says the palace, adding mother and baby were both well.

 Bye.

octubre 14, 2008 12:42 a.m.
 

ImmireToisork said:

[url=http://pendosam.com/?product=accutane]Buy Accutane[/url] Accutane can cause severe, life-threatening birth defects if the mother takes the medication during pregnancy. Even one dose of Accutane can cause major birth defects of the baby's ears, eyes, face, skull, heart, and brain. Never use Accutane if you are pregnant.

Women of child-bearing potential must agree in writing to use two specific forms of birth control and have regular pregnancy tests before, during, and after taking Accutane. Unless you have had a total hysterectomy or have been in menopause for at least a year, you are considered to be of child-bearing potential.

http://pendosam.com/?product=accutane Buy Accutane

octubre 21, 2008 09:24 p.m.
 

ImmireToisork said:

[url=http://pendosam.com/?product=accutane]Buy Accutane[/url] Accutane can cause severe, life-threatening birth defects if the mother takes the medication during pregnancy. Even one dose of Accutane can cause major birth defects of the baby's ears, eyes, face, skull, heart, and brain. Never use Accutane if you are pregnant.

Women of child-bearing potential must agree in writing to use two specific forms of birth control and have regular pregnancy tests before, during, and after taking Accutane. Unless you have had a total hysterectomy or have been in menopause for at least a year, you are considered to be of child-bearing potential.

http://pendosam.com/?product=accutane Buy Accutane

octubre 21, 2008 09:24 p.m.
 

ImmireToisork said:

[url=http://pendosam.com/?product=accutane]Buy Accutane[/url] Accutane can cause severe, life-threatening birth defects if the mother takes the medication during pregnancy. Even one dose of Accutane can cause major birth defects of the baby's ears, eyes, face, skull, heart, and brain. Never use Accutane if you are pregnant.

Women of child-bearing potential must agree in writing to use two specific forms of birth control and have regular pregnancy tests before, during, and after taking Accutane. Unless you have had a total hysterectomy or have been in menopause for at least a year, you are considered to be of child-bearing potential.

http://pendosam.com/?product=accutane Buy Accutane

octubre 21, 2008 09:24 p.m.
 

ImmireToisork said:

[url=http://pendosam.com/?product=accutane]Buy Accutane[/url] Accutane can cause severe, life-threatening birth defects if the mother takes the medication during pregnancy. Even one dose of Accutane can cause major birth defects of the baby's ears, eyes, face, skull, heart, and brain. Never use Accutane if you are pregnant.

Women of child-bearing potential must agree in writing to use two specific forms of birth control and have regular pregnancy tests before, during, and after taking Accutane. Unless you have had a total hysterectomy or have been in menopause for at least a year, you are considered to be of child-bearing potential.

http://pendosam.com/?product=accutane Buy Accutane

octubre 21, 2008 09:24 p.m.
 

himamaster said:

XZHGWo himamaster hi

noviembre 26, 2008 04:29 a.m.
 

NumjammonaDob said:

Hello!

[url=http://pendosam.org/]Buy Accutane[/url]

a cervical cap plus spermicidal foam or gel; and

[url=http://pendosam.org/]accutane acne medicine[/url]

diciembre 16, 2008 04:17 p.m.
 

NumjammonaDob said:

Hello!

[url=http://pendosam.org/]Buy Accutane[/url]

a cervical cap plus spermicidal foam or gel; and

[url=http://pendosam.org/]accutane acne medicine[/url]

diciembre 16, 2008 04:17 p.m.
 

NumjammonaDob said:

Hello!

[url=http://pendosam.org/]Buy Accutane[/url]

a cervical cap plus spermicidal foam or gel; and

[url=http://pendosam.org/]accutane acne medicine[/url]

diciembre 16, 2008 04:17 p.m.
 

NumjammonaDob said:

Hello!

[url=http://pendosam.org/]Buy Accutane[/url]

a cervical cap plus spermicidal foam or gel; and

[url=http://pendosam.org/]accutane acne medicine[/url]

diciembre 16, 2008 04:17 p.m.
 

TaphSoiseeSip said:

Hello!

[url=http://pendosam.org/]Buy Accutane[/url]

If you have any of these conditions, you may need a dose adjustment or special tests to safely take Accutane.

[url=http://pendosam.org/]accutane and adult acne[/url]

diciembre 19, 2008 05:02 a.m.
 

TaphSoiseeSip said:

Hello!

[url=http://pendosam.org/]Buy Accutane[/url]

If you have any of these conditions, you may need a dose adjustment or special tests to safely take Accutane.

[url=http://pendosam.org/]accutane and adult acne[/url]

diciembre 19, 2008 05:02 a.m.
 

TaphSoiseeSip said:

Hello!

[url=http://pendosam.org/]Buy Accutane[/url]

If you have any of these conditions, you may need a dose adjustment or special tests to safely take Accutane.

[url=http://pendosam.org/]accutane and adult acne[/url]

diciembre 19, 2008 05:02 a.m.
 

TaphSoiseeSip said:

Hello!

[url=http://pendosam.org/]Buy Accutane[/url]

If you have any of these conditions, you may need a dose adjustment or special tests to safely take Accutane.

[url=http://pendosam.org/]accutane and adult acne[/url]

diciembre 19, 2008 05:03 a.m.
 

Boffloory said:

Hello!

[url=http://pendosam.org/]Buy Accutane[/url]

Accutane

[url=http://pendosam.org/]accutane about[/url]

diciembre 22, 2008 08:14 a.m.
 

Boffloory said:

Hello!

[url=http://pendosam.org/]Buy Accutane[/url]

Accutane

[url=http://pendosam.org/]accutane about[/url]

diciembre 22, 2008 08:15 a.m.
 

Boffloory said:

Hello!

[url=http://pendosam.org/]Buy Accutane[/url]

Accutane

[url=http://pendosam.org/]accutane about[/url]

diciembre 22, 2008 08:16 a.m.
 

Boffloory said:

Hello!

[url=http://pendosam.org/]Buy Accutane[/url]

Accutane

[url=http://pendosam.org/]accutane about[/url]

diciembre 22, 2008 08:16 a.m.
 

Boffloory said:

Hello!

[url=http://pendosam.org/]Buy Accutane[/url]

Accutane

[url=http://pendosam.org/]accutane about[/url]

diciembre 22, 2008 08:17 a.m.
 

Gabriel Porras said:

Carlos.. Eliminá esos comentarios basura!!!

Bueno.. También te quería preguntar si con tu solución el IsPostBack no siempre queda en false.

Eso me está pasando.

Gracias!

enero 31, 2009 07:48 p.m.
 

Gabriel Porras said:

Carlos.. Lee también este problema que encontré sobre tu técnica: http://www.hanselman.com/blog/PermaLink.aspx?guid=e73efd87-2cae-49af-9674-7076a054f2ca

Saludos!

enero 31, 2009 08:03 p.m.
 

black hattitude said:

Hi,

Thank you for the great quality of your blog, each  time i come here, i'm amazed.

[url=http://blackhattitude.video-2-grosse.com]black hattitude[/url].

octubre 21, 2009 07:19 p.m.
 

xm1zcjz17q said:

Hi! Ebanij vrot! u52xov3kmo qorpkywcwp!

diciembre 27, 2009 04:23 a.m.
 

Telefon Schnurlos said:

the precious content u provided do help our team's investigation for our company, thanks.

agosto 21, 2010 12:32 a.m.
 

loarpirunniny said:

I enjoyed reading your blog. Keep it that way.

agosto 21, 2010 08:32 p.m.
 

Email Form said:

You made some good points there. I did a search on the topic and hardly found any specific details on other sites, but then great to be here, seriously, thanks...

- John

agosto 23, 2010 12:56 a.m.

Leave a Comment

(required) 
(optional)
(required) 

About carloszanini

Bio..

Post Calendar

<junio de 2006>
dlmmjvs
28293031123
45678910
11121314151617
18192021222324
2526272829301
2345678

Search

Go

My Certifications

MCPD: Web Developer
MCTS: Web & Distributed Applications
Skype me!

SkinName:CarlosZanini
Powered by Community Server (Personal Edition), by Telligent Systems