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

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>