ASP.NET - commandargument - Asked By arvind gupta on 23-Nov-11 05:08 AM

what is use of commandargument of button control
Devil Scorpio replied to arvind gupta on 23-Nov-11 05:19 AM
Hi,

The use of command argument of button control is to gets or sets an optional parameter passed to the http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.command.aspx event along with the associated http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.commandname.aspx. 

Namespace:  http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.aspx
Assembly:  System.Web (in System.Web.dll) 

Syntax:-
[BindableAttribute(true)]
[ThemeableAttribute(false)]
public string CommandArgument { get; set; }


Riley K replied to arvind gupta on 23-Nov-11 05:33 AM

In case we want to set optional parameter to the button to determine some values in the server side code, we can use this approach.

CommandArguments is a way to get or set optional parameter to the command events along with CommandName.


ASPX Page
<p><asp:Button ID="Button4" runat="server" Text="Sort in Ascending Order"
CommandName="Sort" CommandArgument="Ascending" OnCommand="SortData" /></p>
<p><asp:Button ID="Button5" runat="server" Text="Sort in Descending Order"
CommandName="Sort" CommandArgument="Descending" OnCommand="SortData" /></p>

Code behind

protected void SortData(object sender, CommandEventArgs e)
{
   string commandName = e.CommandName;
   if (commandName.Equals("Sort"))
   {
   var commandArguments = e.CommandArgument;
   if (commandArguments.Equals("Ascending"))
   {
     Response.Write("Sort in ascending order");
   }
   else if (commandArguments.Equals("Descending"))
   {
    Response.Write("Sort in descending order");
   }
   }
}


Regards
Jitendra Faye replied to arvind gupta on 23-Nov-11 05:35 AM
Use the CommandArgument property to specify a parameter that compliments the http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.commandname.aspx property.

Example-


<%@ Page Language="C#" AutoEventWireup="True" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Button CommandName Example</title>
<script runat="server">

      void CommandBtn_Click(Object sender, CommandEventArgs e) 
      {

         switch(e.CommandName)
         {

            case "Sort":

               // Call the method to sort the list.
               Sort_List((String)e.CommandArgument);
               break;

            case "Submit":

               // Display a message for the Submit button being clicked.
               Message.Text = "You clicked the Submit button";

               // Test whether the command argument is an empty string ("").
               if((String)e.CommandArgument == "")
               {
                  // End the message.
                  Message.Text += ".";
               }
               else
               {
                  // Display an error message for the command argument. 
                  Message.Text += ", however the command argument is not recogized.";
               }                
               break;

            default:

               // The command name is not recognized. Display an error message.
               Message.Text = "Command name not recogized.";
               break; 

         }

      }

      void Sort_List(string commandArgument)
      {

         switch(commandArgument)
         {

            case "Ascending":

               // Insert code to sort the list in ascending order here.
               Message.Text = "You clicked the Sort Ascending button.";
               break;

            case "Descending":

               // Insert code to sort the list in descending order here.
               Message.Text = "You clicked the Sort Descending button.";
               break;

            default:

               // The command argument is not recognized. Display an error message.
               Message.Text = "Command argument not recogized.";
               break;

         }

      }

   </script>

</head>

<body>

   <form id="form1" runat="server">

      <h3>Button CommandName Example</h3>

      Click on one of the command buttons.

      <br /><br />

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

      &nbsp;

      <asp:Button id="Button2"
           Text="Sort Descending"
           CommandName="Sort"
           CommandArgument="Descending"
           OnCommand="CommandBtn_Click" 
           runat="server"/>

      <br /><br />

      <asp:Button id="Button3"
           Text="Submit"
           CommandName="Submit"
           OnCommand="CommandBtn_Click" 
           runat="server"/>

      &nbsp;

      <asp:Button id="Button4"
           Text="Unknown Command Name"
           CommandName="UnknownName"
           CommandArgument="UnknownArgument"
           OnCommand="CommandBtn_Click" 
           runat="server"/>

      &nbsp;

      <asp:Button id="Button5"
           Text="Submit Unknown Command Argument"
           CommandName="Submit"
           CommandArgument="UnknownArgument"
           OnCommand="CommandBtn_Click" 
           runat="server"/>

      <br /><br />

      <asp:Label id="Message" runat="server"/>

   </form>

</body>
</html>
Reena Jain replied to arvind gupta on 23-Nov-11 05:37 AM
hi,

Use the CommandArgument property to specify a parameter that compliments the http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.commandname.aspx property
he CommandArgument property complements the http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.commandname.aspx property by allowing you to provide additional information about the command to perform. For example, if you set the http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.commandname.aspx property to Sort and the CommandArgument property to Ascending, you specify a command to sort in ascending order. This property cannot be set by themes or style sheet themes

refer
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.commandargument.aspx
dipa ahuja replied to arvind gupta on 23-Nov-11 05:55 AM

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 bothCommandArgument 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:

EX:

<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"/>

void Command_Button_Click(Object sender, CommandEventArgs e)
{
  switch (e.CommandName)
  {
    case "Sort":
      Response.Write("You clicked the SORT button");
      break;
    case "Alternative":
      Response.Write("You clicked the ALTERNATE button");
      break;
    case "Another":
      Response.Write("You clicked ANOTHER button");
      break;
  }
}

http://www.eggheadcafe.com/articles/20011210.asp