Generate Javascript Confirm Delete Alert Message for Runtime Dynamically created Asp.Net LinkButton or Button Control C# VB.Net

Promting a Javascript Confirm delete alert message before deleting row from database or from datatable is a most frequest job for us. In my previous post i have described “Javascript Confirm Delete Alert Message before Deleting from Database C# VB.Net” for fixed controls. But now I am going to explain a bit advance option. In many cases you need to generate controls dynamically or runtime like generating GridView in runtime & add a LinkButton in GridView rows to give an option to user to delete data then you need to add Javascript Confirm Message Dialog in run time. Look at the below code how one can add dynamically add javascript confirmation message on a dynamically generated ASP.Net LinkButton or Button control:

If you are looking for GridView Rows Deletion alert message then CLICK HERE.

To do the example add a PlaceHolder Like Below:

<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>

Note: Use placeholder to add dynamic controls in your preferred place. You can use Literal also.

Write the below Sample Server Side Code under Page_Load Event:
C# Code:

        if (!IsPostBack)
            LinkButton lnkbtnDelete = new LinkButton();
            lnkbtnDelete.Text = "Dynamic Link Button to Delete";
            lnkbtnDelete.Attributes.Add("onclick", "return confirm('Are you sure to delete?');");

            Button btnDelete = new Button();
            btnDelete.Text = "Dynamic Button Control to Delete";
            btnDelete.Attributes.Add("onclick", "return confirm('Are you sure to delete?');");

VB.Net Code:

        If Not IsPostBack Then
            Dim lnkbtnDelete As LinkButton = New LinkButton()
            lnkbtnDelete.Text = "Dynamic Link Button to Delete"
            lnkbtnDelete.Attributes.Add("onclick", "return confirm('Are you sure to delete?');")

            Dim btnDelete As Button = New Button()
            btnDelete.Text = "Dynamic Button Control to Delete"
            btnDelete.Attributes.Add("onclick", "return confirm('Are you sure to delete?');")
        End If

Now run the project. You will get the below experience:
Javascript_Confirm_Delete_ Message_Runtime_created_LinkButton_Button_Control_Csharp_VB.Net

Hope now one easily add javascript confirmation message for LinkButton, Button & Image Button even though these were within a GridView or DataList or a Repeater control.

Download Code Example C#        Download Code Example VB.Net

Leave a Reply

Your email address will not be published. Required fields are marked *

AlphaOmega Captcha Classica  –  Enter Security Code