Powered by Blogger.

Friday, February 14, 2014

Show confirmation message in gridview for delete in Asp.net




In this post we will discuss how we can show a confirmation message while deleting a record from gridview in asp.net. Like it should first as us whether you want to delete the record and then if user clicks on yes then it should delete the record.

Also you can check out my previous posts on:

- RegularExpressionValidator example in Asp.Net

- Asp.Net MVC4 sample application

- How the Windows Communication Foundation’s service model works?

There are different ways you can do this:

1st Approach:
You can directly write in the OnClientClick event of the LinkButton like below:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:LinkButton ID="LinkButton1" runat="server" CommandArgument='<%#Eval("UserID") %>'
                            CommandName="Delete" OnClientClick="return confirm('Are you sure you want to delete this record?');">Delete</asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>

        </asp:GridView>

2nd Approach:
You can write a javascript function and call that function in the OnClientClick event like below:

<head id="Head1" runat="server">
    <title>Show confirmation message in gridview for delete in Asp.net</title>
    <script type="text/javascript">
        function DeleteRecord() {
            return confirm("Are you sure you want to delete this record?");
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:LinkButton ID="LinkButton1" runat="server" CommandArgument='<%#Eval("UserID") %>'
                        CommandName="Delete" OnClientClick="return DeleteRecord();">Delete</asp:LinkButton>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
    </form>

</body>



0 comments

Post a Comment