Adding a Confirm Javascript PopUp to a ASP.NET GridView

The more and more I use the GridView control, the more I end up writing my own rather then using the DataBound columns because of the additional flexiblity you get when writing your own templates.

This time I ended up writing my own TemplateField because I need to add a confirm pop up to basically a “delete” command (it didn’t really delete the database record but in fact flagged it as a different type)… but you get the idea where this might come in handy. After all you don’t want users calling saying “Yeah, I just deleted something I didn’t mean to”. Instead you want to pop up a javascript box with a “Do you really want to do this…because I am not fixing it again!”

Unfortunetly the doesn’t offer you an onClientClick event, so this is my work around.

Here is the basic Javascript code to pop up a confirm button.

<SCRIPT LANGUAGE=”JavaScript”>

      function confirmSubmit() {

      var agree=confirm(“Do you really want to mark this prospect as in the database?”);

      if (agree)

       return true ;

      else

       return false ;

    }

    </SCRIPT>

Now in your GridView->Column Tag add a and your onClientClick attribute. This will call your Javascript function above and display a confirm popup to your users. You also must define a onClick event that points to a function that you will need to create for the server side processing of this request.

<asp:TemplateField>

    <ItemTemplate>

        <asp:Button ID=”btnCustomerExist”

        text=”customer exist”

        Runat=Server

        OnClick=”CustomerAlreadyExist”

        OnClientClick=”return confirmSubmit()”

        Width=”100″ />

   </ItemTemplate>

  </asp:TemplateField>

If the user clicks “OK” on the popup, you will need to handle the server side processing. I tend to write my own Data Access layer to handle Selects and Deletes so for me it is pretty straight forward from here, I call a function and pass in the unique id from the database.

Public Sub CustomerAlreadyExist(ByVal sender As Object, ByVal e As EventArgs)

        Dim ProspectID As Int32

 

        Dim btnRemoveProspect As Button = CType(sender, Button)

        Dim grdRow As GridViewRow = CType(btnRemoveProspect.Parent.Parent, GridViewRow)

        ‘Get ID

        ProspectID = grdRow.Cells(0).Text

        ‘Instant Class

        myAddProspect = New AddProspect()

        ‘Removes from Prospect Table

        myAddProspect.RemoveFromDatabase(ProspectID)

        ‘Refresh Gridview

        Page_Load(sender, e)

    End Sub

The End Result

Of course this won’t stop everyone from accidentally deleting users but it will stop 9/10.

Tim

A .NET, PHP, Marketing Guru authority, at least I hope I am. Reach me at tboland@gmail.com

466 thoughts on “Adding a Confirm Javascript PopUp to a ASP.NET GridView

  1. Contact Write My Custom Essay via the 24-hour live chat. Our friendly customer service representatives will inform you about the various services and policies we offer. Furthermore, if you desire to place an order, all you have to do is say, “ Do my essay for me” and we will instantly initiate the job!

  2. mcafee.com/activate – McAfee is an antivirus which is available worldwide for devices like Computer, Laptop or Smartphones, and McAfee is one of the best 10 Antivirus according to all reviews in last 10 years. McAfee Security is used to secure the devices from Trojans, Attacks, Malware or any other activities which may harm the devices or network.

  3. Office.com/setup – Microsoft Office is world-renowned suite available for both personal and professional use. If you are one of Microsoft Office users then you would have an idea about the great services provided by them. Furthermore, it one of the oldest and still thriving suites in the market.

  4. McAfee.com/activate – Visit the website in order to Get Started with the McAfee Activation or you may also contact us anytime at our Toll-free number to get help for McAfee Activate or Install.

  5. Norton.com/setup – Setup your Norton subscription now with the help of this link. Let’s get started with your Norton Security today so just Activate Norton Subscription to enjoy the services.

  6. McAfee.com/Activate – McAfee Antivirus one of the popular Antivirus and Security System around the Globe. It helps many users to provide protection from the virus, trojan, spyware, and many similar threats. Ig you want to get started with McAfee then you have to go through the steps to McAfee.com/Activate. Secure your PC, Laptop, Tablet, and Smartphones with McAfee Antivirus and follow these to McAfee.com/Activate on your respective device.

  7. norton.com/setup allows you to download and install the Norton Antivirus setup online on your device. You can manage your Norton product subscription and renewal from your Norton account. Norton provides you a Norton Utility tool to analyze and optimize your computer. Norton utility is a free tool which can be downloaded from the manufacturer website. It increased the boot speed and optimize the overall performance of the system.

  8. Webroot.com/safe – It is very easy to install and use www.webroot.com/safe by following a few easy steps.In order to stop virus and worm to infect your device using Webroot, run setup and install it.

  9. norton.com/setup – In today’s world everything is online or in digital form which also has increased the risk of Viruses, Trojans, scams, data and identity theft, and Norton provides protection to your digital life from viruses and malware in a fraction of seconds. The Norton Product is better than any other anti-viruses as it monitors antimalware. 

  10. These are some important things. It has never been honored that it can be changed. To supply such tips, the responsibility of appreciation by a wide edge of centrality is for everyone.

Leave a Reply

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