C
clintonG
A Matter of Design: OnCommand or OnClick Event Handler?
Attributing a control using OnCommand supports the use of other attibutes
such as CommandName and CommandArgument. AFIK OnClick does not.
Where it matters when using OnCommand for example is the value of the
CommandArgument seems to be easier to use to indicate some behavior in that
can be handled in the event handler that would have to use a value from some
other control such as the state of a radio button to indicate behavior a
click event should handle.
Is it a good practice to use OnCommand this way?
I really don't understand the intended use of OnCommand yet. Command what
for example?
It seems like a good idea to use the OnCommand CommandArgument and I sure
like the idea of accessing properties using e which is supported in
IntelliSense when it means one or less controls need to be used to indicate
the context of some desired behavior as following examples imply...
// Command event handler: succint, terse and readable and context relevant
protected void BuildNewFileButton_Command(Object sender, CommandEventArgs e)
{
if(e.CommandArgument == "NoExtensions")
Response.Redirect("Somewhere.aspx");
else
Response.Redirect("~/HomePage.aspx");
}
// Click event handler: verbose
protected void BuildNewFileButton_Click(Object sender, EventArgs e)
{
// PITA when using MasterPages
Button buildNewFileRadioButton =
(Button)Master.FindControl("...").FindControl("...").FindControl("...");
if(buildNewFileBuildRadioButton == "Selected")
Response.Redirect("Somewhere.aspx");
else
Response.Redirect("~/HomePage.aspx");
}
Your comments?
Attributing a control using OnCommand supports the use of other attibutes
such as CommandName and CommandArgument. AFIK OnClick does not.
Where it matters when using OnCommand for example is the value of the
CommandArgument seems to be easier to use to indicate some behavior in that
can be handled in the event handler that would have to use a value from some
other control such as the state of a radio button to indicate behavior a
click event should handle.
Is it a good practice to use OnCommand this way?
I really don't understand the intended use of OnCommand yet. Command what
for example?
It seems like a good idea to use the OnCommand CommandArgument and I sure
like the idea of accessing properties using e which is supported in
IntelliSense when it means one or less controls need to be used to indicate
the context of some desired behavior as following examples imply...
// Command event handler: succint, terse and readable and context relevant
protected void BuildNewFileButton_Command(Object sender, CommandEventArgs e)
{
if(e.CommandArgument == "NoExtensions")
Response.Redirect("Somewhere.aspx");
else
Response.Redirect("~/HomePage.aspx");
}
// Click event handler: verbose
protected void BuildNewFileButton_Click(Object sender, EventArgs e)
{
// PITA when using MasterPages
Button buildNewFileRadioButton =
(Button)Master.FindControl("...").FindControl("...").FindControl("...");
if(buildNewFileBuildRadioButton == "Selected")
Response.Redirect("Somewhere.aspx");
else
Response.Redirect("~/HomePage.aspx");
}
Your comments?