<?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>Always Get Better &#187; ASP.NET</title>
	<atom:link href="http://www.alwaysgetbetter.com/blog/category/web-programming/aspnet/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.alwaysgetbetter.com/blog</link>
	<description>Never stop looking for ways to improve</description>
	<lastBuildDate>Mon, 30 Jan 2012 12:00:17 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>ASP.NET &#8220;Service Unavailable&#8221;</title>
		<link>http://www.alwaysgetbetter.com/blog/2008/11/08/aspnet-service-unavailable/</link>
		<comments>http://www.alwaysgetbetter.com/blog/2008/11/08/aspnet-service-unavailable/#comments</comments>
		<pubDate>Sat, 08 Nov 2008 16:04:32 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[IIS]]></category>

		<guid isPermaLink="false">http://www.alwaysgetbetter.com/blog/?p=57</guid>
		<description><![CDATA[If you have threaded processes called by your ASP.NET code, and those processes crash, your site may start giving Service Unavailable errors. Use iisreset to quickly get back up and running. In order to prevent this from occurring at all, I recommend putting try { } catch { } around any statements inside a threaded [...]
No related posts.]]></description>
			<content:encoded><![CDATA[<p>If you have threaded processes called by your ASP.NET code, and those processes crash, your site may start giving <strong>Service Unavailable</strong> errors.</p>
<p>Use <strong>iisreset</strong> to quickly get back up and running.</p>
<p>In order to prevent this from occurring at all, I recommend putting <em>try { } catch { }</em> around any statements inside a threaded function.</p>
<div style="width:468px;margin:0 auto">
<script type="text/javascript"><!--
google_ad_client = "ca-pub-0410364841759590";
/* Homepage, 468x60 ads, After posts, created 12/19/08 */
google_ad_slot = "4210204644";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div><p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://www.alwaysgetbetter.com/blog/2008/11/08/aspnet-service-unavailable/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>CommandEventHandler Event Won&#8217;t Fire for Button in ASP.NET Custom Control</title>
		<link>http://www.alwaysgetbetter.com/blog/2008/09/24/commandeventhandler-event-wont-fire-for-button-in-aspnet-custom-control/</link>
		<comments>http://www.alwaysgetbetter.com/blog/2008/09/24/commandeventhandler-event-wont-fire-for-button-in-aspnet-custom-control/#comments</comments>
		<pubDate>Wed, 24 Sep 2008 23:03:04 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[VB.Net]]></category>

		<guid isPermaLink="false">http://www.alwaysgetbetter.com/blog/?p=55</guid>
		<description><![CDATA[Problem: I created a custom control with a dynamic button and attached an event handler to that button.  When I run the control, clicking the button causes a postback but the event is not fired. Solution: Changed the class inheritance from WebControl to CompositeControl.  Re-compiled and it worked like a charm. Related posts: Command and [...]
Related posts:<ol>
<li><a href='http://www.alwaysgetbetter.com/blog/2011/04/08/command-control-social-media/' rel='bookmark' title='Command and Control Social Media'>Command and Control Social Media</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p><strong>Problem</strong>: I created a custom control with a dynamic button and attached an event handler to that button.  When I run the control, clicking the button causes a postback but the event is not fired.</p>
<p><strong>Solution:</strong> Changed the class inheritance from <em>WebControl</em> to <em>CompositeControl</em>.  Re-compiled and it worked like a charm.</p>
<p>Related posts:<ol>
<li><a href='http://www.alwaysgetbetter.com/blog/2011/04/08/command-control-social-media/' rel='bookmark' title='Command and Control Social Media'>Command and Control Social Media</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.alwaysgetbetter.com/blog/2008/09/24/commandeventhandler-event-wont-fire-for-button-in-aspnet-custom-control/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ASP.NET: A potentially dangerous Request.Form value was detected from the client</title>
		<link>http://www.alwaysgetbetter.com/blog/2008/08/02/aspnet-a-potentially-dangerous-requestform-value-was-detected-from-the-client/</link>
		<comments>http://www.alwaysgetbetter.com/blog/2008/08/02/aspnet-a-potentially-dangerous-requestform-value-was-detected-from-the-client/#comments</comments>
		<pubDate>Sat, 02 Aug 2008 16:31:52 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[security]]></category>

		<guid isPermaLink="false">http://www.alwaysgetbetter.com/blog/?p=42</guid>
		<description><![CDATA[This error is caused by the presence of HTML in the fields returned by a form post.  In many cases, for example page management tools, you may want to allow your users to enter text formatted with HTML.  By default, ASP.NET doesn&#8217;t like this. To turn it off, add ValidateRequest=&#8221;false&#8221; to the top of your [...]
No related posts.]]></description>
			<content:encoded><![CDATA[<p><span style="font-size: 11pt; font-family: &quot;Calibri&quot;,&quot;sans-serif&quot;;">This error is caused by the presence of HTML in the fields returned by a form post.  In many cases, for example page management tools, you may want to allow your users to enter text formatted with HTML.  By default, ASP.NET doesn&#8217;t like this.</span></p>
<p>To turn it off, add <strong>ValidateRequest=&#8221;false&#8221;</strong> to the top of your aspx file.  This turns off validation of form results.</p>
<p>Honestly, I would rather if this property were available for individual form controls, because in my mind validation is still desirable overall even if one or two fields should allow HTML.  But there you have it.</p>
<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://www.alwaysgetbetter.com/blog/2008/08/02/aspnet-a-potentially-dangerous-requestform-value-was-detected-from-the-client/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Replacing / Adding Line Breaks in GridView Text</title>
		<link>http://www.alwaysgetbetter.com/blog/2008/05/27/replacing-adding-line-breaks-in-gridview-text/</link>
		<comments>http://www.alwaysgetbetter.com/blog/2008/05/27/replacing-adding-line-breaks-in-gridview-text/#comments</comments>
		<pubDate>Tue, 27 May 2008 14:19:08 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[HTML]]></category>

		<guid isPermaLink="false">http://www.alwaysgetbetter.com/blog/?p=34</guid>
		<description><![CDATA[The GridView is a powerful control for quickly and easily displaying tables of data. However, a raw dump of information is not always good &#8211; when displayed by a web browser, normal line breaks are simply rendered as spaces. For long blocks of text, it may be desirable to have your GridView insert HTML line [...]
No related posts.]]></description>
			<content:encoded><![CDATA[<p>The GridView is a powerful control for quickly and easily displaying tables of data.  However, a raw dump of information is not always good &#8211; when displayed by a web browser, normal line breaks are simply rendered as spaces.</p>
<p>For long blocks of text, it may be desirable to have your GridView insert HTML line breaks into your data.  This can be accomplished either programatically or declaratively.</p>
<h2>Programatically</h2>
<p>As a programmer, my first instinct is to try to solve the problem using code behind.  I add a RowDataBound event handler to my GridView and create the command this way:</p>
<p>protected void gvMessageList_RowDataBound(object sender, GridViewRowEventArgs e)<br />
{<br />
GridViewRow row = e.Row;<br />
if (e.Row.RowType == DataControlRowType.DataRow)<br />
{<br />
row.Cells[2].Text = row.Cells[2].Text.Replace(&#8220;\n&#8221;, &#8220;&#8221;);<br />
}<br />
}</p>
<p>Although it works, it has several drawbacks:</p>
<ul>
<li>This solution uses a <em>magic number</em> to cause the compiler to replace the third column in the row.  If the structure of the GridView were to change, this function may break</li>
<li>This solution requires the developer to be aware of the final layout of the GridView and to make the connection between the control&#8217;s declaration and its logical code.</li>
</ul>
<h2>Use The Design</h2>
<p>By far, the better solution is to simply declare the formatting changes in the same place as the GridView.  Using a Template field, I can add line breaks to my message by adding this:</p>
<p>&lt;%# Eval(&#8220;Message&#8221;).ToString().Replace(&#8220;\n&#8221;, &#8220;&lt;br /&gt;&#8221;) %&gt;</p>
<h2>More Information</h2>
<p>This solution assumes the contents of &#8220;Message&#8221; are not null.  For more information about this technique (including how to deal with null values), I recommend the ASP.NET message boards: http://forums.asp.net/p/1027728/1403884.aspx</p>
<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://www.alwaysgetbetter.com/blog/2008/05/27/replacing-adding-line-breaks-in-gridview-text/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Handling Relative Paths Programmatically In ASP.NET</title>
		<link>http://www.alwaysgetbetter.com/blog/2008/04/25/handling-relative-paths-programmatically-in-aspnet/</link>
		<comments>http://www.alwaysgetbetter.com/blog/2008/04/25/handling-relative-paths-programmatically-in-aspnet/#comments</comments>
		<pubDate>Fri, 25 Apr 2008 15:28:27 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://www.alwaysgetbetter.com/blog/?p=26</guid>
		<description><![CDATA[One of the nicest features in ASP.NET is its out-of-box support for relative paths in hyper links and other controls. This is very important for developers whose code resides within the root of their testing environment but within a sub-directory of the production server. Whereas one would have to carefully program things like image links [...]
No related posts.]]></description>
			<content:encoded><![CDATA[<p>One of the nicest features in ASP.NET is its out-of-box support for relative paths in hyper links and other controls.  This is very important for developers whose code resides within the root of their testing environment but within a sub-directory of the production server.</p>
<p>Whereas one would have to carefully program things like image links to point at <strong>&#8220;/applicationbasepath/images/&#8221;</strong>, in ASP.NET we can simply use <strong>&#8220;~/images&#8221;</strong>.  Genious!</p>
<p>When writing custom code, however, the work isn&#8217;t done for us automatically.  We have to pass our URL to a server-side function in order to convert the &#8220;<strong>~/</strong>&#8221; into a base path usable by our visitors.</p>
<p>There are two ways of doing this:</p>
<h2>Using Relative Paths From a Web Page or Control</h2>
<p>If we are programming a page template or a server control, we can use the <strong>ResolveUrl()</strong> function provided by our environment context.</p>
<p>string resolvedUrl = ResolveUrl( &#8220;~/index.php&#8221; );</p>
<p>On AlwaysGetBetter.com, &#8220;~/index.php&#8221; would resolve to <strong>http://www.alwaysgetbetter.com/blog/index.php</strong>.  On another site, it might resolve to the root such as <strong>http://www.abetterblog.com/index.php</strong>.  Programming this way makes our solution more portable.</p>
<h2>Using Relative Paths From Outside a Web Page</h2>
<p>Sometimes we need to use relative paths from outside the context of a web page.  For example, if we were to make a change in our Global.asx file, the program code we use may not be considered to be within a web page scope.</p>
<p>Fortunately, .NET provides us with the static helper class <em>VirtualPathUtility</em>.</p>
<p>string redirUrl = VirtualPathUtility.ToAbsolute(&#8220;~/redirPage.aspx&#8221;);<br />
Response.Redirect( redirUrl );</p>
<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://www.alwaysgetbetter.com/blog/2008/04/25/handling-relative-paths-programmatically-in-aspnet/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>404 Errors when Accessing ASPX Pages on Windows 2003</title>
		<link>http://www.alwaysgetbetter.com/blog/2008/04/16/404-errors-when-accessing-aspx-pages-on-windows-2003/</link>
		<comments>http://www.alwaysgetbetter.com/blog/2008/04/16/404-errors-when-accessing-aspx-pages-on-windows-2003/#comments</comments>
		<pubDate>Wed, 16 Apr 2008 11:42:36 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[Web Browsers]]></category>
		<category><![CDATA[IIS]]></category>
		<category><![CDATA[Windows Server 2003]]></category>

		<guid isPermaLink="false">http://www.alwaysgetbetter.com/blog/?p=21</guid>
		<description><![CDATA[By default, windows 2003 Server is locked down and won&#8217;t display ASP.NET pages. To enable them you must set its status to Allowed within the Web Service Extensions of IIS. Related posts: Windows Azure Thoughts &#8211; First Six Months Accessing Configuration Parameters using Play Framework&#8217;s Template Engine Ensure SLA with Multiple Web Role Instances on [...]
Related posts:<ol>
<li><a href='http://www.alwaysgetbetter.com/blog/2011/08/02/windows-azure-thoughts-months/' rel='bookmark' title='Windows Azure Thoughts &#8211; First Six Months'>Windows Azure Thoughts &#8211; First Six Months</a></li>
<li><a href='http://www.alwaysgetbetter.com/blog/2011/05/09/accessing-configuration-parameters-play-frameworks-template-engine/' rel='bookmark' title='Accessing Configuration Parameters using Play Framework&#8217;s Template Engine'>Accessing Configuration Parameters using Play Framework&#8217;s Template Engine</a></li>
<li><a href='http://www.alwaysgetbetter.com/blog/2011/04/25/ensure-sla-multiple-web-role-instances-windows-azure/' rel='bookmark' title='Ensure SLA with Multiple Web Role Instances on Windows Azure'>Ensure SLA with Multiple Web Role Instances on Windows Azure</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>By default, windows 2003 Server is locked down and won&#8217;t display ASP.NET pages.</p>
<p>To enable them you must set its status to <strong>Allowed </strong>within the <strong>Web Service Extensions</strong> of IIS.</p>
<p>Related posts:<ol>
<li><a href='http://www.alwaysgetbetter.com/blog/2011/08/02/windows-azure-thoughts-months/' rel='bookmark' title='Windows Azure Thoughts &#8211; First Six Months'>Windows Azure Thoughts &#8211; First Six Months</a></li>
<li><a href='http://www.alwaysgetbetter.com/blog/2011/05/09/accessing-configuration-parameters-play-frameworks-template-engine/' rel='bookmark' title='Accessing Configuration Parameters using Play Framework&#8217;s Template Engine'>Accessing Configuration Parameters using Play Framework&#8217;s Template Engine</a></li>
<li><a href='http://www.alwaysgetbetter.com/blog/2011/04/25/ensure-sla-multiple-web-role-instances-windows-azure/' rel='bookmark' title='Ensure SLA with Multiple Web Role Instances on Windows Azure'>Ensure SLA with Multiple Web Role Instances on Windows Azure</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.alwaysgetbetter.com/blog/2008/04/16/404-errors-when-accessing-aspx-pages-on-windows-2003/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SQL Connections in ASP.NET &#8211; What you learned is WRONG!</title>
		<link>http://www.alwaysgetbetter.com/blog/2008/02/15/sql-connections-in-aspnet-what-you-learned-is-wrong/</link>
		<comments>http://www.alwaysgetbetter.com/blog/2008/02/15/sql-connections-in-aspnet-what-you-learned-is-wrong/#comments</comments>
		<pubDate>Fri, 15 Feb 2008 19:22:49 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[Blogging]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[interfaces]]></category>

		<guid isPermaLink="false">http://www.alwaysgetbetter.com/blog/2008/02/15/sql-connections-in-aspnet-what-you-learned-is-wrong/</guid>
		<description><![CDATA[When we learn how to open and use a database connection with ASP.NET, as with any other programming concept in any other programming language, the simplified version used to explain what’s going on is not truly representative of the quality professional code we will one day be expected to write. Opening and Closing Connections Case [...]
Related posts:<ol>
<li><a href='http://www.alwaysgetbetter.com/blog/2011/04/20/database-migrations/' rel='bookmark' title='Database Migrations'>Database Migrations</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>When we learn how to open and use a database connection with ASP.NET, as with any other programming concept in any other programming language, the simplified version used to explain what’s going on is not truly representative of the quality professional code we will one day be expected to write.<br />
Opening and Closing Connections</p>
<p>Case in point: managing of sql database connection resources. How many of us learned to write something like this:</p>
<p><code><br />
// Create a new SQL Connection object<br />
SqlConnection conn = new SqlConnection( connectionString );</p>
<p>// Open the connection to the database<br />
conn.Open();</p>
<p>// Create a new SQL Command<br />
SqlCommand cmd = new SqlCommand( “DELETE FROM BabyNames;”, conn );</p>
<p>// Execute the command<br />
cmd.ExecuteNonQuery();</p>
<p>// Close the database connection<br />
conn.Close();<br />
</code></p>
<p>Sure it’s easy to follow, but if you deploy that on a moderately busy server you are going to make your client very unhappy.</p>
<h2>Dispose Resources</h2>
<p>SQLConnection and SQLCommand objects reference unmanaged resources, meaning the C# garbage collector has no framework knowledge about your object. Since these classes both implement the disposable interface it is important to call the Dispose() method in order to correctly free your application’s used memory.</p>
<p>So our code gets updated to look like this:</p>
<p><code><br />
// Create a new SQL Connection object<br />
SqlConnection conn = new SqlConnection( connectionString );</p>
<p>// Open the connection to the database<br />
conn.Open();</p>
<p>// Create a new SQL Command<br />
SqlCommand cmd = new SqlCommand( “DELETE FROM BabyNames;”, conn );</p>
<p>// Execute the command<br />
cmd.ExecuteNonQuery();</p>
<p>// Dispose of the command<br />
cmd.Dispose();</p>
<p>// Close the database connection<br />
conn.Close();</p>
<p>// Dispose of the connection object<br />
conn.Dispose();<br />
</code></p>
<h2>Trap for Errors</h2>
<p>What happens if there’s a problem, and your code fails to complete? If your application crashes before your objects are disposed, you are left with the same effect as if you had never disposed your objects at all!</p>
<p>Fortunately, C# includes the try … finally reserved words. If anything within the try { } block fails, the finally { } still executes. We can easily apply this to our program code:</p>
<p><code><br />
// Create a new SQL Connection object<br />
SqlConnection conn = new SqlConnection( connectionString );</p>
<p>try<br />
{<br />
// Open the connection to the database<br />
conn.Open();</p>
<p>// Create a new SQL Command<br />
SqlCommand cmd = new SqlCommand( “DELETE FROM BabyNames;”, conn );</p>
<p>try<br />
{<br />
// Execute the command<br />
cmd.ExecuteNonQuery();<br />
}<br />
finally<br />
{<br />
// Dispose of the command<br />
cmd.Dispose();<br />
}</p>
<p>// Close the database connection<br />
conn.Close();<br />
}<br />
finally<br />
{<br />
// Dispose of the connection object<br />
conn.Dispose();<br />
}<br />
</code></p>
<p>For my own part, I prefer the using keyword. We can include a using call anywhere we would ordinarily use a disposal object. When the code is compiled, it behaves the same as try … catch, but leaves our program code much more readable.</p>
<p>Even better, we don’t even have to bother calling Dispose() because it does it for us!</p>
<p><code><br />
// Create a new SQL Connection object<br />
using ( SqlConnection conn = new SqlConnection( connectionString ) )<br />
{<br />
// Open the connection to the database<br />
conn.Open();</p>
<p>// Create a new SQL Command<br />
using ( SqlCommand cmd = new SqlCommand( “DELETE FROM BabyNames;”, conn ) )<br />
{<br />
// Execute the command<br />
cmd.ExecuteNonQuery();<br />
}</p>
<p>// Close the database connection<br />
conn.Close();<br />
}<br />
</code></p>
<p>Slick.</p>
<h2>Open Late, Close Early (like a bank)</h2>
<p>There is one more thing I would add to this. Creating objects in memory takes time. Although it happens in fractions of a second too fast to be detectable by us, it’s important not to waste processing time wherever possible.</p>
<p>Whenever we Open() a database connection, we expect to use the database right away. If we then create an SqlCommand, we’re wasting the open connection’s time. It’s as if we pick up the phone and listen to the dial tone while we then flip through the white pages looking for the number we want to call.</p>
<p>Let’s change our example code so we will now Open() at the last possible opportunity, and Close() right away when we’ve made our call:</p>
<p><code><br />
// Create a new SQL Connection object<br />
using ( SqlConnection conn = new SqlConnection( connectionString ) )<br />
{<br />
// Create a new SQL Command<br />
using ( SqlCommand cmd = new SqlCommand( “DELETE FROM BabyNames;”, conn ) )<br />
{<br />
// Open the connection to the database<br />
conn.Open();</p>
<p>// Execute the command<br />
cmd.ExecuteNonQuery();</p>
<p>// Close the connection to the database<br />
conn.Close();<br />
}<br />
}<br />
</code></p>
<p>In the end, the program code we wrote is very similar to the newbie code we started with. However, we’re now protecting our system from memory leaks, and we’re protecting our database from wasted clock cycles. Our code is easy to read and stable.</p>
<p>Related posts:<ol>
<li><a href='http://www.alwaysgetbetter.com/blog/2011/04/20/database-migrations/' rel='bookmark' title='Database Migrations'>Database Migrations</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.alwaysgetbetter.com/blog/2008/02/15/sql-connections-in-aspnet-what-you-learned-is-wrong/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>How To Format Dates Using DataFormatString</title>
		<link>http://www.alwaysgetbetter.com/blog/2008/01/20/how-to-format-dates-using-dataformatstring/</link>
		<comments>http://www.alwaysgetbetter.com/blog/2008/01/20/how-to-format-dates-using-dataformatstring/#comments</comments>
		<pubDate>Mon, 21 Jan 2008 04:34:23 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[DataGridView]]></category>
		<category><![CDATA[DetailsView]]></category>
		<category><![CDATA[GridView]]></category>

		<guid isPermaLink="false">http://www.alwaysgetbetter.com/blog/?p=5</guid>
		<description><![CDATA[During a recent project I was setting up a DetailsView control for a record&#8217;s tombstone information [note: this logic is the same in DataGridView, GridView, and anywhere else that DataFormatString is used]. By default, my dates were coming in this format: 04/18/2008 12:00:00 PM I wanted to display dates without any time information and without [...]
No related posts.]]></description>
			<content:encoded><![CDATA[<p>During a recent project I was setting up a DetailsView control for a record&#8217;s tombstone information [note: this logic is the same in DataGridView, GridView, and anywhere else that DataFormatString is used].</p>
<p>By default, my dates were coming in this format:</p>
<pre>
04/18/2008 12:00:00 PM</pre>
<p>I wanted to display dates without any time information and without leading zeroes on the month like this:</p>
<pre>
4/18/2008</pre>
<p>So I create my DetailsView like this:</p>
<p style="font-size: 70%; text-align: left"><font size="2" color="#0000ff">&lt;<font size="2" color="#a31515">asp</font><font size="2" color="#0000ff">:</font><font size="2" color="#a31515">DetailsView</font><font size="2"> </font><font size="2" color="#ff0000">runat</font><font size="2" color="#0000ff">=&#8221;server&#8221;</font><font size="2"> </font><font size="2" color="#ff0000">ID</font><font size="2" color="#0000ff">=&#8221;dvPatientInformation&#8221; </font><font size="2" color="#ff0000">DataSourceID</font><font size="2" color="#0000ff">=&#8221;sqlPatientInformation&#8221;</font><font size="2" color="#0000ff">&gt;<br />
</font><font size="2" color="#0000ff">    &lt;</font><font size="2" color="#a31515">Fields</font><font size="2" color="#0000ff">&gt;<br />
</font><font size="2" color="#0000ff">        &lt;</font><font size="2" color="#a31515">asp</font><font size="2" color="#0000ff">:</font><font size="2" color="#a31515">BoundField</font><font size="2"> </font><font size="2" color="#ff0000">HeaderText</font><font size="2" color="#0000ff">=&#8221;Title:&#8221;</font><font size="2"> </font><font size="2" color="#ff0000">DataField</font><font size="2" color="#0000ff">=&#8221;strTitle&#8221;</font><font size="2"> </font><font size="2" color="#0000ff">/&gt;<br />
</font><font size="2" color="#0000ff">        &lt;</font><font size="2" color="#a31515">asp</font><font size="2" color="#0000ff">:</font><font size="2" color="#a31515">BoundField</font><font size="2"> </font><font size="2" color="#ff0000">HeaderText</font><font size="2" color="#0000ff">=&#8221;First Name:&#8221;</font><font size="2"> </font><font size="2" color="#ff0000">DataField</font><font size="2" color="#0000ff">=&#8221;strFirstName&#8221;</font><font size="2"> </font><font size="2" color="#0000ff">/&gt;<br />
</font><font size="2" color="#0000ff">        &lt;</font><font size="2" color="#a31515">asp</font><font size="2" color="#0000ff">:</font><font size="2" color="#a31515">BoundField</font><font size="2"> </font><font size="2" color="#ff0000">HeaderText</font><font size="2" color="#0000ff">=&#8221;Last Name:&#8221;</font><font size="2"> </font><font size="2" color="#ff0000">DataField</font><font size="2" color="#0000ff">=&#8221;strLastName&#8221;</font><font size="2"> </font><font size="2" color="#0000ff">/&gt;<br />
</font><font size="2" color="#0000ff">        &lt;</font><font size="2" color="#a31515">asp</font><font size="2" color="#0000ff">:</font><font size="2" color="#a31515">BoundField</font><font size="2"> </font><font size="2" color="#ff0000">HeaderText</font><font size="2" color="#0000ff">=&#8221;Date of Birth:&#8221;</font><font size="2"> </font><font size="2" color="#ff0000">DataField</font><font size="2" color="#0000ff">=&#8221;dtBirthdate&#8221;</font><font size="2"> </font><font size="2" color="#ff0000">HtmlEncode</font><font size="2" color="#0000ff">=&#8221;false&#8221;</font><font size="2"> </font><font size="2" color="#ff0000">DataFormatString</font><font size="2" color="#0000ff">=&#8221;{0:M/dd/yyyy}&#8221;</font><font size="2"> </font><font size="2" color="#ff0000">NullDisplayText</font><font size="2" color="#0000ff">=&#8221;Not Yet Entered&#8221;</font><font size="2"> </font><font size="2" color="#0000ff">/&gt;<br />
</font><font size="2" color="#0000ff">    &lt;/</font><font size="2" color="#a31515">Fields</font><font size="2" color="#0000ff">&gt;<br />
</font><font size="2" color="#0000ff">&lt;/</font><font size="2" color="#a31515">asp</font><font size="2" color="#0000ff">:</font><font size="2" color="#a31515">DetailsView</font><font size="2" color="#0000ff">&gt;</font></font></p>
<p>There are a few key issues to consider here:</p>
<h2>Data Format String</h2>
<p>Using one capital M for the month causes ASP.NET to display the month as a minimum digit &#8211; so for months like April the leading 0 will be removed &#8211; displaying &#8220;4&#8243; rather than &#8220;04&#8243;. </p>
<p>More information about data string format options can be found at the MSDN reference: <a href="http://msdn2.microsoft.com/en-us/library/system.globalization.datetimeformatinfo(vs.71).aspx">http://msdn2.microsoft.com/en-us/library/system.globalization.datetimeformatinfo(vs.71).aspx</a></p>
<h2>Null Display Text</h2>
<p>The business rules for this organization allows for the patient&#8217;s birth date to be NULL in the system.  Rather than display a blank field when NULL, I chose to indicate textually that the patient&#8217;s birth date was not yet on file.</p>
<h2>HTMLEncode</h2>
<p>This is the critical part &#8211; if I did not set HTMLEncode to <strong>false</strong>, my date would not have formatted at all.</p>
<p>Why?  Because when rendering content, ASP.NET automatically takes data bound content and converts it into an HTML Encoded string in order to prevent pages from serving unintended - potentially malevolent &#8211; code along with expected text strings.</p>
<p>The problem &#8211; once a string has been encoded, the server no longer considers it a number or date/time information type, so applying the date format is useless.</p>
<p>The only way to get around this is by turning off HTML Encoding for fields whose contents must be formatted.</p>
<p>Yes, this does seem like quite an odd design issue &#8211; one would expect the HTML Encoding to come into play <em>after</em> all of the formatting had been completed.  That must be why .NET programmers get paid the big money &#8211; having to know tidbits like this eats away at your soul.</p>
<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://www.alwaysgetbetter.com/blog/2008/01/20/how-to-format-dates-using-dataformatstring/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Served from: alwaysgetbetter.com @ 2012-02-05 03:59:52 -->
