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

<channel>
	<title>I Can Has Code?!?</title>
	<atom:link href="http://icanhascode.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://icanhascode.com</link>
	<description>No, not for you.</description>
	<pubDate>Sun, 03 Aug 2008 00:21:13 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5.1</generator>
	<language>en</language>
			<item>
		<title>WebForms Knows Better Than You - Part Duex!</title>
		<link>http://icanhascode.com/2008/07/webforms-knows-better-than-you-part-duex/</link>
		<comments>http://icanhascode.com/2008/07/webforms-knows-better-than-you-part-duex/#comments</comments>
		<pubDate>Fri, 18 Jul 2008 05:44:37 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
		
		<category><![CDATA[Annoyances]]></category>

		<category><![CDATA[Reflector]]></category>

		<category><![CDATA[Troubleshooting]]></category>

		<category><![CDATA[WebForms]]></category>

		<guid isPermaLink="false">http://icanhascode.com/2008/07/webforms-knows-better-than-you-part-duex/</guid>
		<description><![CDATA[It must just be my luck.  This is the second time in not so many weeks that I&#8217;ve ran into an issue with WebForms where something decides that it knows better than me, ignores what I tell it, and goes off on its own merry way.  At least it offers a good opportunity to break [...]]]></description>
			<content:encoded><![CDATA[<p>It must just be my luck.  This is the second time in not so many weeks that I&#8217;ve ran into an issue with WebForms where something decides that it knows better than me, ignores what I tell it, and goes off on its own merry way.  At least it offers a good opportunity to break out .NET Reflector and do some detective work.</p>
<p>Last time we looked over an issue with the CssTextWriter writing out invalid &#8220;background-image&#8221; attributes.  This time we are going to be looking over an issue with the GridView and it mis-calculating column span for pager sections when adding cells to the GridView in a very specific manner.</p>
<p>Now, there are several ways to go about adding cells to the GridView.  The most common way, and generally the simplest way, is to add an &#8220;empty&#8221; column to the Columns collection and doing what you will with it, either with a template or by modifying in OnRowCreated.  This works for most situations, spans correctly, and is the easiest to manage.  Let&#8217;s imagine for a second, though, that you cannot do it that way.  Perhaps you want a little more fine grained control over where those cells get added, or perhaps, as it was in my case, you&#8217;ve already extended the GridView and have a lot of existing code in consumers of your GridView that rely on an explicit ordering of the cells and spectacularly explode when you add the cells this way.  Now, how do you go about adding the cells?</p>
<p>Before we get into the code of adding the cells, let&#8217;s just get a basic extended GridView setup:</p>
<pre name="code" class="c-sharp">public class MyGridView : GridView
{
}</pre>
<pre name="code" class="xhtml">&lt;wfs:MyGridView ID="MyGridView" runat="server" AutoGenerateColumns="false" Width="300" PageSize="4" AllowPaging="true"&gt;
    &lt;PagerSettings Mode="NumericFirstLast" Position="TopAndBottom" /&gt;
    &lt;PagerStyle BackColor="SteelBlue" ForeColor="White" /&gt;
    &lt;HeaderStyle BackColor="Gainsboro" /&gt;
    &lt;Columns&gt;
        &lt;asp:BoundField HeaderText="One" DataField="one" /&gt;
        &lt;asp:BoundField HeaderText="Two"  DataField="two" /&gt;
        &lt;asp:BoundField HeaderText="Three"  DataField="three" /&gt;
    &lt;/Columns&gt;
&lt;/wfs:MyGridView&gt;</pre>
<p>Now, the way we are going to add our extra TableCells is by overriding OnRowCreated and adding the cells *after* calling base.OnRowCreated().  By doing the manipulation after calling base, any consumers that manipulate the cells will be working with the un-modified cell list.</p>
<pre name="code" class="c-sharp">base.OnRowCreated(e);</pre>
<pre name="code" class="c-sharp">if (e.Row.RowType == DataControlRowType.DataRow ||
    e.Row.RowType == DataControlRowType.Header ||
    e.Row.RowType == DataControlRowType.Footer)
{
    TableCell myCell = new TableCell();
    myCell.Text = "AMG";
    e.Row.Cells.AddAt(0, myCell);
}</pre>
<p>Now, if we go and view the site, it looks like this:</p>
<p><a href="http://icanhascode.com/wp-content/uploads/2008/08/original-after-col.jpg"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" src="http://icanhascode.com/wp-content/uploads/2008/08/original-after-col-thumb.jpg" border="0" alt="original-after-col" width="325" height="198" /></a></p>
<p>Doh!  That&#8217;s not quite what we were expecting.  If you go and inspect the HTML you will quickly notice that the &#8220;colspan&#8221; attribute on the top and bottom pagers is incorrect.  In fact, it&#8217;s off by one and is the same as the original number of columns.  Well, so how do we get rid of it.  The way you would expect to get rid of it is to override the InitializePager method of the GridView.  The InitializePager method accepts a parameter for the &#8220;colspan&#8221;, so it should be easy to just override it, add +1 to the input &#8220;colspan&#8221; parameter and pass it back to the base, right?</p>
<pre name="code" class="c-sharp">protected override void InitializePager(GridViewRow row, int columnSpan, PagedDataSource pagedDataSource)
{
    columnSpan += 1;

    base.InitializePager(row, columnSpan, pagedDataSource);
}</pre>
<p>So, that shouldn&#8217;t be too hard. After a quick recompile and going back to the site, you&#8217;ll find it looks exactly the same as before. Well, poop! Time to break open the debugger and see if it&#8217;s actually being set.</p>
<p>If we set a breakpoint in the OnRowCreated method (and go to a row after the pager is created) you&#8217;ll see that the TopPagerRow is does in fact have the correct values.</p>
<p><a href="http://icanhascode.com/wp-content/uploads/2008/08/col-span-after.jpg"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" src="http://icanhascode.com/wp-content/uploads/2008/08/col-span-after-thumb.jpg" border="0" alt="col-span-after" width="449" height="250" /></a></p>
<p>At least at this point we can reasonably assume it&#8217;s not necessarily our fault.  Time to put on our detective hats and break out .NET Reflector; first, though, we need to determine where to look.  I like to do that by sticking breakpoints in at key points and seeing where the values change (or don&#8217;t change).  A good place to start is with the Render method.  Simply override it, call the base version, and set a breakpoint on that line.  Now, if you were to do that in this situation, you&#8217;ll notice that the ColumnSpan is still correct which means that something in the Render function is killing it.</p>
<p>If you navigate to the GridView&#8217;s Render method in .NET Reflector you&#8217;ll see the following:</p>
<p><a href="http://icanhascode.com/wp-content/uploads/2008/08/reflector-render.jpg"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" src="http://icanhascode.com/wp-content/uploads/2008/08/reflector-render-thumb.jpg" border="0" alt="reflector-render" width="427" height="158" /></a></p>
<p>The best way to find these things is to look for anything outside the ordinary and as luck would have it, we don&#8217;t have to look very far.  What&#8217;s this PrepareControlHeirarchy method and what does it do?</p>
<p><a href="http://icanhascode.com/wp-content/uploads/2008/08/reflector-prepare.jpg"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" src="http://icanhascode.com/wp-content/uploads/2008/08/reflector-prepare-thumb.jpg" border="0" alt="reflector-prepare" width="397" height="276" /></a></p>
<p>Notice the rows I&#8217;ve underlined.  The above code looks through the cells in any given row that are of the DataControlFieldCell type, which ours is definitely not, and it increments a variable for each one found that is in a data row.  After looking for where the &#8220;num&#8221; variable is used, you&#8217;ll find the following:</p>
<p><a href="http://icanhascode.com/wp-content/uploads/2008/08/reflector-prepare-2.jpg"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" src="http://icanhascode.com/wp-content/uploads/2008/08/reflector-prepare-2-thumb.jpg" border="0" alt="reflector-prepare-2" width="458" height="186" /></a></p>
<p>And there we have it!  That&#8217;s the cause of our ColumnSpan being off.  Since our cell was not of the &#8220;correct&#8221; type, it happily ignored it and did what it thought it should do.  Nice.  Well, at least in this case the PrepareControlHeirarchy method is virtual.  If we override that method and adjust it like so, we should be golden.</p>
<pre name="code" class="c-sharp">protected override void PrepareControlHierarchy()
{
    base.PrepareControlHierarchy();

    if (TopPagerRow != null &amp;&amp; TopPagerRow.Cells.Count &gt; 0)
        TopPagerRow.Cells[0].ColumnSpan = 4;
}</pre>
<p>I&#8217;m only &#8220;fixing&#8221; the TopPagerRow here with regards to space and to clearly illustrate the fix.  A quick rebuild and run and we are greeted with the following, wonderous, sight:</p>
<p><a href="http://icanhascode.com/wp-content/uploads/2008/08/finished.jpg"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" src="http://icanhascode.com/wp-content/uploads/2008/08/finished-thumb.jpg" border="0" alt="finished" width="325" height="198" /></a></p>
<p>Which is exactly what we were going for.</p>
<p>I mentioned at the start of this post that this situation is not one that is likely encountered, but it&#8217;s always good to know how to dig into the framework and see what&#8217;s going on under the covers.  .NET Reflector a very valuable tool to keep in your toolkit.</p>
]]></content:encoded>
			<wfw:commentRss>http://icanhascode.com/2008/07/webforms-knows-better-than-you-part-duex/feed/</wfw:commentRss>
		</item>
		<item>
		<title>WebForms Knows Better Than You!</title>
		<link>http://icanhascode.com/2008/06/webforms-knows-better-than-you/</link>
		<comments>http://icanhascode.com/2008/06/webforms-knows-better-than-you/#comments</comments>
		<pubDate>Fri, 20 Jun 2008 06:40:29 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
		
		<category><![CDATA[Annoyances]]></category>

		<category><![CDATA[Reflector]]></category>

		<category><![CDATA[Troubleshooting]]></category>

		<category><![CDATA[WebForms]]></category>

		<guid isPermaLink="false">http://icanhascode.com/?p=6</guid>
		<description><![CDATA[Sometimes I get annoyed with ASP.NET WebForms &#8230; No, make that greatly annoyed and disappointed with web forms.  As I mentioned in my last post, I was recently troubleshooting a 2nd incarnation of the mysterious multiple request syndrome (MMRS for short).  What made this time worse was what ending up being the cause.  In this [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes I get annoyed with ASP.NET WebForms &#8230; No, make that greatly annoyed and disappointed with web forms.  As I mentioned in my last post, I was recently troubleshooting a 2nd incarnation of the mysterious multiple request syndrome (MMRS for short).  What made this time worse was what ending up being the cause.  In this case the request was being caused by a &#8220;url(none)&#8221; in a style attribute.  After tracking down the offending component I found something similar to the following example in the code.</p>
<pre name="code" class="c-sharp">Panel inner = new Panel();
inner.ID = "inner";
inner.Style.Add("background-image", "none");</pre>
<p>Notice the last line.  Looks correct doesn&#8217;t it?  WRONG!  It&#8217;s not correct because WebForms Know Better Than You! &#8482;.  When the above is rendered out to the page, it looks like this:</p>
<pre name="code" class="xhtml">&lt;div id="inner" style="background-image: url(none);"&gt; &lt;/div&gt;</pre>
<p>Notice anythign wrong?  Yep, right there in the middle &#8230; the wonderful &#8220;url(none);&#8221; and a 2nd request in your browser for the &#8220;/none&#8221; path.  Well, poo&#8230; How did that get in there?  More importantly why did it get in there?  Especially considering the CSS spec specifically allows &#8220;none&#8221; as a value for the &#8220;background-image&#8221; property.  In fact the w3 page for the CSS1 (and CSS2) spec says &#8220;Value: &lt;url&gt; | none&#8221;.</p>
<p>Being the inquisitive sort, there was only one thing that I could do;  Fire up Reflector.NET and go to town.  First order of business was to find the Style property for the Panel control, which ends up being the Style property of the Panel&#8217;s base class, WebControl.  It is of type CssStyleCollection &#8230; My answer must lie somewhere in there.  The Add method in the CssStyleCollection class isn&#8217;t that special and just adds the items to some hash tables.  The Render methods might be interesting however.  After using the Analyzer on those and seeing how they are used we can determine two things.  First, the Render method that takes an HtmlTextWriter merely calls AddStyleAttribute on the writer and adds the CSS properties while the other one takes a CssTextWriter &#8230; Wait, what?  That&#8217;s a bit different.  It seems to call a WriteAttribute method of the CssTextWriter class.  After making our way to the static WriteAttribute method, we see the following:</p>
<pre name="code" class="c-sharp">if (key != ~HtmlTextWriterStyle.BackgroundColor)
{
    isUrl = attrNameLookupArray[(int) key].isUrl;
}
if (!isUrl)
{
    writer.Write(value);
}
else
{
    WriteUrlAttribute(writer, value);
}</pre>
<p>Hmm, what is this &#8220;isUrl&#8221; and WriteUrlAttribute? Let&#8217;s go find out.  A quick Analyze on the &#8220;attrNameLookupArray&#8221; member shows that it is set in the static constructor so off we go.  In there we see a bunch of RegisterAttribute calls and we see one for the property we set at the beginning of this ordeal, &#8220;background-image&#8221;.  Digging into the RegisterAttribute method, we see that the last parameter to the function, which in the case of &#8220;background-image&#8221; is &#8220;true&#8221;, is the &#8220;isUrl&#8221; value we are looking for.  So the CssTextWriter considers the &#8220;background-image&#8221; attribute a URL based attribute &#8230; Makes sense, as it is one, so the issue must lie somewhere within WriteUrlAttribute.  Looking at the code for that function:</p>
<pre name="code" class="c-sharp">if (StringUtil.StringStartsWith(url, "url("))
{
    int startIndex = 4;
    int length = url.Length - 4;
    if (StringUtil.StringEndsWith(url, ')'))
    {
        length--;
    }
    str = url.Substring(startIndex, length).Trim();
}
// str is rendered out as "url(" + str + ")" after.</pre>
<p>Well, that is unfortunately where are issue lies and there isn&#8217;t a damn thing we can do about it.  No special handling of &#8220;none&#8221;; Just a regurgitation of whatever we set the property to.  That is just too awesome.</p>
]]></content:encoded>
			<wfw:commentRss>http://icanhascode.com/2008/06/webforms-knows-better-than-you/feed/</wfw:commentRss>
		</item>
		<item>
		<title>The Mystery of the Multiple Requests</title>
		<link>http://icanhascode.com/2008/06/the-mystery-of-the-multiple-requests/</link>
		<comments>http://icanhascode.com/2008/06/the-mystery-of-the-multiple-requests/#comments</comments>
		<pubDate>Fri, 20 Jun 2008 05:04:12 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
		
		<category><![CDATA[Troubleshooting]]></category>

		<category><![CDATA[Multiple Requests]]></category>

		<guid isPermaLink="false">http://icanhascode.com/?p=5</guid>
		<description><![CDATA[I&#8217;ve ran into this issue for the second time today and it was as much of a pain to troubleshoot it as it was the first time around so I figured I&#8217;d do a little write-up about it so that others may save themselves a good bit of frustration.&#160; 
The first time I encountered the [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve ran into this issue for the second time today and it was as much of a pain to troubleshoot it as it was the first time around so I figured I&#8217;d do a little write-up about it so that others may save themselves a good bit of frustration.&#160; </p>
<p>The first time I encountered the issue, it seemed to happen at random.&#160; I was stepping through some code for the web application I was working on at the time in the debugger and had a breakpoint set that should have only ever been reached once per request and yet this time it was reached twice.&#160; As I was going through some testing, the problem would go away for a time, and then come back, depending on the state of the page.&#160; </p>
<p>Enter the mysterious request&#8230; So instead of trying to troubleshoot any further in the debugger I decided it would be good to look at the requests as they happen using <a href="https://addons.mozilla.org/en-US/firefox/addon/1843">FireBug</a> (or <a href="http://www.fiddlertool.com/fiddler/">Fiddler</a>).&#160; Sure enough, there were two requests anytime I had a certain component visible on the page.&#160; So that narrowed it down further at least in that I knew what component was causing it.&#160; What gave me enough to google on was the fact that the request headers for the 2nd request were different in one very important way, the &quot;Accept&quot; header.&#160; Instead of the usual &quot;text/html&quot; plus friends it was &quot;image/png,image/*&quot; &#8230; which indicates it was a request for an image resource. </p>
<p>Armed with that information, I prayed before the google gods and found a couple of posts (seriously, there were only two that I could find) about the issue and one mentioned an empty &quot;src&quot; attribute on an image tag.&#160; That wasn&#8217;t the case that time, but it got me thinking &#8230; If image tags could do it, what about CSS properties?&#160; Sure enough, that was the problem.&#160; There was one CSS property defined in a style attribute that was &quot;background-image: url();&quot;.&#160; Taking care of the offending property made the issue go away and everyone was happy.</p>
<p>Now, there are a number of things that can cause this situation and they are outlined below.&#160; There are also multiple ways that the issue will show itself in a request log.&#160; </p>
<p> An empty &quot;src&quot; attribute on an image tag.
<pre class="xhtml" name="code">&lt;img src=&quot;&quot; /&gt;</pre>
<p>An emtpy url definition on a CSS property or a url definition with &quot;none&quot;* inside of it. </p>
<pre class="css" name="code">.test {
background: #ffffff url();
background-image: url('');
background-image: url(none);
}</pre>
<p>I&#8217;ll be talking more about the last one in a follow up post. </p>
<p>As for how they can show up in a request log:</p>
<ul>
<li>A second request for the same path and query.&#160; </li>
<li>A second request for the root path (i.e. the normal request is for &quot;/default.aspx&quot; and the second request is for &quot;/&quot;). </li>
<li>A second request for the &quot;none&quot; path (i.e. &quot;/none&quot;). </li>
</ul>
<p>In Firefox the second request will usually always have the &quot;Accept: image/png,image/*&quot; header.&#160; Unfortunately that is not always the case with IE.&#160; IE likes to happily send &quot;Accept: */*&quot; along.</p>
<p>Now how do you go about finding these in your code and markup?&#160; Unfortunately that is not always that easy and you may have to resort to using multiple tools.&#160; A good start is the HTML tab in FireBug.&#160; Another good option is &quot;View -&gt; Source -&gt; DOM&quot; option in the IE Developer Toolbar.&#160; Another thing to do is to search your solution for the offending items.&#160; Searching with regular expressions can be a big help there.</p>
<p>I hope this post proves helpful to someone else that encounters the problem.&#160; It was almost as frustrating to find the culprit the second time as it was the first time and I knew what I was looking for &#8230; though part of that problem was WebForms working against me, but more on that later.</p>
]]></content:encoded>
			<wfw:commentRss>http://icanhascode.com/2008/06/the-mystery-of-the-multiple-requests/feed/</wfw:commentRss>
		</item>
		<item>
		<title>First Post!</title>
		<link>http://icanhascode.com/2008/06/first-post/</link>
		<comments>http://icanhascode.com/2008/06/first-post/#comments</comments>
		<pubDate>Tue, 17 Jun 2008 07:50:21 +0000</pubDate>
		<dc:creator>Michael</dc:creator>
		
		<category><![CDATA[Nothing Important]]></category>

		<category><![CDATA[Welcome]]></category>

		<guid isPermaLink="false">http://icanhascode.com/?p=4</guid>
		<description><![CDATA[It&#8217;s finally here!  After much procrastination I have finally gotten around to setting up my blog.  My plan is to try and post at least once a week and to keep the topics primarily development and web design related.  However, with a title like &#8220;I Can Has Code?!?&#8221;, I&#8217;m sure there will [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s finally here!  After much procrastination I have finally gotten around to setting up my blog.  My plan is to try and post at least once a week and to keep the topics primarily development and web design related.  However, with a title like &#8220;I Can Has Code?!?&#8221;, I&#8217;m sure there will be plenty of room for less &#8220;structured&#8221; posts.</p>
<p>As for how the name came about&#8230; Well, I&#8217;m a big fan of <a title="lolcats" href="http://icanhascheezburger.com/">lolcats</a>, and while designing the site in photoshop, I typed it in as a placeholder and it just kinda stuck <img src='http://icanhascode.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /></p>
]]></content:encoded>
			<wfw:commentRss>http://icanhascode.com/2008/06/first-post/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 1.882 seconds -->
