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

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

  1. Nice post! thank you for sharing with us.

    Dissertation Proposal

  2. The mentioned website also offer free trails to its users regarding xbox. Great website with the information of xbox. Nice offers are available online as free xbox live and free gold xbox live codes 2016
    membership for xbox. These are really good and real
    websites for xbox. Keep sharing more information regarding xbox.

  3. available online as free xbox live and free gold
    membership for xbox. The mentioned website also offer free trails to its users. These are really good and real
    websites for xbox

  4. Wonderful website with the information of xbox. Nice offers are available online as free xbox live and free gold
    membership for xbox and many more. The mentioned website also offer free trails to its users. These are really good and real
    websites for xbox

  5. Y8Y8Y8 says:

    I’m glad you enjoyed it. Those are great habits! Thank you for sharing.

  6. truck games says:

    This article is very good. I really enjoyed reading this. The information here was fascinating to read. I just wish that you will give us more articles like this one in the future. Thanks!

  7. anang says:

    You should take part in a contest for one of the best blogs on the web. I will recommend this site!
    cara mengobati infeksi saluran kencing wanitamanfaat vitamin b12 untuk kesehatan

  8. It was very important for us to know keep posting more information like this thanks and bye

  9. happy wheels says:

    This is an informative post review. I am so pleased to get this post article and nice information. I was looking forward to get such a post which is very helpful to us. A big thank for posting this article in this website. Keep it up.

  10. Such a nice post, keep providing good resources.

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>