Display Confirmation Message on GridView Deleting (ASP.NET 3.5 Visual Studio 2008)

Super dad movies Add comments

I want to share how to get a confirmation popup when deleting a record from a ASP.NET 3.5 Gridview. 
Here’s how you can do it.

First create the javascript function and place it in your head section.

<head runat=”server”>
    <title>GridView Data Manipulation</title>
    <script type=”text/javascript” language=”javascript”>
        function ConfirmOnDelete()
        {
           return confirm(“Are you sure to delete ?”);
        }
    </script>
</head>

Then add a Template field in your gridview like shown below, look at the CommandName attribute, that’s important. 


    <asp:GridView ID=”TypesGridView” runat=”server” AllowSorting=”True” 
        AutoGenerateColumns=”False” DataKeyNames=”Id” DataSourceID=”CustomerDS”>
<Columns>
     <asp:BoundField DataField=”CustomerID” HeaderText=”ID” ReadOnly=”true” />
     <asp:BoundField DataField=”CompanyName” HeaderText=”Company”/>
     <asp:BoundField DataField=”ContactName” HeaderText=”Name”/>
     <asp:BoundField DataField=”ContactTitle” HeaderText=”Title” />
     <asp:BoundField DataField=”Address” HeaderText=”Address”/>
     <asp:BoundField DataField=”Country” HeaderText=”Country”/>

        <asp:TemplateField>
        <ItemTemplate>
        <asp:LinkButton
ID=”LinkDelete” runat=”server” CommandName=”delete” OnClientClick=”return ConfirmDelete();”>Delete</asp:LinkButton>
         </ItemTemplate>
         </asp:TemplateField>

</Columns>
    </asp:GridView>

I hope you liked this post, if so consider bying me this Twitter shirt!

Posted via email from wiibart’s posterous



Comments are closed.

WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS Log in