<?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>The Married Geek » ASP.NET, jQuery, Web, JavaScript, and CSS blog &#187; delegates</title>
	<atom:link href="http://marriedgeek.com/index.php/tag/delegates/feed/" rel="self" type="application/rss+xml" />
	<link>http://marriedgeek.com</link>
	<description></description>
	<lastBuildDate>Thu, 14 Apr 2011 15:58:05 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Create Custom Event Handlers &amp; Arguments For Controls</title>
		<link>http://marriedgeek.com/2009/07/create-custom-event-handlers-and-arguments-controls-delegate-asp-net/</link>
		<comments>http://marriedgeek.com/2009/07/create-custom-event-handlers-and-arguments-controls-delegate-asp-net/#comments</comments>
		<pubDate>Wed, 29 Jul 2009 03:37:27 +0000</pubDate>
		<dc:creator>_theMarriedGeek</dc:creator>
				<category><![CDATA[ASP.NET and Web]]></category>
		<category><![CDATA[arguments]]></category>
		<category><![CDATA[asp.net]]></category>
		<category><![CDATA[composite controls]]></category>
		<category><![CDATA[custom control]]></category>
		<category><![CDATA[custom event args]]></category>
		<category><![CDATA[delegates]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[event handlers]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://www.marriedgeek.com/?p=275</guid>
		<description><![CDATA[I was hoping to get this post up earlier, but got distracted by a couple of things. First, I jacked up my hand trying to separate my dogs from fighting to the point I couldn&#8217;t even move my ring finger (long story about the dogs, but I love &#8216;em). Second, I started a new job [...]]]></description>
			<content:encoded><![CDATA[<p>I was hoping to get this post up earlier, but got distracted by a couple of things. First, I jacked up my hand trying to separate my dogs from fighting to the point I couldn&#8217;t even move my ring finger (long story about the dogs, but I love &#8216;em). Second, I started a new job at <a href="http://groovecommerce.com" target="_blank">Groove Commerce</a> on Monday. So far, so good. I feel it&#8217;ll keep me on my game and give me some nice challenges ahead.</p>
<p>A few entries ago we talked about custom server controls and mentioned how to structure a composite control to allow the ViewState to &#8220;pick up&#8221; on it&#8217;s post back content so that data isn&#8217;t re-binded with every call. It was a very basic composite control, but it showed how to add native WebControl objects to the Controls tree to create a basic layout without using a .ASCX design file. This type of control can be added to your <em>app_code</em> directory or manually compiled as an assembly and placed into your <em>bin </em>directory. The advantage of the latter is to feasibly add it to your Visual Studio toolbar by loading the assembly. This time, we&#8217;ll add a custom event to our control using a delegate for an event, as well as a custom event argument class to pass along with it.</p>
<p>We&#8217;ll take the code from last time, but add several things which are highlighted in green.</p>
<ul>
<li>A line to declare our event delegate variable and the delegate itself.</li>
<li>A class which will be used for our arguments.</li>
<li>A binded event for the button Click.</li>
<li>A line in the button Click handler to invoke the event delegate.</li>
</ul>
<pre>using System;
using System.Web;
using System.Web.UI.WebControls;
using System.ComponentModel;

namespace RyanControls
{
    public class MyCompositeControl : CompositeControl
    {
        public MyCompositeControl() { }

        <span style="color: #339966;">// Declare the Updating event of delegate type CustomControlUpdateHandler</span>
        <span style="color: #339966;"><strong>public event CustomControlUpdateHandler Updating;</strong></span>

        protected override void CreateChildControls()
        {
            TextBox txtMyData = new TextBox();
            DropDownList drpMyList = new DropDownList();
            Button btnUpdate = new Button() { Text = "Update!" };

            Controls.Add(drpMyList);
            Controls.Add(txtMyData);
            Controls.Add(btnUpdate);

            if (!Page.IsPostBack)
            {
                drpMyList.Items.Add(new ListItem("item 1"));
                drpMyList.Items.Add(new ListItem("item 2"));
                drpMyList.Items.Add(new ListItem("item 3"));
            }

            <span style="color: #339966;">// Add a handler for the button click to launch the Updating event</span>
            <strong><span style="color: #339966;">btnUpdate.Click += new EventHandler(FireEvent);</span></strong>
        }
<strong>
<span style="font-weight: normal;"><span style="color: #339966;">        // The method that handles the Click event from the button
        // and triggers the Updating event by calling the delegate</span></span>
        <span style="color: #339966;">protected void FireEvent(Object s, EventArgs e)
        {</span></strong><span style="color: #339966;">
</span><strong><span style="color: #339966;">            Updating(s, new CustomControlUpdateEventArgs() { SomeData = "The control was updated!" });
        }</span>
<span style="color: #000000; font-weight: normal;">    }</span></strong><span style="color: #339966;"><span style="color: #000000;">
</span><strong>
<span style="font-weight: normal;">    // Our custom event class that inherits EventArgs. You may use
    // EventArgs as the class if you don't have additional data to pass</span></strong><strong>
    public class CustomControlUpdateEventArgs : EventArgs
    {
        public CustomControlUpdateEventArgs() { }
        public String SomeData;
    }

<span style="font-weight: normal;">    // The delegate declaration (or template, as I call it) for the handler</span></strong><strong>
    public delegate void CustomControlUpdateHandler(Object s, CustomControlUpdateEventArgs e);</strong></span>
}</pre>
<p>There you have it. If you place this code into a .CS file inside your app_code directory, you&#8217;ll be able to include the control on your pages. If you compiled it into an assembly, toss it into your <em>bin</em> directory and feel free to add it to your toolbar in VS or VWD.</p>
<pre>&lt;asp:MyCompositeControl runat="server" ID="cntMyControl" OnUpdating="DisplayUpdate" /&gt;</pre>
<p>By assigning an event handler for the Updating event (the page automatically prepends the &#8220;On&#8221; for &#8220;OnUpdating&#8221;) to call a method to handle the update, you can use data contained in the CustomControlUpdateEventArgs object. In this case, there&#8217;s only a public string called <em>SomeData</em>.</p>
<pre>protected void DisplayUpdate(Object s, RyanControls.CustomControlUpdateEventArgs e)
{
    Response.Write(String.Format("&lt;div&gt;From the event handler: {0}&lt;/div&gt;", e.SomeData));
}</pre>
<p>When you run the code and click the button, the method will be invoked and the message displayed. Similarly, you can bind the handler programmaticallyÂ by adding <em>cntMyControl.Updating += new RyanControls.CustomControlUpdateHandler(DisplayUpdate)</em> in your code-behind.</p>
<p><a href="http://www.marriedgeek.com/wp-content/uploads/2009/07/custom_event.JPG"><img class="alignright size-medium wp-image-298" title="custom_event" src="http://www.marriedgeek.com/wp-content/uploads/2009/07/custom_event-300x62.jpg" alt="custom_event" width="300" height="62" /></a></p>
<p>So again, while the code isn&#8217;t really useful, it shows how to create your own event handlers and custom argument class(es) for a composite server control. The point is to be able to create one or more event handlers, as well as combine those with one or more types of event argument classes. The fields of that particular argument class should be specific to that type of event and it is best not to use one custom event args class to satisfy many types of event argsuments, unless the data being passed into your hander functions generic enough to do so.</p>
<p>Happy coding!</p>
]]></content:encoded>
			<wfw:commentRss>http://marriedgeek.com/2009/07/create-custom-event-handlers-and-arguments-controls-delegate-asp-net/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->
