one thing that i have been searching since many days that confuses me is that the difference between ONCOMMNAD and ONROWCOMMAND,
Oncommand methid use parameters like "DELTEROW" EDITROW while ONROWCOMMAND uses DELETE, EDIT etc and both send different kinda objects like one send GridviewEventargs type object while another sends commandeventargs,

WHY ? Where to use ? HOW ?

Recommended Answers

All 3 Replies

They are used for similar reasons, but one is used with the GridView control while the other is used for controls outside of the gridview.

OnRowcommand is used in the GridView control. The RowCommand event is raised say when a button is clicked in the GridView control. This enables you to provide an event-handling method that performs a custom routine whenever this event happens. You specify the OnRowCommand within the gridview control, and the CommandName on the control, say the button that is in the field (column) of the gridview.

OnCommand is used outside of the gridview for controls such as buttons, linkbuttons, imagebuttons.. The Command event is raised when the control is clicked. This is also typically used in conjuction with a commandName on the same control. For example, this allows you to create multiple Button controls on a Web page and programmatically determine which Button control is clicked by checking the commandName.

Example of OnRowCommand...

void GridView1_RowCommand(Object sender, GridViewCommandEventArgs e)
    {
      switch(e.CommandName)
     {
       case "Add":
       // code...

       case "Del":
       // code...
     }
    }  

    <asp:gridview id="GridView1" 
         ...   onrowcommand="GridView1_RowCommand" runat="server">
       <columns>
          <asp:buttonfield buttontype="Link" commandname="Add" text="Add"/>
          <asp: .........                    commandname="Del" text="Del"/>  
          ...
          </columns>
    </asp:gridview>

Example of OnCommand...

void CommandBtn_Click(Object sender, CommandEventArgs e) 
{
 switch(e.CommandName)
 {
   case "One":
   // code...

   case "Two":
   // code...
 }
}

<asp:Button id="btn1" Text="Button One" CommandName="One"
     OnCommand="CommandBtn_Click" runat="server"/>

     <br />

<asp:Button id="btn2" Text="Button Two" CommandName="Two"
     OnCommand="CommandBtn_Click" runat="server"/>

Thakns jorGem but when i use OnCommand inside GV then it works perfecty, why ? what shortcommings might come ? sometimes these both are sued interchangeably.

but when i use OnCommand inside GV then it works perfecty, why ? what shortcommings might come ? sometimes these both are sued interchangeably

Not sure... I have used both but I've only used OnRowCommand within the gridview and OnCommand for controls like buttons. I havent tried to use OnCommand in the gridview control. I'll have to take a look and see what you mean.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.