Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I want to delete record from GridView.Before to this ask for confirmation like "Are you sure to delete?"

I want to delete record from GridView.Before to this ask for confirmation like "Are you sure to delete?"

I used command field in GridView,

<asp:CommandField HeaderText="Delete" ShowDeleteButton="True" />

I wrote a function in javascript

function confirm_Delete()
{
    var r = confirm("Are you sure you want to Remove this Record!");

    if (r == true)
    {
        alert("Record Deleted");
        return true;
    }
    else 
    {
        return false;
    }
}

How I will call this on delete click. Kindly suggest !

like image 621
im useless Avatar asked Apr 01 '11 02:04

im useless


2 Answers

You can't achieve this using the command field, you have to make a template field:

<asp:TemplateField>
        <ItemTemplate>
            <asp:LinkButton ID="lbtnDelete" runat="server" CommandName="Delete" Text="Delete" 
             OnClientClick="javascript:return confirm('Are you sure you want to Remove this Record!');">
            </asp:LinkButton>
        </ItemTemplate>
    </asp:TemplateField>

It will behave the same way you are doing currently with the Command Field.

like image 67
Muhammad Akhtar Avatar answered Nov 06 '22 18:11

Muhammad Akhtar


I would do the same as @Muhammad told you, and at the server side code for deleting I would also register an script for showing the "Record Deleted" message, as follows;

public void MethodForDeletingARecord()
{
    ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "RecordDeletedMessage", "javascript:alert('Record Deleted');", true);
}
like image 5
Renato Gama Avatar answered Nov 06 '22 18:11

Renato Gama