what is the purpose of command name and command argument for a control example button? when should we go for this?
You can place a button on asp:FormView
and subscribe to its ItemCommand
event.
FormViewCommandEventArgs has properties CommandName
and CommandArgument
which will be equal to your Button's if it will be pressed.
With this you can pass in extra information when a button gets clicked. Notice that it's not the Click event that gets handled but the Command event.
The place where it's mostly used is in combination with grid control in ASP.NET where standard messages are passed in like Delete, Insert, ... and which get handled in the default eventhandlers of these controls.
For an overview of this, take a look here: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowcommand.aspx.
The Command event makes it easy to write a single method that handles the clicks of multiple buttons.
protected void Button_Command(object sender, CommandEventArgs e)
{
switch (e.CommandName)
{
case "Back":
FeedbackLabel.Text = "Back";
break;
case "Up":
FeedbackLabel.Text = "Up";
break;
case "Forward":
FeedbackLabel.Text = "Forward";
break;
}
}
Have a look at command name and command argument MSDN description.
When you have multiple Button controls on a Web page, use the CommandName property to specify or determine the command name associated with each Button control. You can set the CommandName property with any string that identifies the command to perform. You can then programmatically determine the command name of the Button control and perform the appropriate actions.
The CommandArgument property complements the CommandName property by allowing you to provide additional information about the command to perform. For example, if you set the CommandName property to Sort and the CommandArgument property to Ascending, you specify a command to sort in ascending order.
CommandName is to identify which command you want to execute when a button is clicked, the event you need to handle is called Command (not Click), CommandArgument is the argument passed when you click that button, for example if you want to pass an ID.
This allows you to pass additional parameters to a buttons Command event method identifying the button that called a method. This allows you to have multiple buttons that share similar functionality on your form using the same method when they are clicked rather than implementing a separate click event method for each button.