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