1

I have a GridView control and I have enabled editing.

My edit & update buttons are both LinkButtons, as below:

<asp:LinkButton ID="buttonEdit" runat="server" Text="Edit" CausesValidation="false"
                            CommandName="Edit" />

<asp:LinkButton ID="buttonUpdate" runat="server" CausesValidation="True"
                            CommandName="Update" Text="Update" ValidationGroup="Edit" />

When the user click the edit button one of the columns has a textbox which allows the record to be edited:

<EditItemTemplate>
    <asp:TextBox ID="textBoxEdit" runat="server" Text='<%#Eval("Name") %>' />
    <asp:Label ID="labelEditWarning" CssClass="error" runat="server" Text="Name already exists" Visible="false" />
</EditItemTemplate>

When the user clicks the Update link button the grid's RowCommand Event fires. Within here I want to perform validation against existing records in the database. If validation fails I then what to stop the grid's RowUpdating event firing but there seems no way to do this!?

protected void gridName_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName.Equals("Edit"))
    {
        //Perform validation & cancel update if the validation fails.
    }
}

protected void gridName_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
     //Update my record. But I don't want this to fire if my validation fails in 
     //the row command event.
}

Can anyone help?

I'm using ASP.Net 4.0

Thanks in advance.

Sun
  • 4,458
  • 14
  • 66
  • 108

3 Answers3

1

Change the CommandName on 'buttonUpdate' from 'Update' to something like 'rename'. This will stop the RowUpdating event from firing. You can then add some code in the RowCommand event to handle the validation and updating of record e.g.

protected void gridName_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName.Equals("rename"))
    {
        if(validation == true)
        {
          DatabaseDataContext data = new DatabaseDataContext();

          string rowID = e.CommandArguement.ToString();
          var rowToUpdate = data.TableOne.Where(d => d.ID.ToString() == rowID);
          rowToUpdate.Name = newName;

          data.SubmitChanges();
        }
        else
        {
          //Set error label
        }
    }
}

You also need to change the CommandArguement of the Button to something like:

<asp:LinkButton ID="buttonUpdate" runat="server" CausesValidation="True"
                        CommandName="Update" CommandArguement='<%# Eval("ID") %>' Text="Update" ValidationGroup="Edit" />
chead23
  • 1,859
  • 11
  • 12
  • Thanks for the reply but if I do this then I have no way of knowing which row is being changed. I do this at the moment using the GridViewUpdateEventArgs on the gridName_RowUpdating event – Sun Sep 04 '12 at 13:14
  • You can do: DataBinder.Eval(e.Row.DataItem,"ID"); and then use this to find the record in the database. – chead23 Sep 04 '12 at 13:31
  • Apologies I think that may be wrong as I have just tested it and it doesn't seem to like it. Another way would be to have the ID of the row in the CommandArguement of the Button like <%# Eval("ID") %>. Then use the value of this to find the record in the database. – chead23 Sep 04 '12 at 13:38
0

You can update in you gv_RowCommand event

Sample code :

protected void gridName_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName=="Edit")
    {
        //Perform validation & cancel update if the validation fails.
    }

   if(your validtaion successconditon flag set true)
    {    
       if (e.CommandName == "Update")
       {
       }
    }
}
Satinder singh
  • 10,100
  • 16
  • 60
  • 102
  • Thanks for the reply. How will this stop the RowUpdating event? It's still going to fire and the record will be updated. – Sun Sep 04 '12 at 12:50
0

You can also cancel the update by using the GridViewUpdateEventArg's Cancel property.

e.g.

   protected void GridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
      // Your validation logic goes here...

     // If validation logic fails...
     e.Cancel = true;


    }
user2557397
  • 11
  • 1
  • 3