<?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>Right Brain Left &#187; Unordered List</title>
	<atom:link href="http://rightbrainleft.net/tag/unordered-list/feed/" rel="self" type="application/rss+xml" />
	<link>http://rightbrainleft.net</link>
	<description>C# and Game Development</description>
	<lastBuildDate>Thu, 01 Dec 2011 00:29:29 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>CheckBoxList as an HTML Unordered List with Subheadings</title>
		<link>http://rightbrainleft.net/2009/09/checkboxlist-as-an-html-unordered-list-with-subheadings/</link>
		<comments>http://rightbrainleft.net/2009/09/checkboxlist-as-an-html-unordered-list-with-subheadings/#comments</comments>
		<pubDate>Fri, 18 Sep 2009 21:09:59 +0000</pubDate>
		<dc:creator>Wayne Denier</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[CheckBoxList]]></category>
		<category><![CDATA[Delegates]]></category>
		<category><![CDATA[Events]]></category>
		<category><![CDATA[ListItem]]></category>
		<category><![CDATA[Unordered List]]></category>

		<guid isPermaLink="false">http://rightbrainleft.net/?p=80</guid>
		<description><![CDATA[I was confronted with creating an array of checkboxes sorted into different categories. Keeping it as a single CheckBoxList control was preferable since it would be really easy to save the form.]]></description>
			<content:encoded><![CDATA[<p>I was confronted with creating an array of checkboxes sorted into different categories. Keeping it as a single CheckBoxList control was preferable since it would be really easy to save the form.</p>
<p>The first thing I did was create my own control that inherits from <a href="http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.checkboxlist.aspx" target="_blank">CheckboxList</a>. The table-based output of the standard checkbox list is harder to style, and in general I like to used unordered lists wherever possible, so I followed <a href="http://sim.plified.com/2008/04/18/checkboxlist-as-and-unordered-list-css-stylable/" target="_blank">this simple method</a> for overloading the Render method and making my own output as an &lt;ul&gt; and each selection as a &lt;li&gt;. The example Chris Pollock gives is in VB, below is my loose adaptation in C#.</p>
<blockquote><p><code>protected override void Render(System.Web.UI.HtmlTextWriter writer)<br />
{<br />
string inputFormatString = "&lt;input id=\"{0}\" name=\"{1}\" type=\"checkbox\" value=\"{2}\" {3} {4} /&gt;";<br />
string labelFormatString = "{1}";<br />
if (!string.IsNullOrEmpty(this.CssClass))<br />
writer.WriteLine("&lt;ul class=\"" + this.CssClass + "\"&gt;");<br />
else<br />
writer.WriteLine("&lt;ul&gt;");<br />
for (int i = 0; i &lt; Items.Count; i++)<br />
{<br />
string postbackScript = "";<br />
if(this.AutoPostBack)<br />
{<br />
postbackScript = String.Format("onclick=\"javascript:setTimeout('__doPostBack(\'{0}\',\'\')', 0)\"", this.UniqueID + "$" + i.ToString());<br />
}<br />
string itemChecked = "";<br />
if(Items[i].Selected)<br />
{<br />
itemChecked = "checked=\"checked\"";<br />
}<br />
writer.WriteLine("&lt;li&gt;");<br />
writer.WriteLine(string.Format(inputFormatString, this.ClientID + "_" + i.ToString(), this.UniqueID + "$" + i.ToString(), Items[i].Value, itemChecked, postbackScript));<br />
writer.WriteLine(string.Format(labelFormatString, this.ClientID + "_" + i.ToString(), Items[i].Text));<br />
writer.WriteLine("&lt;/li&gt;");<br />
}<br />
writer.WriteLine("&lt;/ul&gt;");<br />
}</code></p></blockquote>
<p>It&#8217;s a nice start, but it doesn&#8217;t solve my problem of adding headings to the list. I could go pretty far into making this CheckBoxList control really custom so that it accepts a heirarchical data source, but i decided that would be good for another time. I simply decided to create an event on the CheckBoxList that would be called before each item renders.</p>
<blockquote><p><code>public delegate void ItemPreRenderHandler(System.Collections.IEnumerable data, int index, ListItem item, System.Web.UI.HtmlTextWriter writer);<br />
public event ItemPreRenderHandler ItemPreRender;<br />
public void OnItemPreRender(System.Collections.IEnumerable data, int index, ListItem item, System.Web.UI.HtmlTextWriter writer)<br />
{<br />
if (ItemPreRender != null)<br />
{<br />
ItemPreRender(data, index, item, writer);<br />
}<br />
}<br />
</code></p></blockquote>
<p>And call OnItemPreRender in the Render method as the first command in the for loop for each item.</p>
<blockquote><p><code>OnItemPreRender(((System.Collections.IEnumerable)this.DataSource), i ,Items[i], writer);</code></p></blockquote>
<p>With this, on the page using the control, I hooked a method up to the Event that checked to see if the &#8216;Category&#8217; was different than the last item, and if so output an &lt;li&gt; with a heading. Very old school, but rather simple and flexible.</p>
]]></content:encoded>
			<wfw:commentRss>http://rightbrainleft.net/2009/09/checkboxlist-as-an-html-unordered-list-with-subheadings/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

