ASP.NET EventTarget Trapping ("which button was clicked?")

By Peter A. Bromberg, Ph.D.

Peter Bromberg  

This is one that has come up so often, I thought it would be a good idea to illustrate it. What I am referring to is the "How can I tell which button fired the postback!" question. Well, the answer is there in the Docs if you take the time to look. I put up these little gems usually when a search of the web reveals NO usable example for what I'm trying to find an answer to. With my superior searching capability, which consists primarily of typing search terms into the Google main page with quotation marks surrounding any key phrase, I can tell you with absolute certainty that as of December 10, 2001, Google reports that "Searching 1,610,476,000 web pages" did not find a suitable answer to my question! That is, until NOW, because as soon as this page is indexed by Google you'll even be able to search on "How can I tell which button was clicked" and you'll find the answer here!



The key to determining which button was pressed inside an ASP.NET Form is to use the CommandEventArgs collection (Not the "EventArgs" one). This exposes both CommandArgument and CommandName properties and is part of the System.Web.UI.WebControls Namespace.

Each button should have a CommandName attribute, a CommandArgument attribute, and of course an OnCommand Event Handler attribute. These items "give Iife" to the lowly server-side asp:button control and serve to completely disambiguate any actions. So an example button declaratively placed on a page would look like so:

<asp:Button id="Button1" Text="Sort" CommandName="Sort" CommandArgument="Ascending" OnCommand="Command_Button_Click" runat="server"/>

When a button is pressed, we have a Command_Button_Click event handler method in our page, and we can iterate our button collection with a simple switch case statement. Here is a complete page illustating the procedure in all its glory:

<html>
<head>
<script language="C#" runat=server>

void Command_Button_Click(Object sender, CommandEventArgs e)
{
switch(e.CommandName)
{
case "Sort":
Message.Text ="You clicked the SORT button";
break;
case "Alternative":
Message.Text ="You clicked the ALTERNATE button";
break;
case "Another":
Message.Text ="You clicked ANOTHER button";
break;
}
}
</script>
</head>
<body>
<form method="post" runat="server">
<h3><font face="Verdana">CommandEventArgs Example</font></h3>
Select a command:<br><br>
<asp:Button id="Button1"
Text="Sort"
CommandName="Sort"
CommandArgument="Ascending"
OnCommand="Command_Button_Click"
runat="server"/>

<asp:Button id="Button2"
Text="Alternate"
CommandName="Alternative"
OnCommand="Command_Button_Click"
runat="server"/>

<asp:Button id="Button3"
Text="Another"
CommandName="Another"
OnCommand="Command_Button_Click"
runat="server"/>
<br><br>
<asp:Label id="Message" runat="server"/>
</form>
</body>
</html>


So the next time you hear "How do I tell which button was clicked, you'll know - its all in the CommandEventArgs!

 

 


Peter Bromberg is an independent consultant specializing in distributed .NET solutionsa Senior Programmer /Analyst at in Orlando and a co-developer of the NullSkull.com developer website. He can be reached at info@eggheadcafe.com