Click here to Skip to main content
15,881,938 members
Please Sign up or sign in to vote.
4.75/5 (4 votes)
Appreciate if u let me know :

how to display confirm dialog box before deleting a row in gridview by java script/vb script.
I search many article, but that all was making a dialog box control, which I don't like.

Simply , I want to display a confirm box before deleting a row from girdview(I am using Command button [Select,Delete,Edit,Update,cancel])
by the way of either JavaScript or VBScript.


Can anybody answer, Hoping for ur best reply


Thanx
Posted

Adding confirm dialog box in Command buttons Check this link.

GridView Delete Confirmation Using asp:CommandField[^]
 
Share this answer
 
Comments
koool.kabeer 31-Jul-10 4:43am    
hey thats what he wanted.......
in the RowDataBound Event Handler of GridView just get the reference of each ImageButton Control and either add a JavaScript function as Attributes... or just add OnClientClick property value as
"if(!confirm("Are You Sure to Delete?"))
return;"
VB
<asp:ImageButton ID="btnDelete" runat="server" CausesValidation="False" CommandName="Delete" ImageUrl="~/Image/delete.png" OnClick="btnDelete_Click" Text="Delete"
OnClientClick="return confirm('Are you sure you want to Delete this record ?')" />
 
Share this answer
 
hi friend,
see i have solution for u.
just try this.

Design your grid mentioned as below
XML
<asp:templatefield showheader="False" xmlns:asp="#unknown">
     <itemtemplate>
          <asp:linkbutton id="lnkBtn" commandname="Delete" text="Delete" onclientclick="return ConfirmDelete();">
runat="server">Delete</asp:linkbutton>
     </itemtemplate>
</asp:templatefield>


Add this java script into your aspx form
JavaScript
function ConfirmDelete()
{
    var Delet_Confirm= confirm("Do you really want to delete this record ?");
 if (Delet_Confirm== true)
 {
   return true;
 }
 else
 {
  return false;
  }
}



if you are not using template field, you can write same java script into
row command event of grid into .cs file by define <javascript> tag.


Thanks,
Mahesh Patel
 
Share this answer
 
v3
Comments
shanawazway 14-Jun-10 3:14am    
Hi Dear,
As I mentioned in my question,I am not using template field, but it is command field of gridview(Select,Delete,Edit);
How can I call JavaScript function with Gridview Command default field like "Delete".

Again help me out

Thanx
in the RowDataBound Event Handler.....
  protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        // loop all data rows
        foreach (DataControlFieldCell cell in e.Row.Cells)
        {
           // check all cells in one row
           foreach (Control control in cell.Controls)
           {
                // Must use LinkButton here instead of ImageButton
                // if you are having Links (not images) as the command button.
                ImageButton button = control as ImageButton;
                if (button != null && button.CommandName == "Delete")
                    // Add delete confirmation
                    button.OnClientClick = "if (!confirm('Are you sure " + 
                           "you want to delete this record?')) return;";
            }
        }
    }
}


Here you dont need to loop the each Cell and each Control in each Cell..
just use proper indexing... like

ImageButton = (ImageButton)e.Row.Cell[<<yourDeleteColumnIndex>>].Controls[<<getImageButtonControlIndex>>];
 
Share this answer
 
Comments
raju melveetilpurayil 31-Jul-10 7:46am    
Reason for my vote of 1
I give this solution already.
koool.kabeer 31-Jul-10 14:03pm    
sorry friend but i didn't give the link it was just a simple thing which could be seen at the bottom of my answer... which was not in that article
koool.kabeer 31-Jul-10 14:04pm    
no need to use for loops i means.... by the sorry for what ever i done...
take care
use
response.redirect("confirm('Do you really want to delete this record')")
 
Share this answer
 
Comments
Sandeep Mewara 30-Jul-10 16:25pm    
Reason for my vote of 1
Why would someone do Response.Redirect when deleting?
koool.kabeer 31-Jul-10 4:45am    
yeah dear....
can you explain why to use Response.Redirect here....
may be i can get something from it...
thanx in advance

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900