<?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>Meatball&#039;s Guide to .NET &#187; ASP.NET</title>
	<atom:link href="http://rogerpence.com/blog/index.php/archives/category/asp-net/feed" rel="self" type="application/rss+xml" />
	<link>http://rogerpence.com/blog</link>
	<description>A dogma-free guide to making real-world sense of .NET</description>
	<lastBuildDate>Wed, 23 Jun 2010 16:15:35 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Using jQuery to work with ASP.NET-munged HTML elements</title>
		<link>http://rogerpence.com/blog/index.php/archives/263</link>
		<comments>http://rogerpence.com/blog/index.php/archives/263#comments</comments>
		<pubDate>Tue, 06 Oct 2009 21:13:00 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://rogerpence.com/blog/?p=263</guid>
		<description><![CDATA[ASP.NET (pre-ASP.NET 4.0, that is) makes it a point of munging control IDs by forcing a fully-qualified naming scheme on both the resulting HTML element’s ID and Name attributes. For example, a TextBox named textboxName, when placed in a user control used on a content page with a master page gets rendered like this at [...]]]></description>
			<content:encoded><![CDATA[<p>ASP.NET (pre-ASP.NET 4.0, that is) makes it a point of munging control IDs by forcing a fully-qualified naming scheme on both the resulting HTML element’s ID and Name attributes. For example, a TextBox named textboxName, when placed in a user control used on a content page with a master page gets rendered like this at runtime:</p>
<div class="sourceCodeHeading">Figure 1. HTML element with munged ID and Name attributes.</div>
<div id="Id0417589035SourceCode" class="sourceCode">
<pre class="even"><span class="ln">  1</span><span class="code">&lt;input name=&quot;ctl00$ContentPlaceHolder1$ctl00$textboxName&quot; </span></pre>
<pre class="odd"><span class="ln">  2</span><span class="code">       id=&quot;ctl00_ContentPlaceHolder1_ctl00_textboxName&quot; </span></pre>
<pre class="even"><span class="ln">  3</span><span class="code">	      type=&quot;text&quot; /&gt;</span></pre>
</div>
<div class="sourceCodeFooter"><a id="Id0417589035SourceCodeToClipboard" class="copyLink" href="javascript:toggleLineNumbers( 'Id0417589035SourceCode' );">Show copy-friendly code</a></div>
<p>It’s often desirable to dynamically associate either behaviors or cosmetic styling to these elements, but because of the munged ID, locating these elements, especially with bare-naked CSS selected is challenging (unless you hard-code special case CSS class names).</p>
<p>jQuery makes the selection of such elements quite easy. Because the munged IDs always end with the control’s base name, you know that the resulting HTML ID will end with that control name. Here’s how to use jQuery to select these elements based on the ending value of their ID.</p>
<p>First, add a reference to jQuery in the master page’s &lt;head&gt; section. (In this example, I’m pulling jQuery from the <a href="http://weblogs.asp.net/scottgu/archive/2009/09/15/announcing-the-microsoft-ajax-cdn.aspx">Microsoft Ajax Content Delivery Network</a>.)</p>
<div class="sourceCodeHeading">Figure 2. Add a reference to jQuery in the section of the master page.</div>
<div id="Id0416540130SourceCode" class="sourceCode">
<pre class="even"><span class="ln">  1</span><span class="code">&lt;script src=&quot;http://ajax.microsoft.com/ajax/jquery/jquery-1.3.2.min.js&quot; </span></pre>
<pre class="odd"><span class="ln">  2</span><span class="code">        type=&quot;text/javascript&quot;&gt;&lt;/script&gt;     </span></pre>
</div>
<div class="sourceCodeFooter"><a id="Id0416540130SourceCodeToClipboard" class="copyLink" href="javascript:toggleLineNumbers( 'Id0416540130SourceCode' );">Show copy-friendly code</a></div>
<p>To locate an HTML element with a munged ID, use the jQuery <a href="http://docs.jquery.com/Selectors/attributeEndsWith#attributevalue">AttributeEndsWith</a> selector. This selector finds all elements where the given ID ends with a specific value.</p>
<div class="sourceCodeHeading">Figure 3. Add this JavaScript to the content page.</div>
<div id="Id0402404966SourceCode" class="sourceCode">
<pre class="even"><span class="ln">  1</span><span class="code">&lt;script type=&quot;text/javascript&quot;&gt;</span></pre>
<pre class="odd"><span class="ln">  2</span><span class="code">    $( function() {</span></pre>
<pre class="even"><span class="ln">  3</span><span class="code">        $( &quot;input[ id$='textboxName']&quot; ).val( &quot;prove this.&quot; );</span></pre>
<pre class="odd"><span class="ln">  4</span><span class="code">    });</span></pre>
<pre class="even"><span class="ln">  5</span><span class="code">&lt;/script&gt;</span></pre>
</div>
<div class="sourceCodeFooter"><a id="Id0402404966SourceCodeToClipboard" class="copyLink" href="javascript:toggleLineNumbers( 'Id0402404966SourceCode' );">Show copy-friendly code</a></div>
</p>
<p>The example above selects the input element that ends with “textboxName” and sets its value to &#8216;”prove this.”</p>
<p>jQuery’s AttributeEndsWith performs a brute-force traversal of all of the tags specified (and applies a regular expression against the ending value provided) so this technique may have some potential performance issues, but my experience with it hasn’t shown any glaring degradation.</p>
<p>rp</p>
]]></content:encoded>
			<wfw:commentRss>http://rogerpence.com/blog/index.php/archives/263/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
