what is the purpose of command name and command ar

2020-06-03 01:18发布

what is the purpose of command name and command argument for a control example button? when should we go for this?

标签: asp.net
7条回答
女痞
2楼-- · 2020-06-03 01:25

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.

查看更多
3楼-- · 2020-06-03 01:26

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.

查看更多
够拽才男人
4楼-- · 2020-06-03 01:26

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.

查看更多
来,给爷笑一个
5楼-- · 2020-06-03 01:34

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.

查看更多
beautiful°
6楼-- · 2020-06-03 01:36

CommandName:

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.

(My emphasis)

CommandArgument:

An optional parameter passed to the Command event along with the associated CommandName.

...which you retrieve from the event object (for instance, e) as e.CommandArgument.

So for instance, you might have four buttons with the CommandName "DoSomething" but four different CommandArguments telling your "DoSomething" code what to do, or what to do it to. You might also have other buttons with CommandName = "DoSomethingElse" that, well, do something else. :-)

查看更多
一夜七次
7楼-- · 2020-06-03 01:41

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;
    }
}
查看更多
登录 后发表回答