<?xml version="1.0" encoding="ISO-8859-1"?>
<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>Egil Hansen &#187; DropDownList</title>
	<atom:link href="http://egilhansen.com/tag/dropdownlist/feed/" rel="self" type="application/rss+xml" />
	<link>http://egilhansen.com</link>
	<description>.NET, Windows, ASP.Net, scripts</description>
	<lastBuildDate>Thu, 04 Feb 2010 13:32:18 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Simply way of adding a default item to a data bound DropDownList</title>
		<link>http://egilhansen.com/2008/07/31/simply-way-of-adding-a-default-item-to-a-data-bound-dropdownlist/</link>
		<comments>http://egilhansen.com/2008/07/31/simply-way-of-adding-a-default-item-to-a-data-bound-dropdownlist/#comments</comments>
		<pubDate>Thu, 31 Jul 2008 09:55:53 +0000</pubDate>
		<dc:creator>Egil Hansen</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[DropDownList]]></category>

		<guid isPermaLink="false">http://www.egil.dk/?p=81</guid>
		<description><![CDATA[I have always found it too difficult to add a &#8220;Choose xxxxx&#8221; item to the top of a DropDownList, when the DropDownList binds to a ObjectDataSource, SqlDataSource or similar. I almost always ended up just feeding the DropDownList its content from the code behind, even though it felt like something that should be possible directly. [...]]]></description>
			<content:encoded><![CDATA[<p>I have always found it too difficult to add a &#8220;Choose xxxxx&#8221; item to the top of a <code>DropDownList</code>, when the <code>DropDownList </code>binds to a <code>ObjectDataSource</code>, <code>SqlDataSource </code>or similar. I almost always ended up just feeding the <code>DropDownList </code>its content from the code behind, even though it felt like something that should be possible directly. The control do have a <code>AppendDataBoundItems </code>property, but most of the time you just end up with duplicated items.</p>
<p>Today I discovered the simplest and most elegant solution I&#8217;ve seen to date. One line of code in the code behind file is still required, but the elegance of DataSource controls is kept. In short, once the <code>DropDownList </code>has bound to its data source, add the &#8220;Choose xxxxx&#8221; item to the items collection of the DropDownList. The trick is to use the <code>Insert </code>method instead of the <code>Add </code>method, since the Insert method allows you to specify where the new item should be inserted, and not just added to the end of the item list.</p>
<p>In this example I use the <code>OnDataBound </code>event to insert the new item:</p>

<div class="wp_syntax"><div class="code"><pre class="asp" style="font-family:monospace;"><span style="color: #006600; font-weight: bold;">&lt;</span>asp<span style="color: #006600; font-weight: bold;">:</span>DropDownList ID<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;ddlCustomer&quot;</span> runat<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;server&quot;</span> 
    DataSourceID<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;odsCustomer&quot;</span> DataTextField<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;DisplayName&quot;</span> 
    DataValueField<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;CustomerId&quot;</span> OnDataBound<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;ddlCustomer_DataBound&quot;</span><span style="color: #006600; font-weight: bold;">&gt;</span>
<span style="color: #006600; font-weight: bold;">&lt;/</span>asp<span style="color: #006600; font-weight: bold;">:</span>DropDownList<span style="color: #006600; font-weight: bold;">&gt;</span></pre></div></div>

<p>The code behind is just one line of code (excluding the method definition):</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">void</span> ddlCustomer_DataBound<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> sender, EventArgs e<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    ddlCustomer.<span style="color: #0000FF;">Items</span>.<span style="color: #0000FF;">Insert</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">0</span>, <span style="color: #008000;">new</span> ListItem<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Choose a Customer&quot;</span>, <span style="color: #666666;">&quot;0&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://egilhansen.com/2008/07/31/simply-way-of-adding-a-default-item-to-a-data-bound-dropdownlist/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
