Amy
Amy

Reputation: 131

Confirm message upon gridview update

I'm having one gridview which is bound to dataset and set the 'AutoGenerateEditButton' to true. When user click 'Edit', there is two options available as usual ('Update'/'Cancel'). Once user did some changes on gridview data and click 'Update', I want to show confirm message (eight client/server). If user click 'No', abort the server event (RowUpdating). If user choose 'yes', call server event to update into database.

Gridview would look like this:-

<asp:GridView ID="gvUserList" runat="server" GridLines="None"
                Width="100%" AutoGenerateColumns="False" OnRowCancelingEdit="gvUserList_RowCancelingEdit"
                OnRowEditing="gvUserList_RowEditing" OnRowDataBound="gvUserList_RowDataBound"
                OnRowUpdating="gvUserList_RowUpdating" AutoGenerateEditButton="True">

At code behind, gridview will bind with dataset.

gvUserList.DataSource = ds;
gvUserList.DataMember = "ExistingUsers";
gvUserList.DataBind();

I have google around and there are confirm message for gridview delete action. Nothing found for update action.

Appreciate any advice. Thanks.

Upvotes: 0

Views: 4060

Answers (2)

invernomuto
invernomuto

Reputation: 10211

EDIT

You should not use use AutoGenerateEditButton, but instead a template

JavaScript

<script type="text/javascript" language="javascript">
  function ConfirmOnDelete(){
   return confirm("Are you sure to delete the item?")==true)
  }
</script>

Template Field

The LinkDelete_Click is the server-side method to invoke to delete your item

<asp:TemplateField>
 <ItemTemplate>
  <asp:LinkButton ID=" LinkDelete " runat="server" CommandName="Delete" CommandArgument='<%# Eval("YourPrimaryKey") %>' OnClientClick="return ConfirmOnDelete();">Delete</asp:LinkButton>
 </ItemTemplate>
</asp:TemplateField>

C#

protected void GridView1_RowCommand(object sender, 
                         GridViewCommandEventArgs e)
{
  if (e.CommandName == "Delete")
  {
    // get the primary key id of the clicked row
    int id= Convert.ToInt32(e.CommandArgument);
    // Delete the record 
    DeleteRecordByPrimaryKey(id);// Implement this on your own :) 

  }
}

Take a look to this article to another approach

Upvotes: 1

Anuraj
Anuraj

Reputation: 19598

Try this, using RowDataBound event. Also make sure the control type and control location.

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowState == DataControlRowState.Edit)
    {
        LinkButton lb = e.Row.Cells[0].Controls[0] as LinkButton;
        lb.OnClientClick = "return confirm('Are you sure want to update?');";
    }
}

In this code, my update button is a LinkButton which is in First Cell, first control.

Upvotes: 0

Related Questions