<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Gerardo Contijoch</title>
	<atom:link href="http://gerardocontijoch.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://gerardocontijoch.wordpress.com</link>
	<description>Experiencias del día a día trabajando con .NET - ASP.NET, C#, ASP.NET MVC y demas...</description>
	<lastBuildDate>Thu, 19 Jan 2012 17:11:44 +0000</lastBuildDate>
	<language>es</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='gerardocontijoch.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Gerardo Contijoch</title>
		<link>http://gerardocontijoch.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://gerardocontijoch.wordpress.com/osd.xml" title="Gerardo Contijoch" />
	<atom:link rel='hub' href='http://gerardocontijoch.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Suspender el dibujado de un control o formulario</title>
		<link>http://gerardocontijoch.wordpress.com/2009/09/14/suspender-el-dibujado-de-un-control-o-formulario/</link>
		<comments>http://gerardocontijoch.wordpress.com/2009/09/14/suspender-el-dibujado-de-un-control-o-formulario/#comments</comments>
		<pubDate>Mon, 14 Sep 2009 17:10:34 +0000</pubDate>
		<dc:creator>Gerardo Contijoch</dc:creator>
				<category><![CDATA[.Net 2.0]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[control]]></category>
		<category><![CDATA[window-message]]></category>

		<guid isPermaLink="false">http://gerardocontijoch.wordpress.com/?p=349</guid>
		<description><![CDATA[Hay ocasiones en que nuestros Forms terminan recargados de controles, los cuales cambian de estado o contenido todos a la vez. Si la cantidad de controles es mucha y el contenido de los mismo es modificado varias veces en un breve intervalo de tiempo o simplemente el redibujado de los mismos es muy lento (es [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gerardocontijoch.wordpress.com&amp;blog=5700517&amp;post=349&amp;subd=gerardocontijoch&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Hay ocasiones en que nuestros Forms terminan recargados de controles, los cuales cambian de estado o contenido todos a la vez. Si la cantidad de controles es mucha y el contenido de los mismo es modificado varias veces en un breve intervalo de tiempo o simplemente el redibujado de los mismos es muy lento (es decir, el usuario nota como se va llenando cargando el contenido del Form progresivamente), podemos optar por suspender el redibujado del form en su totalidad hasta que los controles estén listos para ser dibujados.</p>
<p>Muy relacionado con mi <a title="último post" href="http://gerardocontijoch.wordpress.com/2009/09/12/encontrar-el-valor-de-un-window-message/" target="_blank">último post</a>, les dejo el <a href="http://stackoverflow.com/questions/487661/how-do-i-suspend-painting-for-a-control-and-its-children" target="_blank">link</a> al código de una clase que se encarga de suspender y reanudar el redibujado de un control (o form en su totalidad si lo deseamos). La técnica se basa en el envío de mensajes al control o ventana en cuestión.</p>
<p>¡Nos vemos en el próximo post!</p>
<p><em>Publicado originalmente en <a title="http://gerardocontijoch.wordpress.com" href="http://gerardocontijoch.wordpress.com/" target="_blank">http://gerardocontijoch.wordpress.com</a>.</em></p>
<br />Posted in .Net 2.0, C# Tagged: control, window-message <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/gerardocontijoch.wordpress.com/349/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/gerardocontijoch.wordpress.com/349/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/gerardocontijoch.wordpress.com/349/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/gerardocontijoch.wordpress.com/349/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/gerardocontijoch.wordpress.com/349/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/gerardocontijoch.wordpress.com/349/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/gerardocontijoch.wordpress.com/349/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/gerardocontijoch.wordpress.com/349/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/gerardocontijoch.wordpress.com/349/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/gerardocontijoch.wordpress.com/349/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/gerardocontijoch.wordpress.com/349/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/gerardocontijoch.wordpress.com/349/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/gerardocontijoch.wordpress.com/349/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/gerardocontijoch.wordpress.com/349/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gerardocontijoch.wordpress.com&amp;blog=5700517&amp;post=349&amp;subd=gerardocontijoch&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://gerardocontijoch.wordpress.com/2009/09/14/suspender-el-dibujado-de-un-control-o-formulario/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/08a523d4aac4f7ad6ff4eb1cb6ac85e2?s=96&#38;d=http%3A%2F%2Fs0.wp.com%2Fi%2Fmu.gif&#38;r=G" medium="image">
			<media:title type="html">Gerardo Contijoch</media:title>
		</media:content>
	</item>
		<item>
		<title>Encontrar el valor de un Window Message</title>
		<link>http://gerardocontijoch.wordpress.com/2009/09/12/encontrar-el-valor-de-un-window-message/</link>
		<comments>http://gerardocontijoch.wordpress.com/2009/09/12/encontrar-el-valor-de-un-window-message/#comments</comments>
		<pubDate>Sat, 12 Sep 2009 05:38:06 +0000</pubDate>
		<dc:creator>Gerardo Contijoch</dc:creator>
				<category><![CDATA[.Net 2.0]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[control]]></category>
		<category><![CDATA[eventos]]></category>
		<category><![CDATA[window-message]]></category>

		<guid isPermaLink="false">http://gerardocontijoch.wordpress.com/?p=348</guid>
		<description><![CDATA[De vez en cuando, si trabajamos con WinForms, nos puede surgir la necesidad de manejar un evento a muy bajo nivel y para hacerlo tenemos que interceptar los famosos Window Messages, o mensajes de ventana. Estos mensajes son el mecanismo que utiliza Windows para comunicarse con una aplicación. Cuando apretamos una tecla y nuestra aplicación [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gerardocontijoch.wordpress.com&amp;blog=5700517&amp;post=348&amp;subd=gerardocontijoch&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>De vez en cuando, si trabajamos con WinForms, nos puede surgir la necesidad de manejar un evento a muy bajo nivel y para hacerlo tenemos que interceptar los famosos<em> Window Messages</em>, o mensajes de ventana. Estos mensajes son el mecanismo que utiliza Windows para comunicarse con una aplicación. Cuando apretamos una tecla y nuestra aplicación responde, parece que es nuestra aplicación la que ‘atrapó’ el teclazo, pero en realidad es el Sistema Operativo (SO) el que lo atrapó (debido a que el SO es quien controla el puerto donde esta conectado el teclado) y este le envía un mensaje a nuestra aplicación para que responda al mismo, normalmente imprimiendo el caracter representado por la tecla presionada. Aunque no lo parezca, el SO es quien tiene control de todo lo que sucede y cuando nuestra aplicación responde a distintos inputs, es en realidad el SO el que le ordena que haga una cosa u otra.</p>
<p>Si tenemos un Form y el SO determina que es necesario que sea redibujado (porque cambio su contenido por ejemplo), entonces se envía un mensaje conocido como <strong>WM_PAINT </strong>(en .NET veremos que esto dispara el evento Paint), en cambio si presionamos la tecla ‘Q’ en el teclado, entonces el mensaje recibido por nuestra aplicación es <strong>WM_KEYDOWN </strong>(también tenemos el evento KeyDown en .NET, el cual es lanzado cuando se recibe un mensaje de este tipo). Cabe aclarar que el nombre de los mensajes es solo una ayuda para nosotros los humanos, ya que los mensajes en realidad no son más que un valor entero, y estos nombres son los nombres de las constantes asociadas a los mismos.</p>
<p>Para interceptar estos mensajes lo que se suele hacer comúnmente es sobrescribir el método <a href="http://msdn.microsoft.com/es-ar/library/system.windows.forms.control.wndproc.aspx" target="_blank">WndProc</a> definido en la clase <strong>Control</strong> de la siguiente manera:</p>
<div style="border:1px solid gray;overflow:auto;line-height:12pt;background-color:#f4f4f4;width:97.5%;font-family:consolas,'Courier New',courier,monospace;max-height:200px;font-size:8pt;cursor:text;margin:20px 0 10px;padding:4px;">
<div style="overflow:visible;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;padding:0;">
<pre style="overflow:visible;line-height:12pt;background-color:white;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> ...</pre>
<pre style="overflow:visible;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span> <span style="color:#0000ff;">private</span> <span style="color:#0000ff;">const</span> <span style="color:#0000ff;">int</span> WM_PAINT = 15;</pre>
<pre style="overflow:visible;line-height:12pt;background-color:white;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span></pre>
<pre style="overflow:visible;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   4:</span> <span style="color:#0000ff;">protected</span> <span style="color:#0000ff;">override</span> <span style="color:#0000ff;">void</span> WndProc(<span style="color:#0000ff;">ref</span> Message m) {</pre>
<pre style="overflow:visible;line-height:12pt;background-color:white;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   5:</span></pre>
<pre style="overflow:visible;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   6:</span>     <span style="color:#0000ff;">if</span> (m.Msg == WM_PAINT) {</pre>
<pre style="overflow:visible;line-height:12pt;background-color:white;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   7:</span>         <span style="color:#008000;">// Atrapamos el mensaje...</span></pre>
<pre style="overflow:visible;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   8:</span>     } <span style="color:#0000ff;">else</span>{</pre>
<pre style="overflow:visible;line-height:12pt;background-color:white;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   9:</span>         <span style="color:#008000;">// Ignoramos el mensaje y dejamos que siga su curso</span></pre>
<pre style="overflow:visible;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  10:</span>         <span style="color:#0000ff;">base</span>.WndProc(<span style="color:#0000ff;">ref</span> m);</pre>
<pre style="overflow:visible;line-height:12pt;background-color:white;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  11:</span>     }</pre>
<pre style="overflow:visible;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  12:</span></pre>
<pre style="overflow:visible;line-height:12pt;background-color:white;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  13:</span> }</pre>
<pre style="overflow:visible;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  14:</span> ...</pre>
</div>
</div>
<p>El método <em>WndProc</em> es el que se encarga de procesar todos los mensajes que recibe un Form y es por eso que ese es el punto indicado para atraparlos.</p>
<p>Ahora el problema esta en conocer el valor de los mensajes, ya que muchas veces uno sabe que mensaje procesar, pero no sabe cual es su valor. Estos valores están definidos en la clase <em>System.Windows.Forms.NativeMethods y <em>System.Design.NativeMethods</em></em>, pero lamentablemente estas clases son internas y no tenemos acceso a ellas, por lo que vamos a tener que usar .NET Reflector para ver su contenido.</p>
<p>La info de este post fue sacada de <a href="http://bphillips76.spaces.live.com/blog/cns!F9B548E4C21D6166!324.entry" target="_blank">aquí</a>.</p>
<p>¡Nos vemos en el próximo post!</p>
<p><em>Publicado originalmente en <a title="http://gerardocontijoch.wordpress.com" href="http://gerardocontijoch.wordpress.com/" target="_blank">http://gerardocontijoch.wordpress.com</a>.</em></p>
<br />Posted in .Net 2.0, C# Tagged: control, eventos, window-message <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/gerardocontijoch.wordpress.com/348/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/gerardocontijoch.wordpress.com/348/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/gerardocontijoch.wordpress.com/348/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/gerardocontijoch.wordpress.com/348/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/gerardocontijoch.wordpress.com/348/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/gerardocontijoch.wordpress.com/348/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/gerardocontijoch.wordpress.com/348/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/gerardocontijoch.wordpress.com/348/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/gerardocontijoch.wordpress.com/348/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/gerardocontijoch.wordpress.com/348/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/gerardocontijoch.wordpress.com/348/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/gerardocontijoch.wordpress.com/348/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/gerardocontijoch.wordpress.com/348/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/gerardocontijoch.wordpress.com/348/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gerardocontijoch.wordpress.com&amp;blog=5700517&amp;post=348&amp;subd=gerardocontijoch&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://gerardocontijoch.wordpress.com/2009/09/12/encontrar-el-valor-de-un-window-message/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/08a523d4aac4f7ad6ff4eb1cb6ac85e2?s=96&#38;d=http%3A%2F%2Fs0.wp.com%2Fi%2Fmu.gif&#38;r=G" medium="image">
			<media:title type="html">Gerardo Contijoch</media:title>
		</media:content>
	</item>
		<item>
		<title>&#161;Este blog no esta muerto! (s&#243;lo un poco dormido)</title>
		<link>http://gerardocontijoch.wordpress.com/2009/09/12/este-blog-no-esta-muerto-soacutelo-un-poco-dormido/</link>
		<comments>http://gerardocontijoch.wordpress.com/2009/09/12/este-blog-no-esta-muerto-soacutelo-un-poco-dormido/#comments</comments>
		<pubDate>Sat, 12 Sep 2009 04:32:59 +0000</pubDate>
		<dc:creator>Gerardo Contijoch</dc:creator>
				<category><![CDATA[Personal]]></category>
		<category><![CDATA[blogging]]></category>

		<guid isPermaLink="false">http://gerardocontijoch.wordpress.com/?p=345</guid>
		<description><![CDATA[Si alguien se estaba preguntando si este era uno más de esos blogs que nacen para morir al poco tiempo, se equivocó. El blog no esta muerto, sólo un poco dormido. La razón principal de mi ausencia es que estuve los últimos meses muy ocupado (ocupadísimo sería un término mas exacto) con proyectos cuyo desarrollo [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gerardocontijoch.wordpress.com&amp;blog=5700517&amp;post=345&amp;subd=gerardocontijoch&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Si alguien se estaba preguntando si este era uno más de esos blogs que nacen para morir al poco tiempo, se equivocó. El blog no esta muerto, sólo un poco dormido. La razón principal de mi ausencia es que estuve los últimos meses muy ocupado (ocupadísimo sería un término mas exacto) con proyectos cuyo desarrollo lamentablemente no entran dentro de la idea que tengo de este blog. Como expliqué en mi <a href="http://gerardocontijoch.wordpress.com/2008/12/02/hello-world/" target="_blank">primer post</a>, mi intensión es centrarme en tecnologías .NET y nada más. Entre estos proyectos que me mantuvieron alejado de la blogosfera se encuentra un site hecho con <a href="http://code.google.com/webtoolkit/" target="_blank">Google Web Tookit</a> (sólo recomendable para aquellos que quieran renegar mucho y obtener como resultado un site con un aire a producto de Google) y un proyecto desarrollado parte con Adobe Flex (realmente disfrute desarrollando en ActionScript 3.0, muy linda experiencia) y parte con un framework/entorno de desarrollo llamado <a href="http://unity3d.com/" target="_blank">Unity3d</a> (importando librerías escritas con <a href="http://mono-project.com/Main_Page" target="_blank">Mono</a>).</p>
<p>Hoy regresé, aunque no se si por mucho tiempo ya que en realidad sigo bastante ocupado.</p>
<p>¡Nos vemos en el próximo post!</p>
<p><em>Publicado originalmente en <a title="http://gerardocontijoch.wordpress.com" href="http://gerardocontijoch.wordpress.com/" target="_blank">http://gerardocontijoch.wordpress.com</a>.</em></p>
<br />Posted in Personal Tagged: blogging <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/gerardocontijoch.wordpress.com/345/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/gerardocontijoch.wordpress.com/345/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/gerardocontijoch.wordpress.com/345/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/gerardocontijoch.wordpress.com/345/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/gerardocontijoch.wordpress.com/345/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/gerardocontijoch.wordpress.com/345/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/gerardocontijoch.wordpress.com/345/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/gerardocontijoch.wordpress.com/345/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/gerardocontijoch.wordpress.com/345/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/gerardocontijoch.wordpress.com/345/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/gerardocontijoch.wordpress.com/345/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/gerardocontijoch.wordpress.com/345/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/gerardocontijoch.wordpress.com/345/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/gerardocontijoch.wordpress.com/345/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gerardocontijoch.wordpress.com&amp;blog=5700517&amp;post=345&amp;subd=gerardocontijoch&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://gerardocontijoch.wordpress.com/2009/09/12/este-blog-no-esta-muerto-soacutelo-un-poco-dormido/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/08a523d4aac4f7ad6ff4eb1cb6ac85e2?s=96&#38;d=http%3A%2F%2Fs0.wp.com%2Fi%2Fmu.gif&#38;r=G" medium="image">
			<media:title type="html">Gerardo Contijoch</media:title>
		</media:content>
	</item>
		<item>
		<title>Un CheckBoxList que funciona en ASP.NET MVC</title>
		<link>http://gerardocontijoch.wordpress.com/2009/07/04/un-checkboxlist-que-funciona-en-asp-net-mvc/</link>
		<comments>http://gerardocontijoch.wordpress.com/2009/07/04/un-checkboxlist-que-funciona-en-asp-net-mvc/#comments</comments>
		<pubDate>Sat, 04 Jul 2009 04:10:57 +0000</pubDate>
		<dc:creator>Gerardo Contijoch</dc:creator>
				<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[Desarrollo Web]]></category>
		<category><![CDATA[checkbox]]></category>
		<category><![CDATA[control]]></category>
		<category><![CDATA[extension-methods]]></category>
		<category><![CDATA[modelstate]]></category>

		<guid isPermaLink="false">http://gerardocontijoch.wordpress.com/?p=336</guid>
		<description><![CDATA[Si buscamos en internet ‘CheckBoxList ASP.NET MVC’ encontraremos infinidad de páginas con quejas de la desaparición del método de extensión CheckBoxList() de la clase HtmlHelper en la Preview 5 de ASP.NET MVC (antes de la versión oficial). Este es un problema de aparente fácil resolución ya que el método CheckBox() sigue estando presente, por lo [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gerardocontijoch.wordpress.com&amp;blog=5700517&amp;post=336&amp;subd=gerardocontijoch&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Si buscamos en internet ‘<em>CheckBoxList ASP.NET MVC</em>’ encontraremos infinidad de páginas con quejas de la desaparición del método de extensión <strong>CheckBoxList()</strong> de la clase <em>HtmlHelper </em>en la Preview 5 de ASP.NET MVC (antes de la versión oficial). Este es un problema de aparente fácil resolución ya que el método <strong>CheckBox()</strong> sigue estando presente, por lo que una lista de CheckBoxes podría crearse con un código similar al siguiente:</p>
<div style="border:1px solid gray;overflow:auto;line-height:12pt;background-color:#f4f4f4;width:97.5%;font-family:consolas,'Courier New',courier,monospace;max-height:200px;font-size:8pt;cursor:text;margin:20px 0 10px;padding:4px;">
<div style="overflow:visible;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;padding:0;">
<pre style="overflow:visible;line-height:12pt;background-color:white;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> &lt;% <span style="color:#0000ff;">for</span> (<span style="color:#0000ff;">int</span> i = 0; i &lt; 4; i++) {%&gt;</pre>
<pre style="overflow:visible;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span>   &lt;%= Html.CheckBox(<span style="color:#006080;">"checks"</span>, (<span style="color:#0000ff;">object</span>)<span style="color:#0000ff;">new</span> { <span style="color:#0000ff;">value</span> = <span style="color:#006080;">"val"</span> + i.ToString() })%&gt;&lt;%= <span style="color:#006080;">"Check "</span> + i.ToString() %&gt;&lt;br/&gt;</pre>
<pre style="overflow:visible;line-height:12pt;background-color:white;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span> &lt;%}%&gt;</pre>
</div>
</div>
<p>Sin embargo, rápidamente nos vamos a encontrar con <strong>dos</strong> problemas. El primero de ellos se hace presente dentro del código de la acción que se ejecuta al postear el form.</p>
<p>Dada la siguiente vista:</p>
<div style="border:1px solid gray;overflow:auto;line-height:12pt;background-color:#f4f4f4;width:97.5%;font-family:consolas,'Courier New',courier,monospace;max-height:200px;font-size:8pt;cursor:text;margin:20px 0 10px;padding:4px;">
<div style="overflow:visible;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;padding:0;">
<pre style="overflow:visible;line-height:12pt;background-color:white;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> &lt;% <span style="color:#0000ff;">using</span> (Html.BeginForm(<span style="color:#006080;">"Index1"</span>, <span style="color:#006080;">"Home"</span>, FormMethod.Post)) { %&gt;</pre>
<pre style="overflow:visible;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span>     &lt;% <span style="color:#0000ff;">for</span> (<span style="color:#0000ff;">int</span> i = 0; i &lt; 4; i++) {%&gt;</pre>
<pre style="overflow:visible;line-height:12pt;background-color:white;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span>         &lt;%= Html.CheckBox(<span style="color:#006080;">"checks1"</span>, (<span style="color:#0000ff;">object</span>)<span style="color:#0000ff;">new</span> { <span style="color:#0000ff;">value</span> = <span style="color:#006080;">"val"</span> + i.ToString() })%&gt;&lt;%= <span style="color:#006080;">"Check "</span> + i.ToString() %&gt;&lt;br/&gt;</pre>
<pre style="overflow:visible;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   4:</span>     &lt;%}%&gt;</pre>
<pre style="overflow:visible;line-height:12pt;background-color:white;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   5:</span>     &lt;input type=<span style="color:#006080;">"submit"</span> <span style="color:#0000ff;">value</span>=<span style="color:#006080;">"Post!"</span> /&gt;</pre>
<pre style="overflow:visible;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   6:</span> &lt;%}%&gt;</pre>
</div>
</div>
<p>veamos como recibimos los valores de los CheckBoxes si tildamos sólo los dos primeros CheckBoxes (los asociados a los valores ‘val0’ y ‘val1’):</p>
<p><img style="display:block;float:none;margin-left:auto;margin-right:auto;" title="aspnetmvc-checkboxes-valoresChecks1" src="http://gerardocontijoch.files.wordpress.com/2009/06/aspnetmvccheckboxesvaloreschecks1.png?w=443&#038;h=153" alt="aspnetmvc-checkboxes-valoresChecks1" width="443" height="153" /></p>
<p>Como se puede apreciar, lo que recibimos es un array de valores un tanto desconcertante. Esto se debe a la manera en que postea el valor de los CheckBoxes.</p>
<h4>Posteo de Checkboxes</h4>
<p><em>Un CheckBox no es más que un input de tipo <strong>checkbox</strong></em> en una página, el cual puede tener o no un valor asociado. A diferencia de como funciona un input de tipo <strong>text</strong> (un TextBox), <strong><em>el valor del checkbox sólo se postea si el mismo esta tildado</em></strong>. Esto significa que si en un form HTML no tildamos un CheckBox, su valor no será enviado al servidor, lo cual puede traer muchos problemas en ASP.NET MVC si nuestra acción (la asociada al form que posteamos) tiene que recibir como parámetro el valor del CheckBox, ya que la llamada fallará cuando este no este tildado y el <strong>DefaultModelBinder </strong>no pueda asignarle un valor al parámetro asociado al mismo (debido a que no se posteó).</p>
<p>Es por este problema que el <em>método de extensión CheckBox renderiza dos inputs</em> en vez de uno solo. El primer input es el propio CheckBox (un imput de tipo &#8216;checkbox&#8217;), y el segundo es un input de tipo <strong>hidden</strong> llamado igual, pero cuyo valor esta hardcodeado a ‘false’. Es decir, dado el siguiente código:</p>
<div style="border:1px solid gray;overflow:auto;line-height:12pt;background-color:#f4f4f4;width:97.5%;font-family:consolas,'Courier New',courier,monospace;max-height:200px;font-size:8pt;cursor:text;margin:20px 0 10px;padding:4px;">
<div style="overflow:visible;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;padding:0;">
<pre style="overflow:visible;line-height:12pt;background-color:white;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> &lt;%= Html.CheckBox(<span style="color:#006080;">"checkbox"<font color="#000000">) </font></span>%&gt;<span style="color:#006080;">"Check"</span></pre>
</div>
</div>
<p>se renderiza lo siguiente en la página:</p>
<div style="border:1px solid gray;overflow:auto;line-height:12pt;background-color:#f4f4f4;width:97.5%;font-family:consolas,'Courier New',courier,monospace;max-height:200px;font-size:8pt;cursor:text;margin:20px 0 10px;padding:4px;">
<div style="overflow:visible;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;padding:0;">
<pre style="overflow:visible;line-height:12pt;background-color:white;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">input</span> <span style="color:#ff0000;">id</span><span style="color:#0000ff;">="checkbox"</span> <span style="color:#ff0000;">type</span><span style="color:#0000ff;">="checkbox"</span> <span style="color:#ff0000;">value</span><span style="color:#0000ff;">="true"</span> <span style="color:#ff0000;">name</span><span style="color:#0000ff;">="checkbox"</span><span style="color:#0000ff;">/&gt;</span></pre>
<pre style="overflow:visible;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span> <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">input</span> <span style="color:#ff0000;">type</span><span style="color:#0000ff;">="hidden"</span> <span style="color:#ff0000;">value</span><span style="color:#0000ff;">="false"</span> <span style="color:#ff0000;">name</span><span style="color:#0000ff;">="checkbox"</span><span style="color:#0000ff;">/&gt;</span></pre>
<pre style="overflow:visible;line-height:12pt;background-color:white;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span> Check</pre>
<pre style="overflow:visible;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   4:</span> <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">br</span><span style="color:#0000ff;">/&gt;</span></pre>
</div>
</div>
<p>Así, si posteamos el form sin tildar el CheckBox, se postea el valor del input oculto, es decir, para la variable del post ‘checkbox’ se asigna el valor ‘<em>false</em>’ (que en el servidor es transformado en el booleano False). En cambio, si tildamos el CheckBox, se postea el valor ‘<em>true, false</em>’ (uno por cada input). Este valor es procesado por el <strong>DefaultModelBinder </strong>y lo interpreta como el booleano True.</p>
<h4>Posteo de más de un CheckBox</h4>
<p>Hay que reconocer que es un método bastante ingenioso de resolver el problema, pero lamentablemente <em>está pensado para funcionar sólo con un único CheckBox</em>. Volviendo al ejemplo que presenté al comienzo de este post, se puede ver ahora que el array que recibimos en nuestra acción esta conformado por los valores asociados a cada uno de los CheckBoxes renderizados, esto es, se posteo ‘val0, false’ para el primer CheckBox, ‘val1, false’ para el segundo, ‘false’ para el tercero, y ‘false’ para el cuarto (recordemos que los dos últimos CheckBoxes no fueron tildados). Esta forma de recibir los parámetro puede ser bastante problemática. Por un lado, no podemos procesar los valores con un <em>foreach </em>ya que no todos los valores nos interesan, sólo aquellos que son distintos a ‘<em>false</em>’ nos resultan útiles. Uno podría discriminar estos valores para recuperar sólo el set que nos interesa, pero esto es posible únicamente si no posteamos valores booleanos ya que si efectivamente queremos un valor ‘false’ cuando no tildamos un CheckBox, no vamos a poder ubicarlo fácilmente dentro del array. Es verdad que uno puede imaginarse una lógica un tanto compleja en donde si se encuentra un valor ‘true’, entonces ignora el siguiente valor (que debería ser ‘false’), pero esto tiene una utilidad limitada, ya que muy posiblemente nos topemos con el segundo de los problemas que mencioné anteriormente.</p>
<p>Este segundo problema es similar al del posteo de los valores, pero se presenta a la hora de mostrarlos a los valores. Cuando posteamos un form en ASP.NET MVC los parámetros de las acciones se cargan en el <strong>ModelState</strong>, lo cual nos permite, al momento de renderizar la vista inicializar los valores de los controles con los valores posteados para que los mismos no aparezcan vacíos (recordemos que la web es <em>stateless </em>y luego de un post las páginas se vuelven a cargar desde cero). El modo de hacerlo es matcheando los nombres de los controles (atributo <strong>name</strong>) con las entradas del <strong>ModelState</strong>. Así, si tenemos un TextBox llamado ‘txtNombre’, ASP.NET MVC va a crear una entrada dentro del <strong>ModelState </strong>con el nombre ‘txtNombre’ asociado al valor posteado en el TextBox. Al momento de renderizar la página se consulta el <strong>ModelState </strong>y se cargan los valores posteados con anterioridad. Particularmente en nuestro caso, lo que se carga en el <strong>ModelState </strong>es un arrays de valores, el cual <em>esta asociado a todos los CheckBoxes en el form</em>. Y eso es un problema ya el método <strong>CheckBox()</strong> no esta preparado para procesar un array de valores, sino un único valor que determina si un CheckBox esta o no tildado (ese código puede verse claramente cerca de la línea 153 de la clase <strong>InputExtensions </strong>en el código fuente de ASP.NET MVC). Como consecuencia de esto, <em>si tenemos más de un CheckBox con el mismo nombre en el form, los mismos no van a recuperar su estado anterior</em>.</p>
<h4>Un CheckBoxList que funciona</h4>
<p>Dado ese problema, decidí crear un nuevo <strong>CheckBoxList </strong>personalizado (hay un par de implementaciones dando vueltas, pero no encontré ninguna que resolviera el segundo problema).</p>
<p>El código es bastante sencillo y esta ‘inspirado’ en el código del <strong>CheckBox </strong>original de ASP.NET MVC.</p>
<div style="border:1px solid gray;overflow:auto;line-height:12pt;background-color:#f4f4f4;width:97.5%;font-family:consolas,'Courier New',courier,monospace;max-height:1400px;font-size:8pt;cursor:text;margin:20px 0 10px;padding:4px;">
<div style="overflow:visible;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;padding:0;">
<pre style="overflow:visible;line-height:12pt;background-color:white;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">partial</span> <span style="color:#0000ff;">class</span> HtmlHelperExtensions {</pre>
<pre style="overflow:visible;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span></pre>
<pre style="overflow:visible;line-height:12pt;background-color:white;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span>     <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">string</span> CheckBoxList(<span style="color:#0000ff;">this</span> HtmlHelper htmlHelper, <span style="color:#0000ff;">string</span> name, IEnumerable&lt;<span style="color:#0000ff;">string</span>&gt; values, <span style="color:#0000ff;">object</span> htmlAttributes) {</pre>
<pre style="overflow:visible;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   4:</span>         <span style="color:#0000ff;">return</span> CheckBoxList(htmlHelper, name, values, values, htmlAttributes);</pre>
<pre style="overflow:visible;line-height:12pt;background-color:white;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   5:</span>     }</pre>
<pre style="overflow:visible;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   6:</span></pre>
<pre style="overflow:visible;line-height:12pt;background-color:white;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   7:</span>     <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">string</span> CheckBoxList(<span style="color:#0000ff;">this</span> HtmlHelper htmlHelper, <span style="color:#0000ff;">string</span> name, IEnumerable&lt;<span style="color:#0000ff;">string</span>&gt; values, IEnumerable&lt;<span style="color:#0000ff;">string</span>&gt; labels, <span style="color:#0000ff;">object</span> htmlAttributes) {</pre>
<pre style="overflow:visible;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   8:</span>         <span style="color:#008000;">// No creamos ningun CheckBox si no hay valores</span></pre>
<pre style="overflow:visible;line-height:12pt;background-color:white;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   9:</span>         <span style="color:#0000ff;">if</span> (values == <span style="color:#0000ff;">null</span>) {</pre>
<pre style="overflow:visible;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  10:</span>             <span style="color:#0000ff;">return</span> <span style="color:#006080;">""</span>;</pre>
<pre style="overflow:visible;line-height:12pt;background-color:white;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  11:</span>         }</pre>
<pre style="overflow:visible;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  12:</span></pre>
<pre style="overflow:visible;line-height:12pt;background-color:white;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  13:</span>         <span style="color:#0000ff;">if</span> (labels == <span style="color:#0000ff;">null</span>) {</pre>
<pre style="overflow:visible;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  14:</span>             labels = <span style="color:#0000ff;">new</span> List&lt;<span style="color:#0000ff;">string</span>&gt;();</pre>
<pre style="overflow:visible;line-height:12pt;background-color:white;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  15:</span>         }</pre>
<pre style="overflow:visible;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  16:</span></pre>
<pre style="overflow:visible;line-height:12pt;background-color:white;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  17:</span>         RouteValueDictionary attributes = htmlAttributes == <span style="color:#0000ff;">null</span> ? <span style="color:#0000ff;">new</span> RouteValueDictionary() : <span style="color:#0000ff;">new</span> RouteValueDictionary(htmlAttributes);</pre>
<pre style="overflow:visible;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  18:</span>         attributes.Remove(<span style="color:#006080;">"checked"</span>);</pre>
<pre style="overflow:visible;line-height:12pt;background-color:white;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  19:</span></pre>
<pre style="overflow:visible;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  20:</span>         StringBuilder sb = <span style="color:#0000ff;">new</span> StringBuilder();</pre>
<pre style="overflow:visible;line-height:12pt;background-color:white;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  21:</span></pre>
<pre style="overflow:visible;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  22:</span>         <span style="color:#0000ff;">string</span>[] modelValues = <span style="color:#0000ff;">new</span> <span style="color:#0000ff;">string</span>[] { };</pre>
<pre style="overflow:visible;line-height:12pt;background-color:white;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  23:</span></pre>
<pre style="overflow:visible;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  24:</span>         ModelState modelState;</pre>
<pre style="overflow:visible;line-height:12pt;background-color:white;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  25:</span>         <span style="color:#0000ff;">if</span> (htmlHelper.ViewData.ModelState.TryGetValue(name, <span style="color:#0000ff;">out</span> modelState)) {</pre>
<pre style="overflow:visible;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  26:</span>             modelValues = ((<span style="color:#0000ff;">string</span>[])modelState.Value.RawValue);</pre>
<pre style="overflow:visible;line-height:12pt;background-color:white;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  27:</span>         }</pre>
<pre style="overflow:visible;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  28:</span></pre>
<pre style="overflow:visible;line-height:12pt;background-color:white;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  29:</span>         <span style="color:#008000;">// Por cada valor pasado generamos un CheckBox</span></pre>
<pre style="overflow:visible;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  30:</span></pre>
<pre style="overflow:visible;line-height:12pt;background-color:white;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  31:</span>         IEnumerator&lt;<span style="color:#0000ff;">string</span>&gt; labelEnumerator = labels.GetEnumerator();</pre>
<pre style="overflow:visible;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  32:</span>         <span style="color:#0000ff;">foreach</span> (<span style="color:#0000ff;">string</span> s <span style="color:#0000ff;">in</span> values) {</pre>
<pre style="overflow:visible;line-height:12pt;background-color:white;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  33:</span>             <span style="color:#008000;">// Si el array contiene el valor correspondiente a este checkbox, entonces fue chequeado</span></pre>
<pre style="overflow:visible;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  34:</span>             <span style="color:#0000ff;">bool</span> isChecked = modelValues.Contains(s);</pre>
<pre style="overflow:visible;line-height:12pt;background-color:white;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  35:</span>             sb.Append(CrearCheckBox(name, s, isChecked, attributes));</pre>
<pre style="overflow:visible;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  36:</span></pre>
<pre style="overflow:visible;line-height:12pt;background-color:white;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  37:</span>             labelEnumerator.MoveNext();</pre>
<pre style="overflow:visible;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  38:</span>             <span style="color:#0000ff;">if</span> (labelEnumerator.Current != <span style="color:#0000ff;">null</span>) {</pre>
<pre style="overflow:visible;line-height:12pt;background-color:white;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  39:</span>                 sb.AppendLine(labelEnumerator.Current);</pre>
<pre style="overflow:visible;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  40:</span>             }</pre>
<pre style="overflow:visible;line-height:12pt;background-color:white;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  41:</span>         }</pre>
<pre style="overflow:visible;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  42:</span></pre>
<pre style="overflow:visible;line-height:12pt;background-color:white;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  43:</span>         <span style="color:#008000;">// Creamos el div contenedor</span></pre>
<pre style="overflow:visible;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  44:</span>         TagBuilder divTag = <span style="color:#0000ff;">new</span> TagBuilder(<span style="color:#006080;">"div"</span>);</pre>
<pre style="overflow:visible;line-height:12pt;background-color:white;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  45:</span>         divTag.InnerHtml = sb.ToString();</pre>
<pre style="overflow:visible;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  46:</span></pre>
<pre style="overflow:visible;line-height:12pt;background-color:white;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  47:</span>         <span style="color:#008000;">// No nos olvidemos de indicar si hay un error en alguno de los checks</span></pre>
<pre style="overflow:visible;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  48:</span>         <span style="color:#0000ff;">if</span> (modelState != <span style="color:#0000ff;">null</span> &amp;&amp; modelState.Errors.Count &gt; 0) {</pre>
<pre style="overflow:visible;line-height:12pt;background-color:white;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  49:</span>             divTag.AddCssClass(HtmlHelper.ValidationInputCssClassName);</pre>
<pre style="overflow:visible;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  50:</span>         }</pre>
<pre style="overflow:visible;line-height:12pt;background-color:white;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  51:</span></pre>
<pre style="overflow:visible;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  52:</span>         <span style="color:#0000ff;">return</span> divTag.ToString(TagRenderMode.Normal);</pre>
<pre style="overflow:visible;line-height:12pt;background-color:white;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  53:</span>     }</pre>
<pre style="overflow:visible;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  54:</span></pre>
<pre style="overflow:visible;line-height:12pt;background-color:white;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  55:</span>     <span style="color:#0000ff;">private</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">string</span> CrearCheckBox(<span style="color:#0000ff;">string</span> name, <span style="color:#0000ff;">string</span> <span style="color:#0000ff;">value</span>, <span style="color:#0000ff;">bool</span> isChecked, IDictionary&lt;<span style="color:#0000ff;">string</span>, <span style="color:#0000ff;">object</span>&gt; htmlAttributes) {</pre>
<pre style="overflow:visible;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  56:</span>         TagBuilder tagBuilder = <span style="color:#0000ff;">new</span> TagBuilder(<span style="color:#006080;">"input"</span>);</pre>
<pre style="overflow:visible;line-height:12pt;background-color:white;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  57:</span>         tagBuilder.MergeAttributes(htmlAttributes);</pre>
<pre style="overflow:visible;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  58:</span>         tagBuilder.MergeAttribute(<span style="color:#006080;">"type"</span>, <span style="color:#006080;">"checkbox"</span>);</pre>
<pre style="overflow:visible;line-height:12pt;background-color:white;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  59:</span>         tagBuilder.MergeAttribute(<span style="color:#006080;">"name"</span>, name, <span style="color:#0000ff;">true</span>);</pre>
<pre style="overflow:visible;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  60:</span></pre>
<pre style="overflow:visible;line-height:12pt;background-color:white;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  61:</span>         tagBuilder.GenerateId(name);</pre>
<pre style="overflow:visible;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  62:</span></pre>
<pre style="overflow:visible;line-height:12pt;background-color:white;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  63:</span>         <span style="color:#0000ff;">if</span> (isChecked) {</pre>
<pre style="overflow:visible;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  64:</span>             tagBuilder.MergeAttribute(<span style="color:#006080;">"checked"</span>, <span style="color:#006080;">"checked"</span>);</pre>
<pre style="overflow:visible;line-height:12pt;background-color:white;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  65:</span>         }</pre>
<pre style="overflow:visible;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  66:</span></pre>
<pre style="overflow:visible;line-height:12pt;background-color:white;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  67:</span>         <span style="color:#0000ff;">if</span> (<span style="color:#0000ff;">value</span> != <span style="color:#0000ff;">null</span>) {</pre>
<pre style="overflow:visible;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  68:</span>             tagBuilder.MergeAttribute(<span style="color:#006080;">"value"</span>, <span style="color:#0000ff;">value</span>, <span style="color:#0000ff;">true</span>);</pre>
<pre style="overflow:visible;line-height:12pt;background-color:white;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  69:</span>         }</pre>
<pre style="overflow:visible;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  70:</span></pre>
<pre style="overflow:visible;line-height:12pt;background-color:white;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  71:</span>         <span style="color:#0000ff;">return</span> tagBuilder.ToString(TagRenderMode.SelfClosing);</pre>
<pre style="overflow:visible;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  72:</span>     }</pre>
<pre style="overflow:visible;line-height:12pt;background-color:white;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  73:</span> }</pre>
</div>
</div>
<p>Su uso es muy sencillo:</p>
<div style="border:1px solid gray;overflow:auto;line-height:12pt;background-color:#f4f4f4;width:97.5%;font-family:consolas,'Courier New',courier,monospace;max-height:200px;font-size:8pt;cursor:text;margin:20px 0 10px;padding:4px;">
<div style="overflow:visible;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;padding:0;">
<pre style="overflow:visible;line-height:12pt;background-color:white;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> &lt;%</pre>
<pre style="overflow:visible;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span>    List&lt;<span style="color:#0000ff;">string</span>&gt; values = <span style="color:#0000ff;">new</span> List&lt;<span style="color:#0000ff;">string</span>&gt;() { <span style="color:#006080;">"val0"</span>, <span style="color:#006080;">"val1"</span>, <span style="color:#006080;">"val2"</span>, <span style="color:#006080;">"val3"</span>, <span style="color:#006080;">"val4"</span> };</pre>
<pre style="overflow:visible;line-height:12pt;background-color:white;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span>    List&lt;<span style="color:#0000ff;">string</span>&gt; labels = <span style="color:#0000ff;">new</span> List&lt;<span style="color:#0000ff;">string</span>&gt;() { <span style="color:#006080;">"Check 0"</span>, <span style="color:#006080;">"Check 1"</span>, <span style="color:#006080;">"Check 2"</span>, <span style="color:#006080;">"Check 3"</span>, <span style="color:#006080;">"Check 4"</span> };</pre>
<pre style="overflow:visible;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   4:</span> %&gt;</pre>
<pre style="overflow:visible;line-height:12pt;background-color:white;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   5:</span> &lt;%= Html.CheckBoxList(<span style="color:#006080;">"checks3"</span>, values, labels, <span style="color:#0000ff;">null</span>) %&gt;</pre>
</div>
</div>
<p>Los valores en la acción son recibidos así:</p>
<p><img style="display:block;float:none;margin-left:auto;margin-right:auto;" title="aspnetmvc-checkboxes-valoresChecks2" src="http://gerardocontijoch.files.wordpress.com/2009/06/aspnetmvccheckboxesvaloreschecks2.png?w=462&#038;h=127" alt="aspnetmvc-checkboxes-valoresChecks2" width="462" height="127" /></p>
<p>El código podría mejorarse un poco más agregando la posibilidad de aplicarles estilos al div contenedor de los CheckBoxes, usar labels reales en vez de solo texto, agregar sobrecargas a los métodos, etc., pero preferí dejarlo así porque era más sencillo. Es preciso aclarar que este <strong>CheckBoxList</strong>, al igual que la primer alternativa que presenté, no va a funcionar bien con valores booleanos ya que la idea es que los valores asociados a cada uno de los CheckBoxes sean únicos.</p>
<p>Si encuentran algún bug o tienen algo que aportar, no duden en comentarlo.</p>
<p>¡Nos vemos en el próximo post!</p>
<p><em>Publicado originalmente en <a title="http://gerardocontijoch.wordpress.com" href="http://gerardocontijoch.wordpress.com/" target="_blank">http://gerardocontijoch.wordpress.com</a>.</em></p>
<br />Posted in ASP.NET MVC, Desarrollo Web Tagged: checkbox, control, extension-methods, modelstate <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/gerardocontijoch.wordpress.com/336/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/gerardocontijoch.wordpress.com/336/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/gerardocontijoch.wordpress.com/336/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/gerardocontijoch.wordpress.com/336/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/gerardocontijoch.wordpress.com/336/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/gerardocontijoch.wordpress.com/336/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/gerardocontijoch.wordpress.com/336/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/gerardocontijoch.wordpress.com/336/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/gerardocontijoch.wordpress.com/336/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/gerardocontijoch.wordpress.com/336/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/gerardocontijoch.wordpress.com/336/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/gerardocontijoch.wordpress.com/336/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/gerardocontijoch.wordpress.com/336/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/gerardocontijoch.wordpress.com/336/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gerardocontijoch.wordpress.com&amp;blog=5700517&amp;post=336&amp;subd=gerardocontijoch&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://gerardocontijoch.wordpress.com/2009/07/04/un-checkboxlist-que-funciona-en-asp-net-mvc/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/08a523d4aac4f7ad6ff4eb1cb6ac85e2?s=96&#38;d=http%3A%2F%2Fs0.wp.com%2Fi%2Fmu.gif&#38;r=G" medium="image">
			<media:title type="html">Gerardo Contijoch</media:title>
		</media:content>

		<media:content url="http://gerardocontijoch.files.wordpress.com/2009/06/aspnetmvccheckboxesvaloreschecks1.png" medium="image">
			<media:title type="html">aspnetmvc-checkboxes-valoresChecks1</media:title>
		</media:content>

		<media:content url="http://gerardocontijoch.files.wordpress.com/2009/06/aspnetmvccheckboxesvaloreschecks2.png" medium="image">
			<media:title type="html">aspnetmvc-checkboxes-valoresChecks2</media:title>
		</media:content>
	</item>
		<item>
		<title>Investigando los eventos de la clase HttpApplication en ASP.NET</title>
		<link>http://gerardocontijoch.wordpress.com/2009/06/27/investigando-los-eventos-de-la-clase-httpapplication-en-asp-net/</link>
		<comments>http://gerardocontijoch.wordpress.com/2009/06/27/investigando-los-eventos-de-la-clase-httpapplication-en-asp-net/#comments</comments>
		<pubDate>Sat, 27 Jun 2009 20:30:52 +0000</pubDate>
		<dc:creator>Gerardo Contijoch</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[Desarrollo Web]]></category>
		<category><![CDATA[eventos]]></category>
		<category><![CDATA[httpapplication]]></category>
		<category><![CDATA[httpmodule]]></category>
		<category><![CDATA[web.config]]></category>

		<guid isPermaLink="false">http://gerardocontijoch.wordpress.com/?p=340</guid>
		<description><![CDATA[Hace unos días me encontré con este interesantísimo post de Rick Strahl en donde responde a una pregunta que alguien le hizo. La pregunta en cuestión es ¿Cómo es que los handlers de los eventos Application_ en ASP.NET son enlazados para que sean invocados automáticamente? Es una pregunta interesante ya que normalmente uno enlaza a [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gerardocontijoch.wordpress.com&amp;blog=5700517&amp;post=340&amp;subd=gerardocontijoch&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Hace unos días me encontré con <a href="http://west-wind.com/weblog/posts/801489.aspx" target="_blank">este</a> interesantísimo post de <a href="http://west-wind.com/Weblog/default.aspx" target="_blank">Rick Strahl</a> en donde responde a una pregunta que alguien le hizo. La pregunta en cuestión es <strong><em>¿Cómo es que los handlers de los eventos Application_ en ASP.NET son enlazados para que sean invocados automáticamente?</em></strong></p>
<p>Es una pregunta interesante ya que normalmente uno enlaza a mano los eventos que desea capturar, pero en el caso de los eventos de la clase HttpApplication, la situación es diferente porque no podemos enlazarlos nosotros mismos (en realidad si podemos, pero no tiene ningún efecto). Veamos como es el asunto.</p>
<p>Cuando agregamos un archivo <em>Global.asax</em> a nuestro proyecto vemos que la clase <strong>Global</strong> ya posee algunos handlers creados:</p>
<div style="border:1px solid gray;overflow:auto;line-height:12pt;background-color:#f4f4f4;width:97.5%;font-family:consolas,'Courier New',courier,monospace;max-height:600px;font-size:8pt;cursor:text;margin:20px 0 10px;padding:4px;">
<div style="overflow:visible;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;padding:0;">
<pre style="overflow:visible;line-height:12pt;background-color:white;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">class</span> Global : System.Web.HttpApplication {</pre>
<pre style="overflow:visible;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span></pre>
<pre style="overflow:visible;line-height:12pt;background-color:white;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span>     <span style="color:#0000ff;">protected</span> <span style="color:#0000ff;">void</span> Application_Start(<span style="color:#0000ff;">object</span> sender, EventArgs e) { }</pre>
<pre style="overflow:visible;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   4:</span></pre>
<pre style="overflow:visible;line-height:12pt;background-color:white;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   5:</span>     <span style="color:#0000ff;">protected</span> <span style="color:#0000ff;">void</span> Session_Start(<span style="color:#0000ff;">object</span> sender, EventArgs e) { }</pre>
<pre style="overflow:visible;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   6:</span></pre>
<pre style="overflow:visible;line-height:12pt;background-color:white;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   7:</span>     <span style="color:#0000ff;">protected</span> <span style="color:#0000ff;">void</span> Application_BeginRequest(<span style="color:#0000ff;">object</span> sender, EventArgs e) { }</pre>
<pre style="overflow:visible;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   8:</span></pre>
<pre style="overflow:visible;line-height:12pt;background-color:white;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   9:</span>     <span style="color:#0000ff;">protected</span> <span style="color:#0000ff;">void</span> Application_AuthenticateRequest(<span style="color:#0000ff;">object</span> sender, EventArgs e) { }</pre>
<pre style="overflow:visible;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  10:</span></pre>
<pre style="overflow:visible;line-height:12pt;background-color:white;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  11:</span>     <span style="color:#0000ff;">protected</span> <span style="color:#0000ff;">void</span> Application_Error(<span style="color:#0000ff;">object</span> sender, EventArgs e) { }</pre>
<pre style="overflow:visible;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  12:</span></pre>
<pre style="overflow:visible;line-height:12pt;background-color:white;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  13:</span>     <span style="color:#0000ff;">protected</span> <span style="color:#0000ff;">void</span> Session_End(<span style="color:#0000ff;">object</span> sender, EventArgs e) { }</pre>
<pre style="overflow:visible;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  14:</span></pre>
<pre style="overflow:visible;line-height:12pt;background-color:white;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  15:</span>     <span style="color:#0000ff;">protected</span> <span style="color:#0000ff;">void</span> Application_End(<span style="color:#0000ff;">object</span> sender, EventArgs e) { }</pre>
<pre style="overflow:visible;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  16:</span> }</pre>
</div>
</div>
<p>Lógicamente, al handler <strong>Application_Start</strong> nunca vamos a poder enlazarlo nosotros mismos ya que el mismo <em>es ejecutado cuando la aplicación se inicia</em>, pero el resto parecería que si podría ser enlazado debido a que ocurre luego de la inicialización de la aplicación. Bueno, no es tan así.</p>
<p>Para empezar, <strong>Application_Start</strong>, <strong>Application_End</strong> y <strong>Session_End</strong> parecen handlers pero en realidad <em>no existe ningún evento asociado a los mismos</em>, por lo que no es posible enlazarlos de otra manera. La clase <strong>HttpApplicationFactory</strong> es la encargada de simular estos eventos con los métodos <strong>FireApplicationOnStart()</strong>, <strong>FireApplicationOnEnd()</strong> y <strong>FireSessionOnEnd()</strong>.</p>
<p>Segundo, el resto de los handlers no son mas que <em>accesos directos a los eventos de distintos módulos</em> (clases que implementan IHttpModule) que pueden ser cargados. Estos módulos son configurados en el archivo web.config en la seccion &lt;httpModules&gt;. Por ejemplo, uno de los que se carga por defecto es el llamado Session (de tipo <strong>System.Web.SessionState.SessionStateModule</strong>). El mismo es el encargado de mantener una sesión entre distintos requests y su carga esta definida en el archivo web.config que se encuentra en <em>%WIN_DIR%\Microsoft.NET\Framework\[VERSION_FRAMEWORK]\CONFIG\</em> (este web.config se carga para todas las aplicaciones que se corran en nuestro equipo). Si en el web.config de nuestra aplicación agregamos la siguiente línea a la sección &lt;httpModules&gt;:</p>
<div style="border:1px solid gray;overflow:auto;line-height:12pt;background-color:#f4f4f4;width:97.5%;font-family:consolas,'Courier New',courier,monospace;max-height:200px;font-size:8pt;cursor:text;margin:20px 0 10px;padding:4px;">
<div style="overflow:visible;line-height:12pt;background-color:#f4f4f4;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;padding:0;">
<pre style="overflow:visible;line-height:12pt;background-color:white;width:100%;font-family:consolas,'Courier New',courier,monospace;color:black;font-size:8pt;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> &lt;remove name=<span style="color:#006080;">"Session"</span>/&gt;</pre>
</div>
</div>
<p>veremos que el handler <strong>Session_Start</strong> no se ejecuta más, ya que no hay ningún módulo llamado <em>Session</em> que provoque un evento <em>Start</em> (y como consecuencia, nuestra aplicación ya no tiene soporte para sesiones). Como no tenemos acceso a esos módulos sino hasta que la aplicación ya haya sido inicializada, no hay manera de enlazar esos eventos con los handlers y por eso necesitamos estos <em>accesos directos</em> que nos provee la clase HttpApplication.</p>
<p>Como se puede ver en el <a href="http://west-wind.com/weblog/posts/801489.aspx" target="_blank">artículo original</a> de Rick Strahl, el enlace ocurre en un método llamado <strong>HookupEventHandlersForApplicationAndModules()</strong> (con ese nombre, ¿a alguien le queda alguna duda de ello?). Resumiendo la funcionalidad del método, básicamente lo que sucede es que, dado un array de objetos <strong>MethodInfo</strong> (son cada uno de los handlers que creamos), se itera sobre ellos y se determina si el evento es de la aplicación o pertenece a algún módulo. Esto se logra buscando el ‘_’ en el nombre del mismo y tomando la primer parte. Luego se guarda una referencia a la instancia de <strong>HttpApplication</strong> o al módulo y se busca via reflection el evento en base al nombre del método (el evento ‘<em>Start</em>’ se asocia al método terminado en ‘<em>Start</em>’, por ejemplo). Un detalle curioso es que esta lógica reconoce a ‘<em>Application_BeginRequest</em>’ y a ‘<em>Application_OnBeginRequest</em>’ como lo mismo, teniendo precedencia el primero si es que se encuentran ambos presentes. Luego se valida que el handler tenga los parámetros correctos y si no es así, se genera un proxy para el mismo (lo que significa que nuestros handlers pueden no aceptar ningún parámetro y van a a ser igual de válidos). Finalmente después de obtener una referencia al evento correcto, se invoca al método <strong>Add</strong> del mismo para asociar nuestra instancia de <strong>MethodInfo</strong> (o el proxy que mencioné hace un momento) al evento y de este modo, cuando ocurra, se ejecuta nuestro handler.</p>
<p>¿Muy complicado? Si, seguro, la clase <strong>HttpApplication </strong>es una de las más complejas de .NET Framework, pero eso es lo que la hace interesante, ¿no?</p>
<p>¡Nos vemos en el próximo post!</p>
<p><em>Publicado originalmente en <a title="http://gerardocontijoch.wordpress.com" href="http://gerardocontijoch.wordpress.com/" target="_blank">http://gerardocontijoch.wordpress.com</a>.</em></p>
<br />Posted in ASP.NET, Desarrollo Web Tagged: eventos, httpapplication, httpmodule, web.config <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/gerardocontijoch.wordpress.com/340/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/gerardocontijoch.wordpress.com/340/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/gerardocontijoch.wordpress.com/340/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/gerardocontijoch.wordpress.com/340/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/gerardocontijoch.wordpress.com/340/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/gerardocontijoch.wordpress.com/340/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/gerardocontijoch.wordpress.com/340/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/gerardocontijoch.wordpress.com/340/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/gerardocontijoch.wordpress.com/340/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/gerardocontijoch.wordpress.com/340/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/gerardocontijoch.wordpress.com/340/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/gerardocontijoch.wordpress.com/340/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/gerardocontijoch.wordpress.com/340/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/gerardocontijoch.wordpress.com/340/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gerardocontijoch.wordpress.com&amp;blog=5700517&amp;post=340&amp;subd=gerardocontijoch&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://gerardocontijoch.wordpress.com/2009/06/27/investigando-los-eventos-de-la-clase-httpapplication-en-asp-net/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/08a523d4aac4f7ad6ff4eb1cb6ac85e2?s=96&#38;d=http%3A%2F%2Fs0.wp.com%2Fi%2Fmu.gif&#38;r=G" medium="image">
			<media:title type="html">Gerardo Contijoch</media:title>
		</media:content>
	</item>
		<item>
		<title>Inicializaci&#243;n del ModelState en ASP.NET MVC</title>
		<link>http://gerardocontijoch.wordpress.com/2009/06/21/inicializacin-del-modelstate-en-asp-net-mvc/</link>
		<comments>http://gerardocontijoch.wordpress.com/2009/06/21/inicializacin-del-modelstate-en-asp-net-mvc/#comments</comments>
		<pubDate>Mon, 22 Jun 2009 03:28:18 +0000</pubDate>
		<dc:creator>Gerardo Contijoch</dc:creator>
				<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[modelbinder]]></category>
		<category><![CDATA[modelstate]]></category>

		<guid isPermaLink="false">http://gerardocontijoch.wordpress.com/?p=332</guid>
		<description><![CDATA[Hoy mientras trabajaba en un site ASP.NET MVC aprendí una lección que espero no olvidar jamás (su olvido me podría traer muchos dolores de cabeza). El problema se presentó cuando uno de los campos del form con el que estaba trabajando no se actualizaba luego de un postback. El form no tenia nada de especial, [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gerardocontijoch.wordpress.com&amp;blog=5700517&amp;post=332&amp;subd=gerardocontijoch&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Hoy mientras trabajaba en un site ASP.NET MVC aprendí una lección que espero no olvidar jamás (su olvido me podría traer muchos dolores de cabeza).</p>
<p>El problema se presentó cuando uno de los campos del form con el que estaba trabajando no se actualizaba luego de un postback. El form no tenia nada de especial, sólo un par de campos, y su posteo provocaba la ejecución de una acción que tampoco hacia nada muy loco. Lo único destacable era que el parámetro con problemas era cargado con un ModelBinder personalizado, pero el mismo funcionaba bien y lo inicializaba correctamente al valor. Lo primero que hice fue verificar que las variables del post lleguen correctamente al server. También verifiqué que no haya ningún código javascript interfiriendo con el llenado de campos. Lo que más me desconcertó fue que dentro de la acción, el parámetro cuyo valor tenia problemas estaba correctamente inicializado, cosa que no esperaba (lo que me llevó a pensar que no había un error en el ModelBinder). Sin embargo había un detalle que pasé de largo: entre los valores del ModelState del request no se encontraba el parámetro con problemas. Fue cuando descubrí eso que supe donde estaba el problema. Un poco más de investigación me confirmó las sospechas. Pero antes de explicarles el problema, veamos el código del ModelBinder:</p>
<div style="border:1px solid gray;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:500px;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;margin:20px 0 10px;padding:4px;">
<div style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;padding:0;">
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">class</span> TipoDeOperacionModelBinder : IModelBinder {</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span>     <span style="color:#cc6633;">#region</span> IModelBinder Members</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   4:</span>     <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">object</span> BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) {</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   5:</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   6:</span>         <span style="color:#0000ff;">if</span> (bindingContext == <span style="color:#0000ff;">null</span>) {</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   7:</span>             <span style="color:#0000ff;">throw</span> <span style="color:#0000ff;">new</span> ArgumentNullException(<span style="color:#006080;">"bindingContext"</span>);</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   8:</span>         }</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   9:</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  10:</span>         ValueProviderResult val;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  11:</span>         <span style="color:#0000ff;">if</span> (!bindingContext.ValueProvider.TryGetValue(bindingContext.ModelName, <span style="color:#0000ff;">out</span> val)) {</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  12:</span>             <span style="color:#008000;">// Si no encontramos el valor, devolvemos null y ASP.NET MVC se encarga de provocar la excepción necesaria</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  13:</span>             <span style="color:#0000ff;">return</span> <span style="color:#0000ff;">null</span>;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  14:</span>         }</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  15:</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  16:</span>         <span style="color:#0000ff;">int</span> realVal = (<span style="color:#0000ff;">int</span>)val.ConvertTo(<span style="color:#0000ff;">typeof</span>(<span style="color:#0000ff;">int</span>));</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  17:</span>         <span style="color:#0000ff;">return</span> (TipoDeOperacion)realVal;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  18:</span>     }</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  19:</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  20:</span>     <span style="color:#cc6633;">#endregion</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  21:</span> }</pre>
</div>
</div>
<p>Este ModelBinder simplemente toma un valor (indicado por bindingContext.ModelName) y lo transforma en un Enum de tipo TipoDeOperacion. Cabe aclarar que este ModelBinder esta registrado para el tipo TipoDeOperacion, por lo que el propio framework es el encargado de hacer uso del mismo cuando necesite hacer el binding de un parámetro de tipo TipoDeOperacion.</p>
<p>Como dije antes, el parámetro en cuestión se inicializaba correctamente, lo que confirmaba que las variables del post llegaban sin problemas. El problema estaba en que no se encontraba dentro del ModelState y eso se debe sencillamente a que en ningún lugar del código del ModelBinder lo estoy agregando. Resulta que el ModelBinder no solo es responsable de cargar los parámetros de las acciones y actualizar el modelo con el que estamos trabajando, sino que ¡<em>también debe inicializar el ModelState</em>! El resto de los parámetros de la acción no tenían problemas ya que eran procesados por la clase DefaultModelBinder, la cual se encarga de todo. Todo se resolvió cuando agregué el siguiente código en la línea 15:</p>
<div style="border:1px solid gray;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;margin:20px 0 10px;padding:4px;">
<div style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;padding:0;">
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;">bindingContext.ModelState.SetModelValue(bindingContext.ModelName, val);</pre>
</div>
</div>
<p>Parece una pavada de detalle, pero esa sola línea de código me hizo perder varias horas de trabajo por no saber que el ModelState era cargado por los ModelBinders. Esto demuestra la importancia de entender bien las herramientas y frameworks con los que uno trabaja. ASP.NET MVC nos provee de montones de puntos de extensión, pero de nada sirve si no los sabemos aprovechar correctamente.</p>
<p>¡Nos vemos en el próximo post!</p>
<p><em>Publicado originalmente en <a title="http://gerardocontijoch.wordpress.com" href="http://gerardocontijoch.wordpress.com/" target="_blank">http://gerardocontijoch.wordpress.com</a>.</em></p>
<br />Posted in ASP.NET MVC Tagged: .net, modelbinder, modelstate <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/gerardocontijoch.wordpress.com/332/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/gerardocontijoch.wordpress.com/332/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/gerardocontijoch.wordpress.com/332/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/gerardocontijoch.wordpress.com/332/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/gerardocontijoch.wordpress.com/332/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/gerardocontijoch.wordpress.com/332/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/gerardocontijoch.wordpress.com/332/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/gerardocontijoch.wordpress.com/332/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/gerardocontijoch.wordpress.com/332/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/gerardocontijoch.wordpress.com/332/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/gerardocontijoch.wordpress.com/332/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/gerardocontijoch.wordpress.com/332/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/gerardocontijoch.wordpress.com/332/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/gerardocontijoch.wordpress.com/332/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gerardocontijoch.wordpress.com&amp;blog=5700517&amp;post=332&amp;subd=gerardocontijoch&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://gerardocontijoch.wordpress.com/2009/06/21/inicializacin-del-modelstate-en-asp-net-mvc/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/08a523d4aac4f7ad6ff4eb1cb6ac85e2?s=96&#38;d=http%3A%2F%2Fs0.wp.com%2Fi%2Fmu.gif&#38;r=G" medium="image">
			<media:title type="html">Gerardo Contijoch</media:title>
		</media:content>
	</item>
		<item>
		<title>Generaci&#243;n personalizada de Ids en ASP.NET 4.0</title>
		<link>http://gerardocontijoch.wordpress.com/2009/06/12/generacion-personalizada-de-ids-en-asp-net-4-0/</link>
		<comments>http://gerardocontijoch.wordpress.com/2009/06/12/generacion-personalizada-de-ids-en-asp-net-4-0/#comments</comments>
		<pubDate>Fri, 12 Jun 2009 16:36:15 +0000</pubDate>
		<dc:creator>Gerardo Contijoch</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[Desarrollo Web]]></category>

		<guid isPermaLink="false">http://gerardocontijoch.wordpress.com/?p=329</guid>
		<description><![CDATA[En mi último post les comentaba sobre un gran problema que hay en ASP.NET (por lo menos hasta que salga la 4.0) con respecto a la generación automática de los ids de controles web. Resumidamente, el problema esta en que ASP.NET puede cambiar el id original que le dimos a nuestros controles (por ejemplo, txtNombre [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gerardocontijoch.wordpress.com&amp;blog=5700517&amp;post=329&amp;subd=gerardocontijoch&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>En mi <a href="http://gerardocontijoch.wordpress.com/2009/06/06/acceder-a-variables-de-servidor-desde-javascript/trackback/" target="_blank">último post</a> les comentaba sobre un gran problema que hay en ASP.NET (por lo menos hasta que salga la 4.0) con respecto a la generación automática de los ids de controles web. Resumidamente, el problema esta en que <em>ASP.NET puede cambiar el id original que le dimos a nuestros controles</em> (por ejemplo, txtNombre por “ctl00_ContentPlaceHolder1_txtNombre”), lo cual suele provocar que <em>nuestros scripts en javascript deje de funcionar</em>. Casualmente, unas horas después de publicado el post, me encuentro con <a href="http://www.dotnetcube.com/post.aspx?id=c2410d77-b10e-4933-a3d5-ba49d82064a9" target="_blank">éste artículo</a> en donde se explica como en ASP.NET 4.0 esto deja de ser un problema ya que se permite la personalización del proceso de generación de ids.</p>
<p>Cabe aclarar que el comportamiento que voy a describir a continuación <em>esta basado en ASP.NET 4.0 CTP</em> y no en la versión definitiva (la cual no salió aún), pero imagino que este es uno de los puntos en donde no va a cambiar el funcionamiento. Otro punto a aclarar es que el id se ve modificado sólo del lado del cliente, es decir, el código del lado del servidor no se ve modificado en nada.</p>
<h3>El atributo ClientIDMode</h3>
<p>La personalización del Id ahora se puede realizar mediante un nuevo atributo de los controles web llamado ClientIDMode. Dependiendo de su valor, el id generado para el control que lo aplique puede ser fijo (siempre el mismo), dinámico (basado en datos asociados al control) o automático (es decir, igual que en las versiones anteriores de ASP.NET).</p>
<p>Sus valores posibles son:</p>
<ul>
<li>Legacy</li>
<li>Static</li>
<li>Predictable</li>
<li>Inherit</li>
</ul>
<p>Veamos uno por uno.</p>
<h4>Legacy</h4>
<p>Este valor indica que el comportamiento de ASP.NET al generar el id va a ser<em> igual al de las versiones anteriores</em>, es decir, el id va a tener el siguiente formato: [<strong>IdControlContenedor</strong>]_[<strong>IdControlHijo</strong>].</p>
<h4>Static</h4>
<p>Éste sea posiblemente el valor más utilizado ya que indica que <em>el id generado sea el que definimos nosotros</em> (es decir, que quede estático o fijo) y no se vea modificado por nada. Hay que tener en mente que <em>esto puede traer conflictos de ids</em> si hay un contenedor con un control con el mismo id dentro de este. Por ejemplo:</p>
<div style="border:1px solid gray;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;margin:20px 0 10px;padding:4px;">
<div style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;padding:0;">
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">body</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span>     <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">form</span> <span style="color:#ff0000;">id</span><span style="color:#0000ff;">="form1"</span> <span style="color:#ff0000;">runat</span><span style="color:#0000ff;">="server"</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span>     <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">div</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   4:</span>         <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">asp:TextBox</span> <span style="color:#ff0000;">ID</span><span style="color:#0000ff;">="txtNombre"</span> <span style="color:#ff0000;">runat</span><span style="color:#0000ff;">="server"</span> <span style="color:#ff0000;">ClientIDMode</span><span style="color:#0000ff;">="Static"</span><span style="color:#0000ff;">&gt;&lt;/</span><span style="color:#800000;">asp:TextBox</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   5:</span>         <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">uc1:MiControlWeb</span> <span style="color:#ff0000;">ID</span><span style="color:#0000ff;">="MiControlWeb1"</span> <span style="color:#ff0000;">runat</span><span style="color:#0000ff;">="server"</span> <span style="color:#0000ff;">/&gt;</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   6:</span>     <span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">div</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   7:</span>     <span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">form</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   8:</span> <span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">body</span><span style="color:#0000ff;">&gt;</span></pre>
</div>
</div>
<p>Donde <em>MiControlWeb</em> esta definido así:</p>
<div style="border:1px solid gray;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;margin:20px 0 10px;padding:4px;">
<div style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;padding:0;">
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> <span style="background-color:#ffff00;">&lt;%@ Control Language="C#" AutoEventWireup="true" CodeFile="MiControlWeb.ascx.cs" Inherits="WebUserControl" %&gt;</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span> <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">asp:TextBox</span> <span style="color:#ff0000;">ID</span><span style="color:#0000ff;">="txtNombre"</span> <span style="color:#ff0000;">runat</span><span style="color:#0000ff;">="server"</span> <span style="color:#ff0000;">ClientIDMode</span><span style="color:#0000ff;">="Static"</span><span style="color:#0000ff;">&gt;&lt;/</span><span style="color:#800000;">asp:TextBox</span><span style="color:#0000ff;">&gt;</span></pre>
</div>
</div>
<p>Esto quedaría renderizado así:</p>
<div style="border:1px solid gray;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;margin:20px 0 10px;padding:4px;">
<div style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;padding:0;">
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">div</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span>   <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">input</span> <span style="color:#ff0000;">name</span><span style="color:#0000ff;">="txtNombre"</span> <span style="color:#ff0000;">type</span><span style="color:#0000ff;">="text"</span> <span style="color:#ff0000;">id</span><span style="color:#0000ff;">="txtNombre"</span> <span style="color:#0000ff;">/&gt;</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span>   <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">input</span> <span style="color:#ff0000;">name</span><span style="color:#0000ff;">="MiControlWeb$txtNombre"</span> <span style="color:#ff0000;">type</span><span style="color:#0000ff;">="text"</span> <span style="color:#ff0000;">id</span><span style="color:#0000ff;">="txtNombre"</span> <span style="color:#0000ff;">/&gt;</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   4:</span> <span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">div</span><span style="color:#0000ff;">&gt;</span></pre>
</div>
</div>
<p>Este atributo no debería usarse nunca dentro de un control como Repeater, ya que los ids de los controles contenidos en el serían todos iguales.</p>
<h4>Inherit</h4>
<p>Este es el modo por defecto, y simplemente indica que el valor de <strong>ClientIDMode</strong> se hereda del contenedor donde se encuentre el control (como pueden ser un WebControl, un ContentPlaceHolder – en MasterPages- o una página).</p>
<h4>Predictable</h4>
<p>Este modo de generación produce resultados similares al modo <strong>Legacy</strong> pero, como lo indica su nombre, <em>los valores son predecibles</em>. Es principalmente útil en los controles enlazados a datos. El id en este caso se genera mediante el uso del dato asociado o enlazado al control. Veamos como funciona.</p>
<p>Dado este control:</p>
<div style="border:1px solid gray;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:500px;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;margin:20px 0 10px;padding:4px;">
<div style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;padding:0;">
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">asp:GridView</span> <span style="color:#ff0000;">ID</span><span style="color:#0000ff;">="gvPersonas"</span> <span style="color:#ff0000;">runat</span><span style="color:#0000ff;">="server"</span> <span style="color:#ff0000;">AutoGenerateColumns</span><span style="color:#0000ff;">="false"</span> <span style="color:#ff0000;">ClientIDMode</span><span style="color:#0000ff;">="Predictable"</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span> <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">Columns</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span>     <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">asp:TemplateField</span> <span style="color:#ff0000;">HeaderText</span><span style="color:#0000ff;">="Id"</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   4:</span>          <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">ItemTemplate</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   5:</span>               <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">asp:Label</span> <span style="color:#ff0000;">ID</span><span style="color:#0000ff;">="lblPersonaId"</span> <span style="color:#ff0000;">runat</span><span style="color:#0000ff;">="server"</span> <span style="color:#ff0000;">Text</span><span style="color:#0000ff;">='&lt;%# Eval("Id") %&gt;'</span> <span style="color:#0000ff;">/&gt;</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   6:</span>          <span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">ItemTemplate</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   7:</span>     <span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">asp:TemplateField</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   8:</span>     <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">asp:TemplateField</span> <span style="color:#ff0000;">HeaderText</span><span style="color:#0000ff;">="Nombre"</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   9:</span>          <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">ItemTemplate</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  10:</span>               <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">asp:Label</span> <span style="color:#ff0000;">ID</span><span style="color:#0000ff;">="lblNombre"</span> <span style="color:#ff0000;">runat</span><span style="color:#0000ff;">="server"</span> <span style="color:#ff0000;">Text</span><span style="color:#0000ff;">='&lt;%# Eval("Nombre") %&gt;'</span> <span style="color:#0000ff;">/&gt;</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  11:</span>          <span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">ItemTemplate</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  12:</span>     <span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">asp:TemplateField</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  13:</span>     <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">asp:TemplateField</span> <span style="color:#ff0000;">HeaderText</span><span style="color:#0000ff;">="Apellido"</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  14:</span>          <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">ItemTemplate</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  15:</span>               <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">asp:Label</span> <span style="color:#ff0000;">ID</span><span style="color:#0000ff;">="lblApellido"</span> <span style="color:#ff0000;">runat</span><span style="color:#0000ff;">="server"</span> <span style="color:#ff0000;">Text</span><span style="color:#0000ff;">='&lt;%# Eval("Apellido") %&gt;'</span> <span style="color:#0000ff;">/&gt;</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  16:</span>          <span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">ItemTemplate</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  17:</span>     <span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">asp:TemplateField</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  18:</span> <span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">Columns</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  19:</span> <span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">asp:GridView</span><span style="color:#0000ff;">&gt;</span></pre>
</div>
</div>
<p>El Html renderizado se vería así:</p>
<div style="border:1px solid gray;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:500px;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;margin:20px 0 10px;padding:4px;">
<div style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;padding:0;">
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">table</span> <span style="color:#ff0000;">id</span><span style="color:#0000ff;">="gvPersonas"</span> <span style="color:#ff0000;">style</span><span style="color:#0000ff;">="border-collapse: collapse"</span> <span style="color:#ff0000;">cellspacing</span><span style="color:#0000ff;">="0"</span> <span style="color:#ff0000;">rules</span><span style="color:#0000ff;">="all"</span> <span style="color:#ff0000;">border</span><span style="color:#0000ff;">="1"</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span>   <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">tbody</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span>     <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">tr</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   4:</span>        <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">th</span> <span style="color:#ff0000;">scope</span><span style="color:#0000ff;">="col"</span><span style="color:#0000ff;">&gt;</span>Id<span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">th</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   5:</span>        <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">th</span> <span style="color:#ff0000;">scope</span><span style="color:#0000ff;">="col"</span><span style="color:#0000ff;">&gt;</span>Nombre<span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">th</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   6:</span>        <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">th</span> <span style="color:#ff0000;">scope</span><span style="color:#0000ff;">="col"</span><span style="color:#0000ff;">&gt;</span>Apellido<span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">th</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   7:</span>     <span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">tr</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   8:</span>     <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">tr</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   9:</span>        <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">td</span><span style="color:#0000ff;">&gt;&lt;</span><span style="color:#800000;">span</span> <span style="color:#ff0000;">id</span><span style="color:#0000ff;">="gvPersonas_lblPersonaId_0"</span><span style="color:#0000ff;">&gt;</span>1<span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">span</span><span style="color:#0000ff;">&gt;&lt;/</span><span style="color:#800000;">td</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  10:</span>        <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">td</span><span style="color:#0000ff;">&gt;&lt;</span><span style="color:#800000;">span</span> <span style="color:#ff0000;">id</span><span style="color:#0000ff;">="gvPersonas_lblNombre_0"</span><span style="color:#0000ff;">&gt;</span>Juan (primer persona)<span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">span</span><span style="color:#0000ff;">&gt;&lt;/</span><span style="color:#800000;">td</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  11:</span>        <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">td</span><span style="color:#0000ff;">&gt;&lt;</span><span style="color:#800000;">span</span> <span style="color:#ff0000;">id</span><span style="color:#0000ff;">="gvPersonas_lblApellido_0"</span><span style="color:#0000ff;">&gt;</span>Lopez<span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">span</span><span style="color:#0000ff;">&gt;&lt;/</span><span style="color:#800000;">td</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  12:</span>     <span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">tr</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  13:</span>     ...</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  14:</span>     <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">tr</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  15:</span>        <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">td</span><span style="color:#0000ff;">&gt;&lt;</span><span style="color:#800000;">span</span> <span style="color:#ff0000;">id</span><span style="color:#0000ff;">="gvPersonas_lblPersonaId_35"</span><span style="color:#0000ff;">&gt;</span>36<span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">span</span><span style="color:#0000ff;">&gt;&lt;/</span><span style="color:#800000;">td</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  16:</span>        <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">td</span><span style="color:#0000ff;">&gt;&lt;</span><span style="color:#800000;">span</span> <span style="color:#ff0000;">id</span><span style="color:#0000ff;">="gvPersonas_lblNombre_35"</span><span style="color:#0000ff;">&gt;</span>Gerardo (36ta persona)<span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">span</span><span style="color:#0000ff;">&gt;&lt;/</span><span style="color:#800000;">td</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  17:</span>        <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">td</span><span style="color:#0000ff;">&gt;&lt;</span><span style="color:#800000;">span</span> <span style="color:#ff0000;">id</span><span style="color:#0000ff;">="gvPersonas_lblApellido_35"</span><span style="color:#0000ff;">&gt;</span>Contijoch<span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">span</span><span style="color:#0000ff;">&gt;&lt;/</span><span style="color:#800000;">td</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  18:</span>     <span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">tr</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  19:</span>   <span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">tbody</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  20:</span> <span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">table</span><span style="color:#0000ff;">&gt;</span></pre>
</div>
</div>
<p>Noten el sufijo numérico al final de los Ids. Este sufijo puede ser personalizado en los controles enlazados a datos para que sea otro valor y no un simple índice. Esto se logra mediante la aplicación del atributo <strong>RowClientIDSuffix</strong>. El valor de este atributo debe ser nombre de la propiedad o columna (dependiendo del origen de datos) cuyo valor deseamos incluir en el Id. Veamos un ejemplo para aclarar esto. Dado este código:</p>
<div style="border:1px solid gray;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:500px;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;margin:20px 0 10px;padding:4px;">
<div style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;padding:0;">
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">asp:GridView</span> <span style="color:#ff0000;">ID</span><span style="color:#0000ff;">="gvPersonas"</span> <span style="color:#ff0000;">runat</span><span style="color:#0000ff;">="server"</span> <span style="color:#ff0000;">AutoGenerateColumns</span><span style="color:#0000ff;">="false"</span> <span style="color:#ff0000;">ClientIDMode</span><span style="color:#0000ff;">="Predictable"</span> <span style="color:#ff0000;">RowClientIDSuffix</span><span style="color:#0000ff;">="Apellido"</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span> <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">Columns</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span>     <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">asp:TemplateField</span> <span style="color:#ff0000;">HeaderText</span><span style="color:#0000ff;">="Id"</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   4:</span>          <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">ItemTemplate</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   5:</span>               <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">asp:Label</span> <span style="color:#ff0000;">ID</span><span style="color:#0000ff;">="lblPersonaId"</span> <span style="color:#ff0000;">runat</span><span style="color:#0000ff;">="server"</span> <span style="color:#ff0000;">Text</span><span style="color:#0000ff;">='&lt;%# Eval("Id") %&gt;'</span> <span style="color:#0000ff;">/&gt;</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   6:</span>          <span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">ItemTemplate</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   7:</span>     <span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">asp:TemplateField</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   8:</span>     <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">asp:TemplateField</span> <span style="color:#ff0000;">HeaderText</span><span style="color:#0000ff;">="Nombre"</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   9:</span>          <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">ItemTemplate</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  10:</span>               <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">asp:Label</span> <span style="color:#ff0000;">ID</span><span style="color:#0000ff;">="lblNombre"</span> <span style="color:#ff0000;">runat</span><span style="color:#0000ff;">="server"</span> <span style="color:#ff0000;">Text</span><span style="color:#0000ff;">='&lt;%# Eval("Nombre") %&gt;'</span> <span style="color:#0000ff;">/&gt;</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  11:</span>          <span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">ItemTemplate</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  12:</span>     <span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">asp:TemplateField</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  13:</span>     <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">asp:TemplateField</span> <span style="color:#ff0000;">HeaderText</span><span style="color:#0000ff;">="Apellido"</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  14:</span>          <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">ItemTemplate</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  15:</span>               <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">asp:Label</span> <span style="color:#ff0000;">ID</span><span style="color:#0000ff;">="lblApellido"</span> <span style="color:#ff0000;">runat</span><span style="color:#0000ff;">="server"</span> <span style="color:#ff0000;">Text</span><span style="color:#0000ff;">='&lt;%# Eval("Apellido") %&gt;'</span> <span style="color:#0000ff;">/&gt;</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  16:</span>          <span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">ItemTemplate</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  17:</span>     <span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">asp:TemplateField</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  18:</span> <span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">Columns</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  19:</span> <span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">asp:GridView</span><span style="color:#0000ff;">&gt;</span></pre>
</div>
</div>
<p>Se genera lo siguiente:</p>
<div style="border:1px solid gray;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:500px;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;margin:20px 0 10px;padding:4px;">
<div style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;padding:0;">
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">table</span> <span style="color:#ff0000;">id</span><span style="color:#0000ff;">="gvPersonas"</span> <span style="color:#ff0000;">style</span><span style="color:#0000ff;">="border-collapse: collapse"</span> <span style="color:#ff0000;">cellspacing</span><span style="color:#0000ff;">="0"</span> <span style="color:#ff0000;">rules</span><span style="color:#0000ff;">="all"</span> <span style="color:#ff0000;">border</span><span style="color:#0000ff;">="1"</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span>   <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">tbody</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span>     <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">tr</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   4:</span>        <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">th</span> <span style="color:#ff0000;">scope</span><span style="color:#0000ff;">="col"</span><span style="color:#0000ff;">&gt;</span>Id<span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">th</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   5:</span>        <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">th</span> <span style="color:#ff0000;">scope</span><span style="color:#0000ff;">="col"</span><span style="color:#0000ff;">&gt;</span>Nombre<span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">th</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   6:</span>        <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">th</span> <span style="color:#ff0000;">scope</span><span style="color:#0000ff;">="col"</span><span style="color:#0000ff;">&gt;</span>Apellido<span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">th</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   7:</span>     <span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">tr</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   8:</span>     <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">tr</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   9:</span>        <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">td</span><span style="color:#0000ff;">&gt;&lt;</span><span style="color:#800000;">span</span> <span style="color:#ff0000;">id</span><span style="color:#0000ff;">="gvPersonas_lblPersonaId_Lopez"</span><span style="color:#0000ff;">&gt;</span>1<span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">span</span><span style="color:#0000ff;">&gt;&lt;/</span><span style="color:#800000;">td</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  10:</span>        <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">td</span><span style="color:#0000ff;">&gt;&lt;</span><span style="color:#800000;">span</span> <span style="color:#ff0000;">id</span><span style="color:#0000ff;">="gvPersonas_lblNombre_Lopez"</span><span style="color:#0000ff;">&gt;</span>Juan (primer persona)<span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">span</span><span style="color:#0000ff;">&gt;&lt;/</span><span style="color:#800000;">td</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  11:</span>        <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">td</span><span style="color:#0000ff;">&gt;&lt;</span><span style="color:#800000;">span</span> <span style="color:#ff0000;">id</span><span style="color:#0000ff;">="gvPersonas_lblApellido_Lopez"</span><span style="color:#0000ff;">&gt;</span>Lopez<span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">span</span><span style="color:#0000ff;">&gt;&lt;/</span><span style="color:#800000;">td</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  12:</span>     <span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">tr</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  13:</span>     ...</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  14:</span>     <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">tr</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  15:</span>        <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">td</span><span style="color:#0000ff;">&gt;&lt;</span><span style="color:#800000;">span</span> <span style="color:#ff0000;">id</span><span style="color:#0000ff;">="gvPersonas_lblPersonaId_Contijoch"</span><span style="color:#0000ff;">&gt;</span>36<span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">span</span><span style="color:#0000ff;">&gt;&lt;/</span><span style="color:#800000;">td</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  16:</span>        <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">td</span><span style="color:#0000ff;">&gt;&lt;</span><span style="color:#800000;">span</span> <span style="color:#ff0000;">id</span><span style="color:#0000ff;">="gvPersonas_lblNombre_Contijoch"</span><span style="color:#0000ff;">&gt;</span>Gerardo (36ta persona)<span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">span</span><span style="color:#0000ff;">&gt;&lt;/</span><span style="color:#800000;">td</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  17:</span>        <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">td</span><span style="color:#0000ff;">&gt;&lt;</span><span style="color:#800000;">span</span> <span style="color:#ff0000;">id</span><span style="color:#0000ff;">="gvPersonas_lblApellido_Contijoch"</span><span style="color:#0000ff;">&gt;</span>Contijoch<span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">span</span><span style="color:#0000ff;">&gt;&lt;/</span><span style="color:#800000;">td</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  18:</span>     <span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">tr</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  19:</span>   <span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">tbody</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  20:</span> <span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">table</span><span style="color:#0000ff;">&gt;</span></pre>
</div>
</div>
<p>Si prestan atención, verán que el sufijo ahora es el valor del campo Apellido en nuestro origen de datos. Una característica interesante del atributo <strong>RowClientIDSuffix</strong> es que <em>acepta más de un único valor</em>, por lo que es posible definir nuestro control así:</p>
<div style="border:1px solid gray;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;margin:20px 0 10px;padding:4px;">
<div style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;padding:0;">
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">asp:GridView</span> <span style="color:#ff0000;">ID</span><span style="color:#0000ff;">="gvPersonas"</span> ... <span style="color:#ff0000;">ClientIDMode</span><span style="color:#0000ff;">="Predictable"</span> <span style="color:#ff0000;">RowClientIDSuffix</span><span style="color:#0000ff;">="Id, Apellido"</span><span style="color:#0000ff;">&gt;</span></pre>
</div>
</div>
<p>Generando un HTML como este:</p>
<div style="border:1px solid gray;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;margin:20px 0 10px;padding:4px;">
<div style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;padding:0;">
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> ...</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span> <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">tr</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span>    <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">td</span><span style="color:#0000ff;">&gt;&lt;</span><span style="color:#800000;">span</span> <span style="color:#ff0000;">id</span><span style="color:#0000ff;">="gvPersonas_lblPersonaId_1_Lopez"</span><span style="color:#0000ff;">&gt;</span>1<span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">span</span><span style="color:#0000ff;">&gt;&lt;/</span><span style="color:#800000;">td</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   4:</span>    <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">td</span><span style="color:#0000ff;">&gt;&lt;</span><span style="color:#800000;">span</span> <span style="color:#ff0000;">id</span><span style="color:#0000ff;">="gvPersonas_lblNombre_1_Lopez"</span><span style="color:#0000ff;">&gt;</span>Juan (primer persona)<span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">span</span><span style="color:#0000ff;">&gt;&lt;/</span><span style="color:#800000;">td</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   5:</span>    <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">td</span><span style="color:#0000ff;">&gt;&lt;</span><span style="color:#800000;">span</span> <span style="color:#ff0000;">id</span><span style="color:#0000ff;">="gvPersonas_lblApellido_1_Lopez"</span><span style="color:#0000ff;">&gt;</span>Lopez<span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">span</span><span style="color:#0000ff;">&gt;&lt;/</span><span style="color:#800000;">td</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   6:</span> <span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">tr</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   7:</span> ...</pre>
</div>
</div>
<p>Se puede ver que el sufijo ahora consiste en el campo Id y el campo Apellido.</p>
<p><strong>Referencias:</strong></p>
<ul>
<li><a href="http://www.codeproject.com/KB/aspnet/ASP_NET4_0ClientIDFeature.aspx" target="_blank">ASP.NET 4.0 Client ID Feature</a></li>
<li><a href="http://www.dotnetcube.com/post.aspx?id=c2410d77-b10e-4933-a3d5-ba49d82064a9" target="_blank">Overview of .NET 4.0 features: ASP.NET 4.0 ClientIDMode</a></li>
<li><a href="http://www.mostlylucid.net/archive/2008/11/03/way-too-much-information-on-control-ids-and-asp.net-4.0.aspx" target="_blank">ASP.NET 4.0: Way too much information on Control IDs and ASP.NET 4.0 Client Id Enhancements</a></li>
</ul>
<p>¡Nos vemos en el próximo post!</p>
<p><em>Publicado originalmente en <a title="http://gerardocontijoch.wordpress.com" href="http://gerardocontijoch.wordpress.com" target="_blank">http://gerardocontijoch.wordpress.com</a>.</em></p>
<br />Posted in ASP.NET, Desarrollo Web  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/gerardocontijoch.wordpress.com/329/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/gerardocontijoch.wordpress.com/329/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/gerardocontijoch.wordpress.com/329/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/gerardocontijoch.wordpress.com/329/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/gerardocontijoch.wordpress.com/329/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/gerardocontijoch.wordpress.com/329/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/gerardocontijoch.wordpress.com/329/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/gerardocontijoch.wordpress.com/329/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/gerardocontijoch.wordpress.com/329/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/gerardocontijoch.wordpress.com/329/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/gerardocontijoch.wordpress.com/329/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/gerardocontijoch.wordpress.com/329/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/gerardocontijoch.wordpress.com/329/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/gerardocontijoch.wordpress.com/329/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gerardocontijoch.wordpress.com&amp;blog=5700517&amp;post=329&amp;subd=gerardocontijoch&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://gerardocontijoch.wordpress.com/2009/06/12/generacion-personalizada-de-ids-en-asp-net-4-0/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/08a523d4aac4f7ad6ff4eb1cb6ac85e2?s=96&#38;d=http%3A%2F%2Fs0.wp.com%2Fi%2Fmu.gif&#38;r=G" medium="image">
			<media:title type="html">Gerardo Contijoch</media:title>
		</media:content>
	</item>
		<item>
		<title>Acceder a variables de servidor desde Javascript</title>
		<link>http://gerardocontijoch.wordpress.com/2009/06/06/acceder-a-variables-de-servidor-desde-javascript/</link>
		<comments>http://gerardocontijoch.wordpress.com/2009/06/06/acceder-a-variables-de-servidor-desde-javascript/#comments</comments>
		<pubDate>Sat, 06 Jun 2009 19:00:26 +0000</pubDate>
		<dc:creator>Gerardo Contijoch</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[Desarrollo Web]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://gerardocontijoch.wordpress.com/?p=327</guid>
		<description><![CDATA[Un problema con el que nos solemos encontrar los desarrolladores web es el de acceder desde un script (en el cliente) a datos que se encuentran sólo disponibles en el servidor. No me refiero a recursos que están en el servidor, sino mas bien a valores o datos que pueden ser resultado (o formar parte) [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gerardocontijoch.wordpress.com&amp;blog=5700517&amp;post=327&amp;subd=gerardocontijoch&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Un problema con el que nos solemos encontrar los desarrolladores web es el de acceder desde un script (en el cliente) a datos que se encuentran sólo disponibles en el servidor. No me refiero a recursos que están en el servidor, sino mas bien a valores o datos que pueden ser resultado (o formar parte) de cierto procesamiento y que no se exponen al cliente.</p>
<p>El caso más común con el que me encuentro yo es conocer el id de ciertos controles desde el lado del cliente. Sabemos bien que ASP.NET tiene la costumbre de renombrar nuestros controles con unos nombres bastante crípticos y esto es un gran problema si queremos acceder a ellos desde javascript. El <em>verdadero nombre</em> de estos controles (lo que se conoce como el <em>ClientId</em>) se encuentra accesible desde el código del lado del servidor, pero no desde el cliente. Y es por esto que <a href="http://www.west-wind.com/WebLog" target="_blank">Rick Strahl</a> creo la clase <strong><a href="http://www.west-wind.com/Weblog/posts/252178.aspx" target="_blank">wwScriptVariables</a></strong> que nos permite pasarle a nuestros scripts los valores que querramos desde el servidor.</p>
<p>Veamos un ejemplo con el cual vamos a trabajar. Imaginemos que tenemos una página en donde se ingresa un valor y como respuesta, se informa si ese valor es numérico o no. Esta es la parte interesante del código HTML:</p>
<div style="border:1px solid gray;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;margin:20px 0 10px;padding:4px;">
<div style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;padding:0;">
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">div</span> <span style="color:#ff0000;">id</span><span style="color:#0000ff;">="valor"</span><span style="color:#0000ff;">&gt;</span>Ingrese un valor: <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">asp:TextBox</span> <span style="color:#ff0000;">ID</span><span style="color:#0000ff;">="txtValor"</span> <span style="color:#ff0000;">runat</span><span style="color:#0000ff;">="server"</span><span style="color:#0000ff;">&gt;&lt;/</span><span style="color:#800000;">asp:TextBox</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span>     <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">asp:Button</span> <span style="color:#ff0000;">ID</span><span style="color:#0000ff;">="btnEnviar"</span> <span style="color:#ff0000;">runat</span><span style="color:#0000ff;">="server"</span> <span style="color:#ff0000;">Text</span><span style="color:#0000ff;">="Enviar"</span> <span style="color:#ff0000;">onclick</span><span style="color:#0000ff;">="btnEnviar_Click"</span> <span style="color:#0000ff;">/&gt;</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span> <span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">div</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   4:</span> <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">div</span> <span style="color:#ff0000;">id</span><span style="color:#0000ff;">="resultado"</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   5:</span>     <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">asp:Label</span> <span style="color:#ff0000;">ID</span><span style="color:#0000ff;">="lblResultado"</span> <span style="color:#ff0000;">runat</span><span style="color:#0000ff;">="server"</span> <span style="color:#ff0000;">Text</span><span style="color:#0000ff;">=""</span><span style="color:#0000ff;">&gt;&lt;/</span><span style="color:#800000;">asp:Label</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   6:</span> <span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">div</span><span style="color:#0000ff;">&gt;</span></pre>
</div>
</div>
<p>Y este es el código del lado del servidor:</p>
<div style="border:1px solid gray;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:500px;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;margin:20px 0 10px;padding:4px;">
<div style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;padding:0;">
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> <span style="color:#0000ff;">using</span> System;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">partial</span> <span style="color:#0000ff;">class</span> _Default : System.Web.UI.Page</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   4:</span> {</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   5:</span>     <span style="color:#0000ff;">protected</span> <span style="color:#0000ff;">void</span> Page_Load(<span style="color:#0000ff;">object</span> sender, EventArgs e){}</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   6:</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   7:</span>     <span style="color:#0000ff;">protected</span> <span style="color:#0000ff;">void</span> btnEnviar_Click(<span style="color:#0000ff;">object</span> sender, EventArgs e) {</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   8:</span>         <span style="color:#0000ff;">if</span> (EsNumero(<span style="color:#0000ff;">this</span>.txtValor.Text)) {</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   9:</span>             <span style="color:#0000ff;">this</span>.lblResultado.Text = <span style="color:#006080;">"Es un número!"</span>;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  10:</span>         } <span style="color:#0000ff;">else</span> {</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  11:</span>             <span style="color:#0000ff;">this</span>.lblResultado.Text = <span style="color:#006080;">"No es un número!"</span>;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  12:</span>         }</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  13:</span>     }</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  14:</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  15:</span>     <span style="color:#0000ff;">private</span> <span style="color:#0000ff;">bool</span> EsNumero(<span style="color:#0000ff;">string</span> valor) {</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  16:</span>         <span style="color:#0000ff;">int</span> val = 0;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  17:</span>         <span style="color:#0000ff;">if</span> (<span style="color:#0000ff;">int</span>.TryParse(valor, <span style="color:#0000ff;">out</span> val)) {</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  18:</span>             <span style="color:#0000ff;">return</span> <span style="color:#0000ff;">true</span>;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  19:</span>         }</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  20:</span>         <span style="color:#0000ff;">return</span> <span style="color:#0000ff;">false</span>;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  21:</span>     }</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  22:</span> }</pre>
</div>
</div>
<p>Si ingresamos un valor numérico el resultado se vería así:</p>
<p><img style="display:block;float:none;margin-left:auto;margin-right:auto;" title="RegistrarVariablesServidor-respuesta-original" src="http://gerardocontijoch.files.wordpress.com/2009/06/registrarvariablesservidorrespuestaoriginal.png?w=348&#038;h=58" alt="RegistrarVariablesServidor-respuesta-original" width="348" height="58" /></p>
<p>Como se ve puede ver, todo es muy sencillo, pero imaginemos que queremos aplicarle algún efecto a la página dependiendo del resultado. En mi caso voy a usar jQuery para esto y el efecto va a consistir simplemente en aplicarle un estilo a la respuesta (ya se que lo mismo se puede hacer desde el lado del servidor, pero la idea es modificarlo desde el lado del cliente como un ejemplo).</p>
<p>Normalmente uno agregaría algo similar a esto en la sección head de la página:</p>
<div style="border:1px solid gray;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:300px;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;margin:20px 0 10px;padding:4px;">
<div style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;padding:0;">
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">link</span> <span style="color:#ff0000;">href</span><span style="color:#0000ff;">="style.css"</span> <span style="color:#ff0000;">rel</span><span style="color:#0000ff;">="stylesheet"</span> <span style="color:#ff0000;">type</span><span style="color:#0000ff;">="text/css"</span> <span style="color:#0000ff;">/&gt;</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span> <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">script</span> <span style="color:#ff0000;">src</span><span style="color:#0000ff;">="jquery-1.2.6.min.js"</span> <span style="color:#ff0000;">type</span><span style="color:#0000ff;">="text/javascript"</span><span style="color:#0000ff;">&gt;&lt;/</span><span style="color:#800000;">script</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   4:</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   5:</span> <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">script</span> <span style="color:#ff0000;">type</span><span style="color:#0000ff;">="text/javascript"</span> <span style="color:#ff0000;">language</span><span style="color:#0000ff;">="javascript"</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   6:</span>   $(document).ready(function() {</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   7:</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   8:</span>     var lbl = $("#lblResultado");</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   9:</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  10:</span>     lbl.addClass("claseResultado");</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  11:</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  12:</span>     });</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  13:</span> <span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">script</span> <span style="color:#0000ff;">&gt;</span></pre>
</div>
</div>
<p>Pero eso sólo funciona cuando nuestro control (en este caso el label ‘lblResultado’) <em>no se encuentra dentro de un contenedor</em>, como puede ser una grilla o una master page. Esto se debe a que <em>ASP.NET modifica los Id de los elementos renderizados para evitar la colisión de nombres</em> y por eso, nuestro ‘lblResultado’ se puede transformar en algo como ‘ctl00_ContentPlaceHolder1_lblResultado’ provocando un error en nuestro script. Lo que necesitamos es pasarle a nuestros scripts el verdadero nombre de los controles lo que se puede lograr con algunos code nuggets incrustados en medio de nuestro script:</p>
<div style="border:1px solid gray;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;margin:20px 0 10px;padding:4px;">
<div style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;padding:0;">
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> $(document).ready(<span style="color:#0000ff;">function</span>() {</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span>     <span style="color:#0000ff;">var</span> lbl = $(<span style="color:#006080;">"#&lt;%= lblResultado.ClientId %&gt;"</span>);</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span>     lbl.addClass(<span style="color:#006080;">"claseResultado"</span>);</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   4:</span> });</pre>
</div>
</div>
<p>Pero esto tiene algunos problemas. Primero y principal, si nuestro script esta en un archivo externo y no embebido en la página, no se pueden usar code nuggets. Otro problema es que el valor que pasemos tiene que estar correctamente escapado para que javascript no lo malinterprete o falle. Sumado a esto, no es muy elegante la solución de incrustar código de servidor dentro de un script y no facilita para nada la reutilización de los scripts.</p>
<p>Ahora veamos como solucionamos este inconveniente con la ayuda de <a href="http://www.west-wind.com/Weblog/posts/252178.aspx" target="_blank">wwScriptVariables</a>. Su uso es sencillísimo, solo hay que instanciarla y mediante el método Add() agregamos los valores que queremos pasarle a nuestros scripts. Luego, desde el lado del cliente, podemos acceder a estos valores desde <em>una nueva variable</em> que tendremos a nuestra disposición, la cual es generada (durante el renderizado) por la clase <strong>wwScriptVariables</strong>. Veamos como queda nuestro ejemplo con el uso de esta clase.</p>
<p>Del lado del servidor:</p>
<div style="border:1px solid gray;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:500px;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;margin:20px 0 10px;padding:4px;">
<div style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;padding:0;">
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> <span style="color:#0000ff;">using</span> System;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">partial</span> <span style="color:#0000ff;">class</span> NuevaVersion : System.Web.UI.Page</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   4:</span> {</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   5:</span>     <span style="color:#0000ff;">protected</span> <span style="color:#0000ff;">void</span> Page_Load(<span style="color:#0000ff;">object</span> sender, EventArgs e){}</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   6:</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   7:</span>     <span style="color:#0000ff;">protected</span> <span style="color:#0000ff;">void</span> btnEnviar_Click(<span style="color:#0000ff;">object</span> sender, EventArgs e) {</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   8:</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   9:</span>         Westwind.Web.Controls.Controls.wwScriptVariables scriptVar = <span style="color:#0000ff;">new</span> Westwind.Web.Controls.Controls.wwScriptVariables(<span style="color:#0000ff;">this</span>, <span style="color:#006080;">"variables"</span>);</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  10:</span>         scriptVar.Add(<span style="color:#006080;">"lblResultado"</span>, <span style="color:#0000ff;">this</span>.lblResultado.ClientID);</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  11:</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  12:</span>         <span style="color:#0000ff;">if</span> (EsNumero(<span style="color:#0000ff;">this</span>.txtValor.Text)) {</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  13:</span>             scriptVar.Add(<span style="color:#006080;">"esNumero"</span>, <span style="color:#006080;">"true"</span>);</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  14:</span>             <span style="color:#0000ff;">this</span>.lblResultado.Text = <span style="color:#006080;">"Es un número!"</span>;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  15:</span>         } <span style="color:#0000ff;">else</span> {</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  16:</span>             scriptVar.Add(<span style="color:#006080;">"esNumero"</span>, <span style="color:#006080;">"false"</span>);</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  17:</span>             <span style="color:#0000ff;">this</span>.lblResultado.Text = <span style="color:#006080;">"No es un número!"</span>;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  18:</span>         }</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  19:</span>     }</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  20:</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  21:</span>     <span style="color:#0000ff;">private</span> <span style="color:#0000ff;">bool</span> EsNumero(<span style="color:#0000ff;">string</span> valor) {</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  22:</span>         <span style="color:#0000ff;">int</span> val = 0;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  23:</span>         <span style="color:#0000ff;">if</span> (<span style="color:#0000ff;">int</span>.TryParse(valor, <span style="color:#0000ff;">out</span> val)) {</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  24:</span>             <span style="color:#0000ff;">return</span> <span style="color:#0000ff;">true</span>;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  25:</span>         }</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  26:</span>         <span style="color:#0000ff;">return</span> <span style="color:#0000ff;">false</span>;</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  27:</span>     }</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  28:</span> }</pre>
</div>
</div>
<p>y del lado del cliente:</p>
<div style="border:1px solid gray;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;margin:20px 0 10px;padding:4px;">
<div style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;padding:0;">
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> $(document).ready(<span style="color:#0000ff;">function</span>() {</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span>     <span style="color:#0000ff;">var</span> lbl = $(<span style="color:#006080;">"#"</span> + variables[<span style="color:#006080;">"lblResultado"</span>]);</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   4:</span>     <span style="color:#008000;">// Aplicamos distintas clases dependiendo del resultado</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   5:</span>     <span style="color:#0000ff;">if</span> (variables[<span style="color:#006080;">"esNumero"</span>] == <span style="color:#006080;">"true"</span>)</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   6:</span>     {</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   7:</span>         lbl.addClass(<span style="color:#006080;">"resultadoNumero"</span>);</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   8:</span>     } <span style="color:#0000ff;">else</span> {</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   9:</span>         lbl.addClass(<span style="color:#006080;">"resultadoTexto"</span>);</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  10:</span>     }</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  11:</span> });</pre>
</div>
</div>
<p>El funcionamiento de la clase es muy sencillo, básicamente lo que hace es registrar los valores que queremos pasar al cliente y dentro del evento PreRender genera el siguiente script:</p>
<div style="border:1px solid gray;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;margin:20px 0 10px;padding:4px;">
<div style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;padding:0;">
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> <span style="color:#0000ff;">&lt;</span><span style="color:#800000;">script</span> <span style="color:#ff0000;">type</span><span style="color:#0000ff;">="text/javascript"</span><span style="color:#0000ff;">&gt;</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span> //<span style="color:#0000ff;">&lt;!</span>[CDATA[</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span> var variables = {</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   4:</span>     "lblResultado": 'ctl00_ContentPlaceHolder1_lblResultado',</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   5:</span>     "esNumero": 'false'</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   6:</span> }</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   7:</span> //]]<span style="color:#0000ff;">&gt;</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   8:</span> <span style="color:#0000ff;">&lt;/</span><span style="color:#800000;">script</span> <span style="color:#0000ff;">&gt;</span></pre>
</div>
</div>
<p>Lo mejor de todo es que Rick pone a nuestra disposición el código completo de la clase para que la modifiquemos a gusto si así lo que deseamos.</p>
<p>La página donde pueden bajar esta clase es <a href="http://www.west-wind.com/Weblog/posts/252178.aspx" target="_blank">ésta</a>, pero aclaro que esa es una versión vieja, hay una versión más nueva <a href="http://www.west-wind.com/WebLog/posts/259442.aspx" target="_blank">acá</a>, la cual agrega nuevas funcionalidades como la posibilidad de actualizar el valor de las variables desde el cliente y pasárselas al servidor. Si no nos interesa esto último, les recomiendo usar la primer versión, ya que tiene menos dependencias y es mucho mas liviana.</p>
<p>¡Nos vemos en el próximo post!</p>
<p><em>Publicado originalmente en <a title="http://gerardocontijoch.wordpress.com" href="http://gerardocontijoch.wordpress.com" target="_blank">http://gerardocontijoch.wordpress.com</a>.</em></p>
<br />Posted in ASP.NET, Desarrollo Web, Javascript  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/gerardocontijoch.wordpress.com/327/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/gerardocontijoch.wordpress.com/327/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/gerardocontijoch.wordpress.com/327/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/gerardocontijoch.wordpress.com/327/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/gerardocontijoch.wordpress.com/327/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/gerardocontijoch.wordpress.com/327/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/gerardocontijoch.wordpress.com/327/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/gerardocontijoch.wordpress.com/327/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/gerardocontijoch.wordpress.com/327/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/gerardocontijoch.wordpress.com/327/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/gerardocontijoch.wordpress.com/327/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/gerardocontijoch.wordpress.com/327/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/gerardocontijoch.wordpress.com/327/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/gerardocontijoch.wordpress.com/327/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gerardocontijoch.wordpress.com&amp;blog=5700517&amp;post=327&amp;subd=gerardocontijoch&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://gerardocontijoch.wordpress.com/2009/06/06/acceder-a-variables-de-servidor-desde-javascript/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/08a523d4aac4f7ad6ff4eb1cb6ac85e2?s=96&#38;d=http%3A%2F%2Fs0.wp.com%2Fi%2Fmu.gif&#38;r=G" medium="image">
			<media:title type="html">Gerardo Contijoch</media:title>
		</media:content>

		<media:content url="http://gerardocontijoch.files.wordpress.com/2009/06/registrarvariablesservidorrespuestaoriginal.png" medium="image">
			<media:title type="html">RegistrarVariablesServidor-respuesta-original</media:title>
		</media:content>
	</item>
		<item>
		<title>TraceTool: simplificando el logging en .NET (&#161;y en otras plataformas tambi&#233;n!)</title>
		<link>http://gerardocontijoch.wordpress.com/2009/05/31/tracetool-simplificando-el-logging-en-net/</link>
		<comments>http://gerardocontijoch.wordpress.com/2009/05/31/tracetool-simplificando-el-logging-en-net/#comments</comments>
		<pubDate>Sun, 31 May 2009 05:19:00 +0000</pubDate>
		<dc:creator>Gerardo Contijoch</dc:creator>
				<category><![CDATA[.Net 2.0]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[logging]]></category>

		<guid isPermaLink="false">http://gerardocontijoch.wordpress.com/?p=322</guid>
		<description><![CDATA[Les voy a presentar una herramienta que me cambió la forma de hacer logging en mis aplicaciones .NET. Estoy hablando de TraceTool, creado por Thierry Parent. TraceTool consiste en una librería (disponible para .NET, Java, Delphi, Javascript, entre otros lenguajes) para hacer tracing o logging (como prefieran llamarlo) y un visor (de uso opcional) que [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gerardocontijoch.wordpress.com&amp;blog=5700517&amp;post=322&amp;subd=gerardocontijoch&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Les voy a presentar una herramienta que me cambió la forma de hacer logging en mis aplicaciones .NET. Estoy hablando de <a href="http://www.codeproject.com/KB/trace/tracetool.aspx" target="_blank">TraceTool</a>, creado por <strong><a href="http://www.codeproject.com/script/Articles/MemberArticles.aspx?amid=507856">Thierry Parent</a></strong>.</p>
<p>TraceTool consiste en una librería (disponible para .NET, Java, Delphi, Javascript, entre otros lenguajes) para hacer tracing o logging (como prefieran llamarlo) y un visor (de uso opcional) que es capaz de interpretar tanto los logs o trazas generadas por la librería como así también otros orígenes.</p>
<p>En este post me voy a limitar a hacer una presentación de la herramienta (actualmente en su versión 11), pero para más información pueden consultar el <a href="http://www.codeproject.com/KB/trace/tracetool.aspx" target="_blank">artículo original</a>, el cual contiene muchos más detalles sobre el funcionamiento y configuración de la misma.</p>
<h4>Uso de la librería</h4>
<p>En la mayoría de los casos la única clase con la que vamos a necesitar interactuar directamente es TTrace. Esta clase expone 3 propiedades, Debug, Warning y Error, las cuales se utilizan para registrar logs de información, advertencia y errores respectivamente. Veamos un ejemplo:</p>
<div style="border:1px solid gray;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;margin:20px 0 10px;padding:4px;">
<div style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;padding:0;">
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">void</span> Log() {</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span>     TTrace.Debug.Send(<span style="color:#006080;">"Esto es un mensaje."</span>);</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span>     TTrace.Warning.Send(<span style="color:#006080;">"Esto es una advertencia..."</span>);</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   4:</span>     TTrace.Error.Send(<span style="color:#006080;">"Esto es un mensaje de error!"</span>);</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   5:</span> }</pre>
</div>
</div>
<p>Ese código produce la siguiente salida (vista en el visor de TraceTool):</p>
<p><img style="display:block;float:none;margin-left:auto;margin-right:auto;border-width:0;" title="TraceTool-log1" src="http://gerardocontijoch.files.wordpress.com/2009/05/tracetoollog1.png?w=445&#038;h=146" border="0" alt="TraceTool-log1" width="445" height="146" /></p>
<p>Como se ve, lo único que diferencia a cada tipo de mensaje, es el icono que lo precede. Si lo deseamos, también podemos agregar un comentario o texto asociado al mensaje de la siguiente manera:</p>
<div style="border:1px solid gray;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;margin:20px 0 10px;padding:4px;">
<div style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;padding:0;">
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;">TTrace.Debug.Send(<span style="color:#006080;">"Esto es un mensaje."</span>, <span style="color:#006080;">"Texto con info extra sobre el mensaje..."</span>);</pre>
</div>
</div>
<p>Que nos quedaría así en el visor:</p>
<p><img style="display:block;float:none;margin-left:auto;margin-right:auto;border-width:0;" title="TraceTool-log2" src="http://gerardocontijoch.files.wordpress.com/2009/05/tracetoollog2.png?w=541&#038;h=45" border="0" alt="TraceTool-log2" width="541" height="45" /></p>
<p>Una característica por de más de interesante es el hecho de que el método Send() nos devuelve una instancia de un objeto de tipo TraceNode (que es la clase base de la cual derivan los objetos Debug, Warning y Error), la cual nos permite seguir enviando trazas pero anidadas dentro de la traza original (es decir, sub-trazas):</p>
<div style="border:1px solid gray;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;margin:20px 0 10px;padding:4px;">
<div style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;padding:0;">
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> TraceNode nodo = TTrace.Debug.Send(<span style="color:#006080;">"Nodo nivel 0."</span>);</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span> nodo.Send(<span style="color:#006080;">"¡Me encuentro en el nivel 1!"</span>);</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span> nodo.Send(<span style="color:#006080;">"¡Yo tambien!"</span>);</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   4:</span> nodo.Send(<span style="color:#006080;">"¡Y yo!"</span>);</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   5:</span> TTrace.Debug.Send(<span style="color:#006080;">"Otro nodo nivel 0."</span>);</pre>
</div>
</div>
<p><img style="display:block;float:none;margin-left:auto;margin-right:auto;border-width:0;" title="TraceTool-LogAnidado" src="http://gerardocontijoch.files.wordpress.com/2009/05/tracetoolloganidado.png?w=369&#038;h=154" border="0" alt="TraceTool-LogAnidado" width="369" height="154" /></p>
<p>Un efecto parecido se puede lograr con los métodos Indent() y UnIndent() que nos permiten indentar todas las trazas sin necesidad de guardar una referencia a un nodo en particular. Esto suele ser muy útil cuando logueamos las entradas, procesamiento y salidas de ciertos métodos aunque para ello TraceTool ya tiene un set de métodos específicamente adaptado para tal fin:</p>
<div style="border:1px solid gray;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:500px;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;margin:20px 0 10px;padding:4px;">
<div style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;padding:0;">
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> <span style="color:#0000ff;">private</span> <span style="color:#0000ff;">void</span> UnMetodo() {</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span>     TTrace.Debug.EnterMethod(<span style="color:#006080;">"UnMetodo()"</span>);</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   4:</span>     <span style="color:#0000ff;">try</span> {</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   5:</span>         TTrace.Debug.Send(<span style="color:#006080;">"Procesando..."</span>);</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   6:</span>         <span style="color:#008000;">// ...</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   7:</span>         <span style="color:#0000ff;">throw</span> <span style="color:#0000ff;">new</span> Exception();</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   8:</span>     } <span style="color:#0000ff;">catch</span> {</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   9:</span>         <span style="color:#008000;">// Aca se podría loguear la excepción</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  10:</span>         TTrace.Error.Send(<span style="color:#006080;">"Error!"</span>);</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  11:</span>     } <span style="color:#0000ff;">finally</span> {</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  12:</span>         TTrace.Debug.ExitMethod(<span style="color:#006080;">"UnMetodo()"</span>);</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  13:</span>     }</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  14:</span> }</pre>
</div>
</div>
<p>Esto produce la siguiente salida:</p>
<p><img style="display:block;float:none;margin-left:auto;margin-right:auto;border-width:0;" title="TraceTool-EnterExit" src="http://gerardocontijoch.files.wordpress.com/2009/05/tracetoolenterexit.png?w=322&#038;h=109" border="0" alt="TraceTool-EnterExit" width="322" height="109" /></p>
<h4>Loguear más que sólo texto</h4>
<p>Con TraceTool también es posible loguear el contenido de objetos enteros con la misma facilidad con la que logueamos sólo texto:</p>
<div style="border:1px solid gray;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;margin:20px 0 10px;padding:4px;">
<div style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;padding:0;">
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">class</span> Persona {</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span>     <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">string</span> Nombre { get; set; }</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span>     <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">string</span> Apellido { get; set; }</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   4:</span>     <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">int</span> Edad { get; set; }</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   5:</span> }</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   6:</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   7:</span> <span style="color:#008000;">/*...*/</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   8:</span></pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   9:</span> TTrace.Debug.Send(<span style="color:#006080;">"Logueando solo texto"</span>);</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  10:</span> var p = <span style="color:#0000ff;">new</span> Persona() { Nombre = <span style="color:#006080;">"Gerardo"</span>, Apellido = <span style="color:#006080;">"Contijoch"</span>, Edad = 27 };</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  11:</span> TTrace.Debug.SendObject(<span style="color:#006080;">"Logueando un objeto"</span>, p);</pre>
</div>
</div>
<p><img style="display:block;float:none;margin-left:auto;margin-right:auto;border-width:0;" title="TraceTool-Objeto" src="http://gerardocontijoch.files.wordpress.com/2009/05/tracetoolobjeto.png?w=753&#038;h=218" border="0" alt="TraceTool-Objeto" width="753" height="218" /></p>
<p>También es posible incluir en la información a loguear sobre un objeto detalles como sus campos, métodos, eventos, miembros privados, etc.</p>
<p>En el caso que quisiéramos registrar el valor de una variable de un tipo primitivo, como puede ser un número, este método no resulta práctico (¿para qué mostrar información de la clase Int32 cuando solo queremos su valor?) y es por eso que la clase TraceNode posee el método SendValue():</p>
<div style="border:1px solid gray;overflow:auto;font-size:8pt;width:97.5%;cursor:text;max-height:200px;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;margin:20px 0 10px;padding:4px;">
<div style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;padding:0;">
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> TTrace.Debug.SendValue(<span style="color:#006080;">"Logueando un Int32"</span>, 50);</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span> TTrace.Debug.SendValue(<span style="color:#006080;">"Logueando un DateTime"</span>, <span style="color:#0000ff;">new</span> DateTime(2005, 10, 6));</pre>
<pre style="overflow:visible;font-size:8pt;width:100%;color:black;line-height:12pt;font-family:consolas,'Courier New',courier,monospace;background-color:white;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span> TTrace.Debug.SendValue(<span style="color:#006080;">"Logueando un Boolean"</span>, <span style="color:#0000ff;">false</span>);</pre>
</div>
</div>
<p><img style="display:block;float:none;margin-left:auto;margin-right:auto;border-width:0;" title="TraceTool-Primitive" src="http://gerardocontijoch.files.wordpress.com/2009/05/tracetoolprimitive.png?w=666&#038;h=128" border="0" alt="TraceTool-Primitive" width="666" height="128" /></p>
<p>(Los logs pueden configurarse para que no se registre la información como la hora y el hilo en el que se ejecutó el logueo)</p>
<p>Como si todo esto fuera poco, también existen métodos similares para registrar Xmls (AddXml()), imágenes (SendBitmap()) y hasta tablas (SendTable()). Pueden ver su uso en el <a href="http://www.codeproject.com/KB/trace/tracetool.aspx" target="_blank">artículo</a> donde se presenta TraceTool.</p>
<h4>Configuración</h4>
<p>Hay dos puntos donde se puede configurar el logueo. El primero es la propiedad Options de la clase TTrace. Esta propiedad nos va a permitir configurar el <em>formato de los logs</em> que se registren.</p>
<p>Para controlar la verbosidad de los logs se pueden usar las propiedades SendEvents, SendFunctions, SendInherited, SendPrivate, SendProcessName y SendThreadId, las cuales permiten configurar que información se registra sobre los objetos que logueamos.</p>
<p>La otra propiedad importante de esta clase es SendMode, la cual nos permite indicarle a la API si vamos a enviar logs al visor (util cuando se depura, pero no siempre en producción) o no.</p>
<p>El otro punto donde podemos configurar el logueo es la propiedad WinTrace de TTrace. TTrace internamente utiliza a la clase WinTrace para registrar todo y es por ello que algunos parámetros referidos al logueo se especifican acá.</p>
<p>Para setear el path a un archivo de logs vamos a usar el método SetLogFile(). Junto con el path de logs también se puede configurar el particionamiento de los logs, es decir, si se crea un log diferente por día, por máximo de líneas o simplemente un único log ilimitado.</p>
<p>Lamentablemente, los logs son únicamente en formato Xml y esto no se puede cambiar.</p>
<p>Cabe aclarar que TraceTool tiene <em>2 logs diferentes</em>, uno es el local (el que normalmente usaremos nosotros) y otro es el log del Visor, el cual puede registrar todo lo que se le envíe (usar este log es útil cuando hay mas de una aplicación logueando al mismo lugar). Ambos logs pueden ser configurados independientemente el uno del otro. Para más detalles sobre la configuración pueden ver el <a href="http://www.codeproject.com/KB/trace/tracetool.aspx" target="_blank">artículo original</a> en donde se detalla un poco más el asunto.</p>
<h4>Soporte Unicode</h4>
<p>Desafortunadamente, si bien la API tiene soporte para logs encodeados como Unicode, el visor aún no los soporta y no es posible abrir logs con caracteres especiales. Es por ello que vamos a tener que cuidarnos de lo que guardamos en los logs porque puede que no podamos abrirlos con el visor. Para solucionar esto, yo opté por modificar la API (el código fuente esta disponible para bajar) y filtrar todos los caracteres especiales con una versión ligeramente modificada del método que presenté en <a href="http://gerardocontijoch.wordpress.com/2009/01/15/sacar-acentos-tildes-y-demas-signos-diacriticos-de-un-texto/" target="_blank">este post</a> (ojo con las tablas, que necesitan los caracteres ‘\t’ para ser procesadas correctamente).</p>
<p>Bien, como dije al comienzo, esto fue sólo una breve presentación de la herramienta, no quise entrar en muchos detalles porque iba a terminar siendo una reescritura completa del artículo original de Thierry Parent. Espero que les resulte interesante esta herramienta y por sobretodo, útil.</p>
<p>¡Nos vemos en el próximo post!</p>
<p><em>Publicado originalmente en <a title="http://gerardocontijoch.wordpress.com" href="http://gerardocontijoch.wordpress.com/" target="_blank">http://gerardocontijoch.wordpress.com</a>.</em></p>
<br />Posted in .Net 2.0, C# Tagged: logging <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/gerardocontijoch.wordpress.com/322/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/gerardocontijoch.wordpress.com/322/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/gerardocontijoch.wordpress.com/322/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/gerardocontijoch.wordpress.com/322/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/gerardocontijoch.wordpress.com/322/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/gerardocontijoch.wordpress.com/322/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/gerardocontijoch.wordpress.com/322/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/gerardocontijoch.wordpress.com/322/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/gerardocontijoch.wordpress.com/322/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/gerardocontijoch.wordpress.com/322/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/gerardocontijoch.wordpress.com/322/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/gerardocontijoch.wordpress.com/322/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/gerardocontijoch.wordpress.com/322/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/gerardocontijoch.wordpress.com/322/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gerardocontijoch.wordpress.com&amp;blog=5700517&amp;post=322&amp;subd=gerardocontijoch&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://gerardocontijoch.wordpress.com/2009/05/31/tracetool-simplificando-el-logging-en-net/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/08a523d4aac4f7ad6ff4eb1cb6ac85e2?s=96&#38;d=http%3A%2F%2Fs0.wp.com%2Fi%2Fmu.gif&#38;r=G" medium="image">
			<media:title type="html">Gerardo Contijoch</media:title>
		</media:content>

		<media:content url="http://gerardocontijoch.files.wordpress.com/2009/05/tracetoollog1.png" medium="image">
			<media:title type="html">TraceTool-log1</media:title>
		</media:content>

		<media:content url="http://gerardocontijoch.files.wordpress.com/2009/05/tracetoollog2.png" medium="image">
			<media:title type="html">TraceTool-log2</media:title>
		</media:content>

		<media:content url="http://gerardocontijoch.files.wordpress.com/2009/05/tracetoolloganidado.png" medium="image">
			<media:title type="html">TraceTool-LogAnidado</media:title>
		</media:content>

		<media:content url="http://gerardocontijoch.files.wordpress.com/2009/05/tracetoolenterexit.png" medium="image">
			<media:title type="html">TraceTool-EnterExit</media:title>
		</media:content>

		<media:content url="http://gerardocontijoch.files.wordpress.com/2009/05/tracetoolobjeto.png" medium="image">
			<media:title type="html">TraceTool-Objeto</media:title>
		</media:content>

		<media:content url="http://gerardocontijoch.files.wordpress.com/2009/05/tracetoolprimitive.png" medium="image">
			<media:title type="html">TraceTool-Primitive</media:title>
		</media:content>
	</item>
		<item>
		<title>Utilidad de la propiedad IsReusable de la interface IHttpHandler</title>
		<link>http://gerardocontijoch.wordpress.com/2009/05/20/utilidad-de-la-propiedad-isreusable-de-la-interface-ihttphandler/</link>
		<comments>http://gerardocontijoch.wordpress.com/2009/05/20/utilidad-de-la-propiedad-isreusable-de-la-interface-ihttphandler/#comments</comments>
		<pubDate>Wed, 20 May 2009 21:00:16 +0000</pubDate>
		<dc:creator>Gerardo Contijoch</dc:creator>
				<category><![CDATA[Desarrollo Web]]></category>
		<category><![CDATA[ihttphandler]]></category>
		<category><![CDATA[pooling]]></category>
		<category><![CDATA[threading]]></category>

		<guid isPermaLink="false">http://gerardocontijoch.wordpress.com/?p=186</guid>
		<description><![CDATA[Una de las propiedades, a mi parecer, menos documentadas del .NET Framework es la propiedad IsReusable de la interface IHttpHandler. La documentación nos indica que el valor de esta propiedad determina si el handler en cuestión puede ser reutilizado por más de un request en una aplicación web, lo cual es correcto, pero en ningún [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gerardocontijoch.wordpress.com&amp;blog=5700517&amp;post=186&amp;subd=gerardocontijoch&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Una de las propiedades, a mi parecer, menos documentadas del .NET Framework es la propiedad <a href="http://msdn.microsoft.com/es-ar/library/system.web.ihttphandler.isreusable(VS.80).aspx" target="_blank">IsReusable</a> de la interface <a href="http://msdn.microsoft.com/es-ar/library/system.web.ihttphandler.isreusable(VS.80).aspx" target="_blank">IHttpHandler</a>. La documentación nos indica que el valor de esta propiedad determina si el handler en cuestión puede ser reutilizado por más de un request en una aplicación web, lo cual es correcto, pero en ningún lugar dice porque o porque no deberíamos reutilizar un handler, ni cuales son las consecuencias de hacerlo. En realidad no es muy difícil imaginar una interpretación el valor de esta propiedad. Puede <a href="http://foreachbiscuit.wordpress.com/2007/11/01/isreusable-on-the-ihttphandler-interface/" target="_blank">indicarnos</a> si nuestro handler es thread safe o no, es decir, puede ser ejecutado concurrentemente en más de un thread sin que ello afecte su funcionamiento. Otra <a href="http://neilkilbride.blogspot.com/2008/01/ihttphandler-isreusable-property.html" target="_blank">interpretación</a> (ver comentarios de este <a href="http://neilkilbride.blogspot.com/2008/01/ihttphandler-isreusable-property.html" target="_blank">post</a>) igual de válida para mi, es que esta propiedad determina si se hace pooling del handler o no. Esto significa que si el handler el reutilizable, el mismo no es destruido una vez finalizado su uso, sino que es guardado en algún lado a la espera de otro request, evitando de ese modo, la reinstanciación innecesaria de handlers de uso muy frecuente.</p>
<p>Yo me inclino por la primera (aunque ello no quita que la segunda pueda ser válida también).</p>
<p>Como regla general, si nuestro handler guarda información (en una propiedad o un campo) referente a un request en particular, entonces la propiedad debería devolver <em>false</em> ya que otro request podría estar siendo procesado con la información incorrecta. <a href="http://foreachbiscuit.wordpress.com/2007/11/01/isreusable-on-the-ihttphandler-interface/" target="_blank">Acá</a> se puede ver un ejemplo del problema este.</p>
<p>¡Nos vemos en el próximo post!</p>
<p><em>Publicado originalmente en <a title="http://gerardocontijoch.wordpress.com" href="http://gerardocontijoch.wordpress.com" target="_blank">http://gerardocontijoch.wordpress.com</a>.</em></p>
<br />Posted in Desarrollo Web Tagged: ihttphandler, pooling, threading <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/gerardocontijoch.wordpress.com/186/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/gerardocontijoch.wordpress.com/186/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/gerardocontijoch.wordpress.com/186/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/gerardocontijoch.wordpress.com/186/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/gerardocontijoch.wordpress.com/186/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/gerardocontijoch.wordpress.com/186/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/gerardocontijoch.wordpress.com/186/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/gerardocontijoch.wordpress.com/186/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/gerardocontijoch.wordpress.com/186/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/gerardocontijoch.wordpress.com/186/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/gerardocontijoch.wordpress.com/186/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/gerardocontijoch.wordpress.com/186/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/gerardocontijoch.wordpress.com/186/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/gerardocontijoch.wordpress.com/186/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=gerardocontijoch.wordpress.com&amp;blog=5700517&amp;post=186&amp;subd=gerardocontijoch&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://gerardocontijoch.wordpress.com/2009/05/20/utilidad-de-la-propiedad-isreusable-de-la-interface-ihttphandler/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/08a523d4aac4f7ad6ff4eb1cb6ac85e2?s=96&#38;d=http%3A%2F%2Fs0.wp.com%2Fi%2Fmu.gif&#38;r=G" medium="image">
			<media:title type="html">Gerardo Contijoch</media:title>
		</media:content>
	</item>
	</channel>
</rss>
