<?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/"
	>

<channel>
	<title>gljakal.com - blog &#187; asp.net</title>
	<atom:link href="http://www.gljakal.com/blog/category/aspnet/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.gljakal.com/blog</link>
	<description></description>
	<lastBuildDate>Sat, 14 Nov 2009 17:27:55 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Getting rid of the Naming container in ASP.Net 2.0 &#8211; update</title>
		<link>http://www.gljakal.com/blog/2009/10/06/getting-rid-of-the-naming-container-in-asp-net-2-0-update/</link>
		<comments>http://www.gljakal.com/blog/2009/10/06/getting-rid-of-the-naming-container-in-asp-net-2-0-update/#comments</comments>
		<pubDate>Tue, 06 Oct 2009 17:33:44 +0000</pubDate>
		<dc:creator>glJakaL</dc:creator>
				<category><![CDATA[Tips and tricks]]></category>
		<category><![CDATA[asp.net]]></category>
		<category><![CDATA[web design]]></category>

		<guid isPermaLink="false">http://www.gljakal.com/blog/?p=59</guid>
		<description><![CDATA[In my previous article, Getting rid of the Naming Container in asp.net 2.0, I explained a method to override the extended naming functionality provided by ASP.net in order to create client-side controls with better IDs.
I was however informed in the comments that by overriding the NamingContainer property the control loses the ability to read its [...]]]></description>
			<content:encoded><![CDATA[<p>In my previous article, <a href="http://www.gljakal.com/blog/2007/09/12/getting-rid-of-the-naming-container-in-aspnet-20/">Getting rid of the Naming Container in asp.net 2.0</a>, I explained a method to override the extended naming functionality provided by ASP.net in order to create client-side controls with better IDs.</p>
<p>I was however informed in the comments that by overriding the NamingContainer property the control loses the ability to read its value from the PostBack data.</p>
<p>Since the controls I developed were not meant to to be used in a postback scenario, this wasn’t a big problem for me.</p>
<p>Fast-forward a couple of years and here I am, wondering why post back does not work in one of my projects <img src='http://www.gljakal.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>Anyway, I looked at the link provided by Alex, where <a href="http://www.west-wind.com/WebLog/ShowPost.aspx?id=4605">Rick Strahl talks about overriding the <code>ClientId</code> and the <code>UniqueId</code> properties</a> instead of <code>NamingContainer</code>.</p>
<p>In a standard web control, the two properties <code>ClientID</code> and <code>UniqueID</code> are mapped, respectively, to the <code>id</code> and <code>name</code> properties of the HTML control generated. </p>
<p>Since most (all? ) JS frameworks use the <code>id</code> property to access the varius HTML elements and the PostBack mechanism uses the <code>name</code> property, I think the “best of both worlds” solution is to only override the ClientId:</p>
</p>
<p>  <pre><pre class="csharpcode">&lt;p&gt;&lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;class&lt;/span&gt; NiceTextBox : System.Web.UI.WebControls.TextBox
{
&nbsp;&nbsp;&nbsp;&nbsp;&lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;bool&lt;/span&gt; UseNamingContainer { get; set; }
&lt;/p&gt;&lt;p&gt;&nbsp;&nbsp;&nbsp;&nbsp;&lt;br /&gt;&nbsp;&nbsp;&nbsp;&nbsp;&lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;override&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;string&lt;/span&gt; ClientID
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;get
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt; (&lt;span class=&quot;kwrd&quot;&gt;this&lt;/span&gt;.UseNamingContainer)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;span class=&quot;kwrd&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;base&lt;/span&gt;.ClientID;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;span class=&quot;kwrd&quot;&gt;else&lt;/span&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;span class=&quot;kwrd&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;this&lt;/span&gt;.ID;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;}
}
&lt;/p&gt;</pre></pre></p>
<style type="text/css">
<p>.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>
<p>Now our NiceTextBox works even during post-back scenarios <img src='http://www.gljakal.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  </p>
<script type="text/javascript">
  addthis_url    = 'http%3A%2F%2Fwww.gljakal.com%2Fblog%2F2009%2F10%2F06%2Fgetting-rid-of-the-naming-container-in-asp-net-2-0-update%2F';
  addthis_title  = 'Getting+rid+of+the+Naming+container+in+ASP.Net+2.0+%26%238211%3B+update';
  addthis_pub    = 'gljakal';
</script><script type="text/javascript" src="http://s7.addthis.com/js/addthis_widget.php?v=12" ></script>
]]></content:encoded>
			<wfw:commentRss>http://www.gljakal.com/blog/2009/10/06/getting-rid-of-the-naming-container-in-asp-net-2-0-update/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>New software &#8211; gljakal&#8217;s Sql Exporter</title>
		<link>http://www.gljakal.com/blog/2007/12/19/new-software-gljakals-sql-exporter/</link>
		<comments>http://www.gljakal.com/blog/2007/12/19/new-software-gljakals-sql-exporter/#comments</comments>
		<pubDate>Wed, 19 Dec 2007 08:54:28 +0000</pubDate>
		<dc:creator>glJakaL</dc:creator>
				<category><![CDATA[News]]></category>
		<category><![CDATA[asp.net]]></category>
		<category><![CDATA[gljakal software]]></category>

		<guid isPermaLink="false">http://www.gljakal.com/blog/2007/12/19/new-software-gljakals-sql-exporter/</guid>
		<description><![CDATA[Gljakal's Sql Exporter extracts tables, views, stored procedures and triggers from your database and saves them as individual text files.]]></description>
			<content:encoded><![CDATA[<p>You created a database for your application. You designed the perfect table structure, added some smart views and created the coolest stored procedures that make your application work like magic. </p>
<p>But now, you want to back them up together with the code of your program, using <a href="http://subversion.tigris.org/">subversion</a> or <acronym title="Concurrent Versions System">CVS</acronym>. Afterall, they are part of the application logic. </p>
<p>Better yet, you would like to automate the extraction procedure so that you only need to launch a batch file and your backup is done.</p>
<p>Gljakal&#8217;s Sql Exporter does just that: it extracts tables, views, stored procedures and triggers from your database and saves them as individual text files. </p>
<p>Used together with a source control software, you can have a precise idea of what changed between the various releases, just like you do with your applications&#8217; source code. </p>
<p><a href="http://www.gljakal.com/sqlexporter/index.php"><img src="http://www.gljakal.com/sqlexporter/conn.png" alt="gljakal's Sql Exporter" /></a></p>
<p>You can do that with the interface version (shown in the picture) as well as with the command line version, that you can automate to run whenever you want.</p>
<p>Plus, it&#8217;s a free download!</p>
<script type="text/javascript">
  addthis_url    = 'http%3A%2F%2Fwww.gljakal.com%2Fblog%2F2007%2F12%2F19%2Fnew-software-gljakals-sql-exporter%2F';
  addthis_title  = 'New+software+%26%238211%3B+gljakal%26%238217%3Bs+Sql+Exporter';
  addthis_pub    = 'gljakal';
</script><script type="text/javascript" src="http://s7.addthis.com/js/addthis_widget.php?v=12" ></script>
]]></content:encoded>
			<wfw:commentRss>http://www.gljakal.com/blog/2007/12/19/new-software-gljakals-sql-exporter/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Getting rid of the Naming Container in asp.net 2.0</title>
		<link>http://www.gljakal.com/blog/2007/09/12/getting-rid-of-the-naming-container-in-aspnet-20/</link>
		<comments>http://www.gljakal.com/blog/2007/09/12/getting-rid-of-the-naming-container-in-aspnet-20/#comments</comments>
		<pubDate>Wed, 12 Sep 2007 13:46:33 +0000</pubDate>
		<dc:creator>glJakaL</dc:creator>
				<category><![CDATA[asp.net]]></category>
		<category><![CDATA[web design]]></category>

		<guid isPermaLink="false">http://gljakal.com/blog/2007/09/12/getting-rid-of-the-naming-container-in-aspnet-20/</guid>
		<description><![CDATA[Lately I&#8217;ve been playing around with asp.net 2.0.
One of the new &#34;features&#34; of asp.net 2 is the introduction of Naming containers. 
While using naming containers can be really useful when using databound controls and standard asp.net coding, they can be a real pain to work with when making extensive use of javascript.
This is because a [...]]]></description>
			<content:encoded><![CDATA[<p>Lately I&#8217;ve been playing around with asp.net 2.0.<br />
One of the new &quot;features&quot; of asp.net 2 is the introduction of <a title="Understanding the naming container hierarchy of ASP.NET databound controls" href="http://aspadvice.com/blogs/joteke/archive/2007/02/25/Understanding-the-naming-container-hierarchy-of-ASP.NET-databound-controls.aspx">Naming containers</a>. <br />
While using naming containers can be really useful when using databound controls and standard asp.net coding, they can be a real pain to work with when making extensive use of javascript.<br />
This is because a control declared as<br />
<pre><pre>
&lt;asp:TextBox ID=”TextBox1” runat=”server” /&gt;
</pre></pre><br />
will be rendered as<br />
<pre><pre>
&lt;input name=”ctl00$ContentPlaceHolder1$TextBox1” type=”text”
&nbsp;&nbsp;&nbsp;&nbsp;id=”ctl00_ContentPlaceHolder1_TextBox1” /&gt;
</pre></pre><br />
on the client side, thus breaking any script referencing <span class="code">TextBox1</span> with, for example, <span class="code">getElementById(&#8217;TextBox1&#8242;)</span>.<br />
This behavior is built inside asp.net and is triggered not only when using databoud controls (where it would be necessary to prevent having multiple controls with the same name, for example inside a datagrid), but it is also triggered when using <a href="http://msdn2.microsoft.com/en-us/library/wtxbf3hh.aspx">Master Pages</a>, where having multiple controls with the same name is almost impossible.<br />
<strong>So, how do we code around it?</strong></p>
<h3>The client-side hack</h3>
<p>Luke Foust, in his article <a href="http://blog.spontaneouspublicity.com/2007/08/20/using-jquery-to-make-aspnet-play-nice-with-aspnet/">Using JQuery to Make Asp.Net Play Nice with Asp.Net</a>, explains a couple of client-side methods to let your javascripts select the right element in this situation. I recommend you to read it, because it contains some nice ideas and it also shows off some of the power of <a href="http://www.jquery.com">JQuery</a>.<br />
While these methods will work for most projects, they still present a couple of issues:</p>
<ul>
<li>Generally quite <strong>hard to mantain</strong>, especially the first hack</li>
<li><strong>Code bloat.</strong> While bandwidth is costantly becoming less of an issue, the generated page will have elements with horribly long declarations</li>
<li>It <strong>breaks existing scripts</strong>. You&#8217;ll have to re-check and re-code most of the scripts that should already work</li>
<li><strong>Extra workload on the client.</strong> Todays PCs are fast, so this is less of an issue, however it is something you should consider if you have a lot of asp.net controls in your page.</li>
</ul>
<h3>The Solution</h3>
<p>Let&#8217;s face it: the problem is really on the server, not on the client. Asp.net should not generate those horribly long IDs in the first place, <em>unless we want to</em>. So the best solution is a <strong>server-side hack</strong>.<br />
How does asp.net know whether or not it should generate a unique id for our controls?<br />
Every WebControl in asp.net exposes a property called (I&#8217;m sure you guessed it) <em>NamingContainer</em>, which tells asp.net what&#8217;s the parent control for our textboxes, labels and so on. All we have to do in our code is create a new class that will inherit the control we want to &quot;sanitize&quot; and hide that property from asp.net. Better said in code than in words:<br />
<pre><pre>
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace wtd
{

&nbsp;&nbsp;&nbsp;&nbsp;/// &lt;summary&gt;
&nbsp;&nbsp;&nbsp;&nbsp;/// A TextBox control without ugly IDs
&nbsp;&nbsp;&nbsp;&nbsp;/// &lt;/summary&gt;
&nbsp;&nbsp;&nbsp;&nbsp;public class TextBox : System.Web.UI.WebControls.TextBox
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public override Control NamingContainer
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;get
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return null; 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;}

}
</pre></pre><br />
Then, in our pages, all we have to do is register our new set of controls, like this:<br />
<pre><pre>
&lt;%@ Register TagPrefix=&quot;wtd&quot; Assembly=&quot;app_code&quot;
&nbsp;&nbsp;&nbsp;&nbsp; Namespace=&quot;wtd&quot; %&gt;
</pre></pre><br />
and replace every instance of the default asp controls with our own set of controls (a simple find&amp;replace will do the trick), so<br />
<pre><pre>
&lt;asp:TextBox ID=&quot;TextBox1&quot; runat=&quot;server&quot;&gt;&lt;/asp:TextBox&gt;
</pre></pre><br />
becomes:<br />
<pre><pre>
&lt;wtd:TextBox ID=&quot;TextBox1&quot; runat=&quot;server&quot;&gt;&lt;/wtd:TextBox&gt;
</pre></pre><br />
And that&#8217;s it! Our textbox will now be rendered like this:<br />
<pre><pre>
&lt;input name=”TextBox1” type=”text” id=”TextBox1” /&gt;
</pre></pre><br />
Not only this method won&#8217;t break your existing javascript, it also works nicely with your existing server-side code <img src='http://www.gljakal.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><b style="color:red;">Update</b></p>
<p>
As pointed out by some of you in the comments, this method <b>does not work</b> in a traditional post-back scenario. <a href="http://www.gljakal.com/blog/2009/10/06/getting-rid-of-the-naming-container-in-asp-net-2-0-update/">See my new article for a possible solution to this problem</a>.
</p>
<p></p>
<script type="text/javascript">
  addthis_url    = 'http%3A%2F%2Fwww.gljakal.com%2Fblog%2F2007%2F09%2F12%2Fgetting-rid-of-the-naming-container-in-aspnet-20%2F';
  addthis_title  = 'Getting+rid+of+the+Naming+Container+in+asp.net+2.0';
  addthis_pub    = 'gljakal';
</script><script type="text/javascript" src="http://s7.addthis.com/js/addthis_widget.php?v=12" ></script>
]]></content:encoded>
			<wfw:commentRss>http://www.gljakal.com/blog/2007/09/12/getting-rid-of-the-naming-container-in-aspnet-20/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
	</channel>
</rss>
