ASP.NET Button Commands

Something that I unbelievably didn’t know was that you can send specific commands and arguments via specific buttons in ASP.NET.

Say you have a button control on a page that has an associated onclick method called myMethod.

<asp:Button ID="myButton" Text="My Button" onclick="myMethod" />

And you want your method to process many different buttons but you don’t know how to get the method to identify which button made the call?

Just add in the CommandName and or CommandArgument properties to the button, like so:

<asp:Button ID="myButton" Text="My Button" onclick="myMethod" CommandName="createNewDriver" commandArgument="Alan Feekery" />

Now all you have to do in your method is:

Button btn = (Button)sender;

String commandName = btn.CommandName;

String commandArgs = btn.CommandArguemnt;

Resource: DevChix

Published by

Alan Feekery

.NET Web Developer, Web Designer & Windows Phone 7 enthusiast.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s