Question

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.

Was it helpful?

Solution

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" />

OTHER TIPS

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")
       {
       }
    }
}

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;


    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top