A Matter of Design: OnCommand or OnClick Event Handler?

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top