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

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

  1. Very helpful advice in this particular post! It’s the little changes that make the largest changes. Thanks for sharing!

  2. happy wheels says:

    Thanks for sharing the information. It is very useful for my future. keep sharing
    A good blog.

  3. Friv Games says:

    Play free Friv games flash online games flash.
    Thanks you for sharing
    Friv Games | Friv Online

  4. Appreciate your work, It’s very helpful and education article. Thank for sharing with us.

  5. I tesed and okie, thank you!

  6. Friv 2 says:

    Wonderful blog! This is very informative site. I am totally pleased by your excellent work. Many thanks for sharing.

  7. There are so many professionals asking others to get the best enjoyable moments of their life. Now if you are a student you need spend your life in such a way it will be memorable for you without worrying about the essay writing because we are here to provide you the best content for your essays.

  8. writing help says:

    Hello! Thanks for this amazing information. I don’t much understand in programming, so your site is very important and helpful for me. I will definitely recommend your site to all my friends. Please keep it good posting. And do not hesitate use particular writing help from http://www.custom-paper-writing.org at any suitable time!

  9. werkbladen says:

    I bookmarked this web. I will come back to continue learning about your web design.I’m glad Yahoo pointed me to it. I was able to get the know-how I was searching so badly for days now.Thank You very much for your really good web page. Have a good day.It’s very useful for everyone for sure.

  10. havaland?rma says:

    Hello! Thanks for this amazing information. I don’t much understand in programming, so your http://www.imesamekanik.com

  11. SKatrine says:

    Hello! Great article! Thanks for sharing! I don’t understand in programming, but I know where can order essay http://www.essay-mania.com/.

  12. Friv says:

    Nice, I really like it. If you love your website when you visit the following websites:

  13. Mg Rabbani says:

    Hello, I enjoy reading through your article.I like to write a little comment to support you.

  14. ????? ???? says:

    thanks for share that…

  15. ??? ???? says:

    thank you very much…

  16. ??? ???? says:

    ?Great post here, thought I could learn more from but we can learn
    more from this post.

  17. graphicup says:

    Sweet blog! I found it while browsing on Yahoo News. Do you have any
    suggestions on how to get listed in Yahoo News? I’ve been trying for a
    while but I never seem to get there! Thanks

  18. Friv 10 says:

    you’d have time to look these kids active. Please visit our website and let us play the game interesting.
    Thanks for sharing !
    Friv 10
    Kizi 10
    Yepi 2

  19. live updates says:

    I read your post and need to thank you for sharing such pleasant lines. Be that as it may, I likewise need to instruct you to share more data as I had needed to peruse all the more yet didn’t get acceptable substance from anyplace.

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>