sams5817
sams5817

Reputation: 1037

How to differentiate updating or inserting in GridView OnUpdating Event?

I'm working on my Web Application using ASP.NET, I came into a situation where I need to differentiate UPDATIng and INSERTING on GridView Updating Event.

 protected void GridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
 {
    //conditional check
    if(Update Flage){
        //Call Update Function
    }
    else{
        //Call Insert Function
    }
 }

I have a ItemTemplate and EditItemTemplate in the GridView, when click on Edit Button (on ItemTemplate), then change to Update Button (on EditItemTemplate).

enter image description here

And I have a Add Button outside of GridView, upon clicking, add a new Row into GridView and change Button text to ADD as following code fragment:

ds.Tables[0].Rows.InsertAt(ds.Tables[0].NewRow(), 0);
GridViewID.EditIndex = 0;

LinkButton cmdButton = GridView.Rows[0].FindControl("btnUpdate") as LinkButton;
cmdButton.Text = "Add";

I know there is InsertItemTemplate for row inserting, but in my situation I was using Button outside of GridView to add new editing row instead.

So, how can I differentiate editing or inserting on RowUpdating Event? Any recommended trick to achieve this? perhaps something like adding a HiddenField as a flag.

Thank you in advanced.

Upvotes: 0

Views: 998

Answers (1)

Ankush Jain
Ankush Jain

Reputation: 1527

You can identify event on basis of that command name value

eg.

 protected void GridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
     {
       string arg = e.CommandName.ToString();
    if(arg="Edit")
     {
     }
    else if(arg=="Update")
      {
      }

mark up

<asp:LinkButton ID="lnkEdit" Text="Customize"  CommandName="edit"  CommandArgument='edit'  runat="server"> 
       </asp:LinkButton>  

Upvotes: 0

Related Questions