<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>DevChix » ASP.NET</title>
	
	<link>http://www.devchix.com</link>
	<description>Boys can't have all the fun</description>
	<pubDate>Sat, 04 Jul 2009 06:18:34 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.7.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" href="http://feeds.feedburner.com/devchix/bsuF" type="application/rss+xml" /><feedburner:browserFriendly></feedburner:browserFriendly><item>
		<title>ASP.NET - ‘PASSING PARAMETERS’ IN BUTTON CLICK-HANDLER</title>
		<link>http://www.devchix.com/2007/08/10/aspnet-passing-parameters-in-button-click-handler/</link>
		<comments>http://www.devchix.com/2007/08/10/aspnet-passing-parameters-in-button-click-handler/#comments</comments>
		<pubDate>Fri, 10 Aug 2007 20:46:04 +0000</pubDate>
		<dc:creator>Kris</dc:creator>
		
		<category><![CDATA[.NET]]></category>

		<category><![CDATA[ASP.NET]]></category>

		<category><![CDATA[Frameworks]]></category>

		<category><![CDATA[Languages]]></category>

		<guid isPermaLink="false">http://www.devchix.com/2007/08/10/aspnet-passing-parameters-in-button-click-handler/</guid>
		<description><![CDATA[A recent posting to a technical mailing list I&#8217;m on asked how to pass a parameter to a button click-event handler. The programmer wanted to do the equivalent of this:
&#60;asp:button ID="btnSubmit" runat="server" OnClick="Btn_Onclick(PARAM)" /&#62;
But, she knew this didn&#8217;t work, because the default signature of event handlers in ASP.NET is this:

void myBtn_OnClick(Object sender, EventArgs e)
So, she [...]]]></description>
			<content:encoded><![CDATA[<p>A recent posting to a technical mailing list I&#8217;m on asked how to pass a parameter to a button click-event handler. The programmer wanted to do the equivalent of this:</p>
<pre>&lt;asp:button ID="btnSubmit" runat="server" OnClick="Btn_Onclick(PARAM)" /&gt;</pre>
<p>But, she knew this didn&#8217;t work, because the default signature of event handlers in ASP.NET is this:</p>
<pre>
void myBtn_OnClick(Object sender, EventArgs e)</pre>
<p>So, she was stumped on how to pass some sort of argument to the event handler.</p>
<p>Now, there a certainly some kludges that would work, such as having a hidden form field which contains the &#8216;parameter&#8217; value, and having the event-handler get the value that way. But surely, there&#8217;s a better way? Well, yes, of course there is! :)</p>
<p>This better way is to use the &#8220;CommandArgument&#8221; property on the button control. This property is directly accessable in the event handler. The added bonus is that you can write a single even-handler for all of your button clicks, and use the associated &#8220;CommandName&#8221; property to know which button fired. </p>
<p>So, now, in your aspx page, your button object will be declared thusly:</p>
<pre>&lt;asp:Button ID="test" runat="server" CommandArgument="MyVal" CommandName="ThisBtnClick" OnClick="MyBtnHandler" /&gt;</pre>
<p>Then, in the OnClick handler, you can cast the sender object to the correct type, and get the values of<br />
 CommandName &amp; CommandArgument.  (You could make this even more generic and handle the OnClick event for different senders, but we&#8217;ll leave that for a later post! :)</p>
<pre>
void MyBtnHandler(Object sender, EventArgs e)
{
	 Button btn = (Button)sender;
          switch (btn.CommandName)
          {
                case "ThisBtnClick":
                    DoWhatever(btn.CommandArgument.ToString());
                    break;
                case "ThatBtnClick":
                    DoSomethingElse(btn.CommandArgument.ToString());
                    break;
           }
}
</pre>
<p>You can see that the switch statement uses the CommandName property to determine which button was clicked, and then can use the CommandArgument property passed in by the button click event.</p>
<p>Voila! You&#8217;ve &#8220;passed a parameter&#8221; to a button click-event handler!</p>
<p>[UPDATE] Thanks to Jim for pointing out some confusion in my sample code. I hope that this version is clearer.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.devchix.com/2007/08/10/aspnet-passing-parameters-in-button-click-handler/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
