<?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; C#</title>
	<atom:link href="http://rightbrainleft.net/category/development/csharp/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>How to use Dropbox to Share a UDK Map Folder</title>
		<link>http://rightbrainleft.net/2011/10/how-to-use-dropbox-to-share-a-udk-map-folder/</link>
		<comments>http://rightbrainleft.net/2011/10/how-to-use-dropbox-to-share-a-udk-map-folder/#comments</comments>
		<pubDate>Fri, 07 Oct 2011 23:44:23 +0000</pubDate>
		<dc:creator>Wayne Denier</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Game Development]]></category>
		<category><![CDATA[Games]]></category>
		<category><![CDATA[Tools]]></category>

		<guid isPermaLink="false">http://rightbrainleft.net/?p=297</guid>
		<description><![CDATA[Our team was having trouble sharing our work-in-progress UDK map files with each other over the popular file sharing software Dropbox. Dropbox by default creates a single destination folder on your hard drive to store all the files on your account. However saving our map files outside of the UDK Content directories cause some significant [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://rightbrainleft.net/wp-content/uploads/2011/10/UDKDropbox.jpg"><img src="http://rightbrainleft.net/wp-content/uploads/2011/10/UDKDropbox-300x167.jpg" alt="" title="UDKDropbox" width="300" height="167" class="alignright size-medium wp-image-300" /></a>Our team was having trouble sharing our work-in-progress UDK map files with each other over the popular file sharing software <a href="https://www.dropbox.com/">Dropbox</a>. Dropbox by default creates a single destination folder on your hard drive to store all the files on your account. However saving our map files outside of the UDK Content directories cause some significant problems with finding references and level streaming just plain didn&#8217;t work.</p>
<p>There is a way, however, to make both Dropbox and UDK happy by using a symbolic link between two folders in Windows. Follow this <a href="http://www.maximumpc.com/article/howtos/howto_master_your_file_system_mklink">great write up and tutorial by Alex Castle</a> to learn more about how these work. The short version is that a simple command line utility will let you create a brand new folder within the UDK Maps folder which is bound to one of your local Dropbox folders. Here&#8217;s the command to run from your command prompt&#8230;</p>
<pre class="brush: plain;">mklink /J &quot;C:\UDK\UDK-2011-09\UDKGame\Content\Maps\MyTeamFolder\&quot; &quot;C:\Users\MyUsersFolder\Dropbox\MyTeamShareFolder&quot;</pre>
<p>The breakdown: mlink.exe is the utility which creates the link, /J says make a hard link between the two folders, the first path is where you would like a new folder to be created (it should be inside your UDK maps directory) and the second path is the path for your already existing Dropbox folder for sharing.</p>
]]></content:encoded>
			<wfw:commentRss>http://rightbrainleft.net/2011/10/how-to-use-dropbox-to-share-a-udk-map-folder/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Default MVC model binder doesn&#8217;t like fields</title>
		<link>http://rightbrainleft.net/2011/02/default-mvc-model-binder-doesnt-like-fields/</link>
		<comments>http://rightbrainleft.net/2011/02/default-mvc-model-binder-doesnt-like-fields/#comments</comments>
		<pubDate>Wed, 23 Feb 2011 04:42:03 +0000</pubDate>
		<dc:creator>Wayne Denier</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[Action]]></category>
		<category><![CDATA[auto implemented property]]></category>
		<category><![CDATA[DefaultModelBinder]]></category>
		<category><![CDATA[model binding]]></category>
		<category><![CDATA[MVC]]></category>
		<category><![CDATA[MVC3]]></category>

		<guid isPermaLink="false">http://rightbrainleft.net/?p=287</guid>
		<description><![CDATA[This issue came up both in trying to complete an MVC 3 exercise and recently again when trying to bind an existing Action with a new View model. The form in the View code was posting to the same Action with a few values specified in GET variables, but the variables were being mysteriously swallowed [...]]]></description>
			<content:encoded><![CDATA[<div>This issue came up both in trying to complete an MVC 3 exercise and recently again when trying to bind an existing Action with a new View model. The form in the View code was posting to the same Action with a few values specified in GET variables, but the variables were being mysteriously swallowed by the Model Binder and not bound despite the variables being named exactly the same as the members of my Model! This is what I discovered both times&#8230;</div>
<pre class="brush: csharp;">
public class ExampleViewModel
{
	public string ThisPropertyWillBeBound { get; set; }
	public int ThisOneToo { get; set; }

	public string ThisFieldWontThough;
	public int WatchOutForThis;
}
</pre>
<div>If you are defining a custom type as an argument for an Action, the DefaultModelBinder can only map GET variables posted to the Action to Properties on that custom type. In the above example, if I had a WatchOutForThis=really value in my GET variables, the binder ignores this entirely since WatchOutForThis is a field and not a property.</p>
<p>The difference between creating a field and an auto implemented property is so subtle this can be a really tricky one to catch! Check this if your suddenly find values mysteriously missing between receiving the request and getting to your Action code.</p></div>
]]></content:encoded>
			<wfw:commentRss>http://rightbrainleft.net/2011/02/default-mvc-model-binder-doesnt-like-fields/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using Powershell to inspect folder permissions</title>
		<link>http://rightbrainleft.net/2010/06/using-powershell-to-inspect-folder-permissions/</link>
		<comments>http://rightbrainleft.net/2010/06/using-powershell-to-inspect-folder-permissions/#comments</comments>
		<pubDate>Mon, 21 Jun 2010 17:47:23 +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[folder permissions]]></category>
		<category><![CDATA[permissions]]></category>
		<category><![CDATA[Powershell]]></category>
		<category><![CDATA[recurse]]></category>

		<guid isPermaLink="false">http://rightbrainleft.net/?p=173</guid>
		<description><![CDATA[We wanted to find out which folders had special write/modify permissions in a website, so we could switch out the current anonymous user account for a new one. Bellow is a Powershell command that we used&#8230; dir D:\Inetpub\Wwwroot\target\ -recurse -exclude *.*  &#124; Get-Acl &#124; % { @{Path=$_.Path; Access=$_.Access}} &#124;% {$a = $_.Access $b = @($a [...]]]></description>
			<content:encoded><![CDATA[<p>We wanted to find out which folders had special write/modify permissions in a website, so we could switch out the current anonymous user account for a new one. Bellow is a Powershell command that we used&#8230;</p>
<pre class="brush: plain;">dir D:\Inetpub\Wwwroot\target\ -recurse -exclude *.*  | Get-Acl | % { @{Path=$_.Path; Access=$_.Access}} |% {$a = $_.Access
$b = @($a | ?{ $_.IsInherited -ne $true -and $_.IdentityReference -and ($_.IdentityReference -contains &quot;NT AUTHORITY\Network Service&quot; -or $_.IdentityReference -contains &quot;MACHINE\IUSR_MACHINE&quot;) }|%{ $_.IdentityReference, $_.FileSystemRights})
if ($b.count -gt 0) {$_.Path, $b}
} &gt;&gt; D:\temp\FolderPermissionsWeb.txt</pre>
<div id="_mcePaste">There&#8217;s a couple of things going on here, but it&#8217;s really rather simple. The first segment goes to a directory, and returns all child items that are not files recursively.</div>
<pre class="brush: plain;">dir D:\Inetpub\Wwwroot\target\ -recurse -exclude *.*</pre>
<p>The pipe takes the results and performs another command, in this case Get-Acl. Get-Acl returns a list of all access control levels for the items. So at this point we&#8217;ve already got all access permission for all the folders in the search. My first proof of concept included only these two commands and outputed the results straight to the command prompt. It was enough to get the job done since it showed me all access for everyone on those folders!</p>
<p>The rest of the statements are all about pruning the results down to only the information I want.</p>
<pre class="brush: plain;">$b = @($a | ?{ $_.IsInherited -ne $true -and $_.IdentityReference -and ($_.IdentityReference -contains &quot;NT AUTHORITY\Network Service&quot; -or $_.IdentityReference -contains &quot;MACHINE\IUSR_MACHINE&quot;) }|%{ $_.IdentityReference, $_.FileSystemRights})</pre>
<p>This section achieves that. The end of the first line declares $a as a list of all ACL it could find.  I then declared a variable called $b that will contain all the results from $a that match my conditions. I don&#8217;t want to see any access that has been inherited, and I only want to see right assigned for Network Service or the machine&#8217;s IUSER account (IUSER account name varies per machine).</p>
<pre class="brush: plain;">if ($b.count -gt 0) {$_.Path, $b}
} &gt;&gt; D:\temp\FolderPermissionsWeb.txt</pre>
<p>Finally, I check to see if there are any matches, and then append them to my file. The code here is sort of rough, which I will chalk up to being a novice at Powershell. I&#8217;d hope to make it shorter and format the output a little more, which I&#8217;ll work on.</p>
]]></content:encoded>
			<wfw:commentRss>http://rightbrainleft.net/2010/06/using-powershell-to-inspect-folder-permissions/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>How to bind a nested enumerated control from an ObjectDataSource</title>
		<link>http://rightbrainleft.net/2010/04/how-to-bind-a-nested-enumerated-control-from-an-objectdatasource/</link>
		<comments>http://rightbrainleft.net/2010/04/how-to-bind-a-nested-enumerated-control-from-an-objectdatasource/#comments</comments>
		<pubDate>Thu, 29 Apr 2010 04:05:27 +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[asp]]></category>
		<category><![CDATA[databinding]]></category>
		<category><![CDATA[IEnumerable]]></category>
		<category><![CDATA[ObjectDataSource]]></category>

		<guid isPermaLink="false">http://rightbrainleft.net/?p=162</guid>
		<description><![CDATA[If you have multiple levels of enumerated controls (ie. Repeaters, ListViews) that need to be databound to properties of a bound DataItem, use the below DataSource property on the nested control&#8230; DataSource='&#60;%# DataBinder.Eval(Container.DataItem, &#34;Tags&#34;)%&#62;' This can be required if you have some sort of hierarchical data. My example came up while loading article posts that [...]]]></description>
			<content:encoded><![CDATA[<p>If you have multiple levels of enumerated controls (ie. Repeaters, ListViews) that need to be databound to properties of a bound DataItem, use the below DataSource property on the nested control&#8230;</p>
<pre class="brush: plain;">DataSource='&lt;%#  DataBinder.Eval(Container.DataItem, &quot;Tags&quot;)%&gt;'</pre>
<p>This can be required if you have some sort of hierarchical data. My example came up while loading article posts that contained a list of tags.</p>
]]></content:encoded>
			<wfw:commentRss>http://rightbrainleft.net/2010/04/how-to-bind-a-nested-enumerated-control-from-an-objectdatasource/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Script controls may not be registered before PreRender</title>
		<link>http://rightbrainleft.net/2010/01/script-controls-may-not-be-registered-before-prerender/</link>
		<comments>http://rightbrainleft.net/2010/01/script-controls-may-not-be-registered-before-prerender/#comments</comments>
		<pubDate>Mon, 25 Jan 2010 20:44:07 +0000</pubDate>
		<dc:creator>Wayne Denier</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://rightbrainleft.net/?p=126</guid>
		<description><![CDATA[This ended up being really simple. The page had a few UpdatePanels and a master page containing the ScriptManager. I went to add an UpdateProgress control and got the error&#8230; Script controls may not be registered before PreRender If you&#8217;ve gotten this error, before you go to explore the other possibilities, check to see if [...]]]></description>
			<content:encoded><![CDATA[<p>This ended up being really simple. The page had a few UpdatePanels and a master page containing the ScriptManager. I went to add an UpdateProgress control and got the error&#8230;</p>
<p><strong>Script controls may not be registered before PreRender</strong></p>
<p>If you&#8217;ve gotten this error, before you go to explore the other possibilities,<em> check to see if you&#8217;ve overridden the OnPreRender method for your page</em>. If so, make sure that you&#8217;ve included <strong>base.OnPreRender(e);</strong> up in the method somewhere. If this didn&#8217;t help, there are a number of other scenarios where this could happen and article about them on the internets.</p>
]]></content:encoded>
			<wfw:commentRss>http://rightbrainleft.net/2010/01/script-controls-may-not-be-registered-before-prerender/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to Trim Hierarchical Data Without Breaking It</title>
		<link>http://rightbrainleft.net/2009/11/how-to-trim-hierarchical-data-without-breaking-it/</link>
		<comments>http://rightbrainleft.net/2009/11/how-to-trim-hierarchical-data-without-breaking-it/#comments</comments>
		<pubDate>Thu, 19 Nov 2009 14:52:11 +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[Hierarchical]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[recursive]]></category>

		<guid isPermaLink="false">http://rightbrainleft.net/?p=112</guid>
		<description><![CDATA[We&#8217;ve got a Hierarchical list of nodes that make up a tree where users can expand/collapse and select different nodes. The nodes however come in two distinct types, and I needed to come up with a way of trimming the list on the fly to display one type or another. I came up with the [...]]]></description>
			<content:encoded><![CDATA[<p>We&#8217;ve got a Hierarchical list of nodes that make up a tree where users can expand/collapse and select different nodes. The nodes however come in two distinct types, and I needed to come up with a way of trimming the list on the fly to display one type or another. I came up with the following code.</p>
<pre class="brush: csharp;">
private static List&lt;Node&gt; FilterList(List&lt;Node&gt; list, Utilities.TypeRequired type)
{
	List&lt;Node&gt; returnList = new List&lt;Node&gt;();

	// Go through each node of the full list and run the FilterNode recursive method
	foreach (Node item in list)
		returnList.AddRange(FilterNode(item, type));

	return returnList;
}

private static List&lt;Node&gt; FilterNode(Node item, Utilities.TypeRequired type)
{
	// Prep return list
	List&lt;Node&gt; list = new List&lt;Node&gt;();

	// call this same method on each of the children of this node, store in list
	foreach (var child in item.Children)
		list.AddRange(FilterNode(child, type));

	if (item.TypeId == (int)type)
	{
		// if the node is of the correct type, set it's current children to the
		// results from above and return the item within a list
		item.Children.Clear();
		item.Children.AddRange(list);
		return new List&lt;Node&gt;() { item };
	}
	else
		// if not, just return the children without the parent
		return list;
}
</pre>
<p>The <em>FilterList()</em> method accepts a list of nodes, this is considered to be the first level of children under the root node (in this case there is no root node). It also takes an object that I&#8217;ve called <em>TypeRequired</em>, really it can be any kind of data you&#8217;d like to use to evaluate the differences between one node and another. The method self-references on the children of the current node and so works through the whole tree.</p>
<p>The real trick here is that in cases where a parent node is not a match to the desired type, the <strong>children are promoted to it&#8217;s place in the hierarchy</strong> before the parent is removed. This way qualifying children stay in the list organized in the proper fashion.</p>
]]></content:encoded>
			<wfw:commentRss>http://rightbrainleft.net/2009/11/how-to-trim-hierarchical-data-without-breaking-it/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Pushing a file to the user to download with a programmatically generated name in ASP.NET</title>
		<link>http://rightbrainleft.net/2009/11/pushing-a-file-to-the-user-to-download-with-a-programmatically-generated-name-in-asp-net/</link>
		<comments>http://rightbrainleft.net/2009/11/pushing-a-file-to-the-user-to-download-with-a-programmatically-generated-name-in-asp-net/#comments</comments>
		<pubDate>Tue, 03 Nov 2009 16:48:28 +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[attachment]]></category>
		<category><![CDATA[contentType]]></category>
		<category><![CDATA[download]]></category>
		<category><![CDATA[Response]]></category>
		<category><![CDATA[Stream]]></category>

		<guid isPermaLink="false">http://rightbrainleft.net/?p=99</guid>
		<description><![CDATA[I’ve been saving the files that I am receiving via user uploads with a unique identifier as the name (ie. 8b8c9145-1ce1-4cd1-99a7-fc57871b5671.pdf) so we don’t receive collisions with like named files. Here’s how I rewrite the filename to its’ original name (loaded from the database) before sending it back to the user. Using this you can [...]]]></description>
			<content:encoded><![CDATA[<p>I’ve been saving the files that I am receiving via user uploads with a unique identifier as the name (ie. 8b8c9145-1ce1-4cd1-99a7-fc57871b5671.pdf) so we don’t receive collisions with like named files. Here’s how I rewrite the filename to its’ original name (loaded from the database) before sending it back to the user. Using this you can make up any appropriate file name (ie UserReport_CurrentDate.xls).</p>
<pre class="brush: csharp;">

// Load the physical file into a byte array

byte[] byteArray = new byte[file.Size];
System.IO.Stream stream = file.GetFile();
stream.Read(byteArray, 0, file.Size);
stream.Close();

// Push the byte array to the user as a download
context.Response.BinaryWrite(byteArray);
context.Response.ContentType = file.MimeType;
context.Response.AddHeader(&quot;content-disposition&quot;, &quot;attachment;filename=&quot; + context.Server.UrlEncode(file.FileName));
</pre>
]]></content:encoded>
			<wfw:commentRss>http://rightbrainleft.net/2009/11/pushing-a-file-to-the-user-to-download-with-a-programmatically-generated-name-in-asp-net/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Field Validators Disabled with Display Instead of Visibility</title>
		<link>http://rightbrainleft.net/2009/09/field-validators-disabled-with-display-instead-of-visibility/</link>
		<comments>http://rightbrainleft.net/2009/09/field-validators-disabled-with-display-instead-of-visibility/#comments</comments>
		<pubDate>Fri, 25 Sep 2009 13:28:22 +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[CSS]]></category>
		<category><![CDATA[Validators]]></category>

		<guid isPermaLink="false">http://rightbrainleft.net/?p=89</guid>
		<description><![CDATA[ASP.NET&#8217;s Field validators are super useful, but one thing has always bothered me. By default when they don&#8217;t want the control to display it sets the style of the element to &#8220;visibility:hidden&#8220;. This is a problem most of the time, as the HTML element still occupies that space on the screen, it&#8217;s just invisible. My [...]]]></description>
			<content:encoded><![CDATA[<p>ASP.NET&#8217;s Field validators are super useful, but one thing has always bothered me. By default when they don&#8217;t want the control to display it sets the style of the element to &#8220;<strong>visibility:hidden</strong>&#8220;. This is a problem most of the time, as the HTML element still occupies that space on the screen, it&#8217;s just invisible. My preference is for it to be &#8220;<strong>display:none</strong>&#8221; as it will no longer take up space in the DOM and other elements will flow into it&#8217;s place (ie. other required field validators).</p>
<blockquote><p><code>&lt;asp:RequiredFieldValidator ID="valFirstNameReq" runat="server" ControlToValidate="txbFirstName" SetFocusOnError="true" ErrorMessage="Please enter your first name." <span style="color: #ff0000;">Display="Dynamic"</span> ValidationGroup="cgstpForm" /&gt;</code></p></blockquote>
<p>There it is. The property <strong>Display=&#8221;Dynamic&#8221;</strong> on a validation control switches it to this method. I found the naming of this to be non intuitive, since display set to dynamic makes sense now that i know it, but i never would have thought it had to do with this.</p>
]]></content:encoded>
			<wfw:commentRss>http://rightbrainleft.net/2009/09/field-validators-disabled-with-display-instead-of-visibility/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<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>
		<item>
		<title>Top and Bottom Pagination on a Page Using Events</title>
		<link>http://rightbrainleft.net/2009/09/top-and-bottom-pagination-on-a-page-using-events/</link>
		<comments>http://rightbrainleft.net/2009/09/top-and-bottom-pagination-on-a-page-using-events/#comments</comments>
		<pubDate>Wed, 09 Sep 2009 16:22: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[Delegates]]></category>
		<category><![CDATA[Events]]></category>

		<guid isPermaLink="false">http://rightbrainleft.net/?p=31</guid>
		<description><![CDATA[Here are a few approaches if you&#8217;d like to have a customized pager control in multiple places on a page. Datagrid will do this for you, but if for whatever reason that doesn&#8217;t solve your problem this will get the job done! Create a Pager control Create a UserControl for your pager (mine is called [...]]]></description>
			<content:encoded><![CDATA[<p>Here are a few approaches if you&#8217;d like to have a customized pager control in multiple places on a page. <a href="http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.datagrid.allowpaging.aspx" target="_blank">Datagrid will do this for you</a>, but if for whatever reason that doesn&#8217;t solve your problem this will get the job done!</p>
<h4>Create a Pager control</h4>
<p>Create a <a href="http://quickstarts.asp.net/QuickStartv20/aspnet/doc/ctrlref/userctrl/default.aspx" target="_blank">UserControl</a> for your pager (mine is called Pagination.ascx). On the Page that will contain the pagination, register the Pagination UserControl and drop two of them on there.</p>
<h4>Handling CurrentPage and PageSize values on the pager</h4>
<p>The pagers will need two numbers to function; the current page of results, and records per page. The super duper easy way to save and load this state is to read the values from GET variables and store them as Parameters. You can go ahead and do this on the pagers themselves and you are done! When the value is changed via a dropdownlist for the PageSize or clicking to navigate to a new page, just Response.Redirect back to the same page with your values in GET (ie ?currentpage=1&amp;pagesize=15).  In fact if you have to get this done right away just go do that. It&#8217;s also worth noting that this makes it possible for users to bookmark searches (the below method does not).</p>
<h4>The Event Driven Way: Handling the values on the containing element</h4>
<p>As an alternative, lets say that you don&#8217;t wanna use GET variables and want to keep the info in a central location. When you first load the page, initialize the two variables and <a href="http://haacked.com/archive/2007/03/16/gain-control-of-your-control-state.aspx" target="_blank">stuff the values into a serializable struct and save/load the control state of the page</a>. Now the values are in one place and reliable.</p>
<h4>Pass the values down to the child elements</h4>
<p>Go ahead and write some code into the Pagination.ascx control to insert the values from the parent. To do this, you will need to create public Properties on the Pagination.ascx control in the codebehind file. Make a property for PageSize and CurrentPage as int. In the codebehind for your containing page, within the OnLoad method set these properties with your values like so&#8230;</p>
<blockquote><p><code>cstTopPagination.PageSize = this.PageSize;<br />
cstTopPagination.CurrentPage = this.CurrentPage;<br />
cstBottomPagination.PageSize = this.PageSize;<br />
cstBottomPagination.CurrentPage = this.CurrentPage;</code></p></blockquote>
<p>So just before the pagers render, they will have the proper values and will be able to display as needed!</p>
<h4>What to do when the pager changes</h4>
<p>Here is where events come in. Even if you are pretty new to .NET you&#8217;ve likely utilized some events on Microsoft controls such as the DataGrid&#8217;s OnItemDataBound event or DropDownList&#8217;s OnSelectIndexChanged. We&#8217;re gonna make some of our own! Place the following lines into the codebehind for your Pagination UserControl&#8230;</p>
<blockquote><p><code>public delegate void PageSizeChangedHandler(int PageSize);<br />
public delegate void CurrentPageChangedHandler(int CurrentPage);<br />
public event PageSizeChangedHandler PageSizeChanged;<br />
public event CurrentPageChangedHandler CurrentPageChanged;</code></p></blockquote>
<p>You can read more <a href="http://www.akadia.com/services/dotnet_delegates_and_events.html" target="_blank">about how the above works</a>, but in short the event serves as a method that you will call to signal &#8216;Hey, I&#8217;m changing the page size&#8217;.  Any controls that link to this event will be notified, this is called <a href="http://www.odetocode.com/code/94.aspx" target="_blank">Event Bubbling</a>. The delegate is a contract with the controls consuming the event that tells them how they must create their methods to comply with the event. Think of it as a kind of interface.</p>
<p>Here is an example from Pagination.ascx.cs of how I call the event when a user clicks on a LinkButton with the CommandName of &#8220;Navigate&#8221; and a CommandArgument of a page number.</p>
<blockquote><p><code>protected void CommandFilter(object sender, CommandEventArgs e)<br />
{<br />
switch (e.CommandName)<br />
{<br />
case "Navigate":<br />
OnCurrentPageChanged(int.Parse(e.CommandArgument.ToString()));<br />
break;<br />
default:<br />
break;<br />
}</code></p>
<p>}</p></blockquote>
<p>Any controls that link into the Event CurrentPageChanged will be alerted with the value int.Parse(e.CommandArgument.ToString()). You&#8217;ll notice also that I call OnCurrentPageChanged not CurrentPageChanged. .NET creates this handle for you for your event.</p>
<p>On your containing page, you&#8217;ve probably got something similar to the following where your pagination control is on the page&#8230;</p>
<blockquote><p><code>&lt;rbl:Pagination ID="cstBottomPagination" runat="server" /&gt;</code></p></blockquote>
<p>Modify it like so&#8230;</p>
<blockquote><p><code>&lt;rbl:Pagination ID="cstBottomPagination" runat="server" <span style="color: #ff0000;">OnPageSizeChanged="PageSizeChanged" OnCurrentPageChanged="CurrentPageChanged"</span> /&gt;</code></p></blockquote>
<p>See how the events are accessible on the control? Now go to your codebehind for the page and add a method for PageSizeChanged&#8230;</p>
<blockquote><p><code>protected void PageSizeChanged(int size)<br />
{<br />
this.PageSize = size;<br />
this.CurrentPage = 1;<br />
}</code></p></blockquote>
<p>There you go. This officially completes the cycle; Value is loaded in the containing page, passed down to child, if it changes it is bubbled back, and the value is saved on the containing page at the end. This method also works if you have a UserControl within a UserControl and you&#8217;d like to bubble the event up.</p>
<p>There&#8217;s lots of practical and creative uses for events. This implementation is simple but it was fairly easy to learn and has potential for very powerful and smart controls. It cuts down on &#8216;black magic&#8217; in your child controls where information is obtained circumstantialy or through unpredicatble traversal. Events are clean and readable once you understand them, and I like them alot.</p>
]]></content:encoded>
			<wfw:commentRss>http://rightbrainleft.net/2009/09/top-and-bottom-pagination-on-a-page-using-events/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

