<?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; Events</title>
	<atom:link href="http://rightbrainleft.net/tag/events/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>
		<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>

