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

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

  1. Jon Limjap says:

    Hi,

    I’m just wondering, how do you go around the EvaluateEvent error? It appears that by default onclick events are evaulated by the .NET Runtime, and this traps my OnClientClick event. If I set it to false, some of the events on my page won’t be processed properly. How do you go about that?

  2. [...] your users &quotAre you sure you want to delete this…because I am not fixing it again!&quothttp://www.techtoolblog.com/archives/adding-a-confirm-javascript-popup-to-net-gridviewGridView Confirm Delete ASP.Net 2.0 : gridview, delete, confirmGridView confirm delete ASP.Net 2.0 I [...]

  3. Damien says:

    function confirmSubmit() {
    return confirm(“Do you really want to mark this prospect as in the database?”);
    }

    Less code lines and same result.

  4. Seth Kaufmann says:

    This is lame!!! How about we attach the confirmation directly to the button and get rid of those few little lines in the script block….

    OnClientClick =”return confirm(‘You are about to DELETE a system user. Are you sure you want to do this?’)”

  5. Tim says:

    #4,
    That would add that line of JS to every single gridview row, not optimal!

  6. In the past I’ve always made a scrollable GridView by wrapping a tag around the control, adding a table with header rows inside the (but above the GridView control code), and setting the CSS overflow property on the wrapper div to “auto”. Scott Mitchell has a nice article on doing this with a DataGrid and you can see a sample of doing something like that at my XML for ASP.NET Developers Website if you’re interested. This technique works well but makes it a bit hard to line up the header and child rows perfectly (although it can be done).

  7. Love Poems says:

    I’m just wondering, how do you go around the EvaluateEvent error? It appears that by default onclick events are evaulated by the .NET Runtime, and this traps my OnClientClick event. If I set it to false, some of the events on my page won’t be processed properly. How do you go about that?

  8. Sex Problems says:

    It appears that by default onclick events are evaulated by the .NET Runtime, and this traps my OnClientClick event. If I set it to false,

    free Dofollow sites list

  9. A big thank you for your post.Really looking forward to read more. Really Great hyper marketing

  10. Your blog article is very inter sting and fantastic,at the same time the blog theme is unique and perfect,great job.To your success.Wholesale Towel Supplier

  11. I accidentally got that Great Stuff foam all over my hands and I have tried everything to remove it, I used degrease, soap, greaser. everything and it wont come off. I scrub and scrub but nothing.luxury gulet cruise

  12. Smithd851 says:

    But a smiling visitor here to share the love , btw outstanding layout. deaeeagddbdckeae

  13. Inn remodeling, not like home renovating, can’t be exercised pertaining to only 1 room or maybe one spot. Hotel renovating involves some sort of renovation of any great component of the inn and from time to time, even the main hotel per Se.catamaran charter yachts

  14. I simply get a crucial information right here and additionally this is often really Maine I trying. because of your current posting.shampoo para queda de cabelo

  15. I have seen your awareness about this theme when you post it and it really gives an informational message to us readers. I am hoping that you will continue writing this kind of blog. Thanks for sharing this information.Brustvergrößerung Kosten

  16. Aanrechtblad says:

    I prefer to study this kind of material. Nicely written information in this post, the quality of content is fine and the conclusion is lovely. Things are very open and intensely clear explanation of issues

  17. Its wonderful blog really very nice site and blog facility.every title is very nice and very fatastic concept. Thanks for sharing the information.

  18. Wir genießen immer wieder gerne eure Köstlichkeiten. Super Service und klasse Website. recuperar dados

  19. 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.

  20. Hi . . . I like your commenting stage really I like your work with deeply, Thanks for providing us these matchless guidelines it’s actually helpful for each person. This website is excellent.
    Online Diploma

  21. I was about to say something on this topic. But now i can see that everything on this topic is very amazing and mind blowing, so i have nothing to say here. I am just going through all the topics and being appreciated. Thanks for sharing.

  22. trap says:

    They are maintaining this nicely. I am so impressed by this.
    Trap

  23. trap says:

    Sometimes when you innovate, you make mistakes. It is best to admit them quickly, and get on with improving your other innovations.

  24. Really i appreciate the effort you made to share the knowledge.The topic here i found was really effective to the topic which i was researching for a long time

  25. So well explained incredible article, any buddy can follow it, thanks and keep up the great work. All the contents you mentioned in post are too good and can be very useful, Thanks

  26. I just need to mention I’m newbie to blogging and really liked you’reI just need to mention I’m newbie to blogging and really liked you’re web site. Really likely I’m arranging to bookmark your blog post . You really have fantastic stories. With thanks for revealing your website page. web site. Really likely I’m arranging to bookmark your blog post . You really have fantastic stories. With thanks for revealing your website page.

  27. I actuallyhunting everywhere for this! Thank goodness I located it on Bing. You’ve got made my day! Thx once more

  28. Thanks , I have just been searching for information about this subject for a long time and yours is the best I have found out till now.

  29. This blog is providing beautiful news about about the Liberian Educational Sector. It is nice to know about it this is useful information. And such blogs makes us familiar with the whole world.

  30. No doubt your website is well published and also well defined also like it very much.

  31. Amazing! I also wish him good luck to defend his gold medal. I like to share it with all my friends and hope they will also encourage him.

  32. Such a very useful article. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article.

  33. The Blog is very fantastic really its imazing.Good concept of traslation for different language.Nice subscriber categories.

  34. Wasbak says:

    Thanks for posting this info. I just want to let you know that I just check out your site and I find it very interesting and informative. I can’t wait to read lots of your posts.

  35. It seems like an educational blog which always some new information to visitors and Enjoying every little bit of it and I have you bookmarked to check out new stuff you blog post.

  36. Good information here. I really enjoy reading them every day. I’ve learned a lot from them.Thanks so much for sharing this information. Greatly help me being a newbie.

  37. Energiefonds says:

    Hi my friend! I wish to say that this post is amazing, nice written and include approximately all important infos. I would like to see more posts like this,

  38. siliconen bh says:

    This continually is amazing to me how bloggers such as yourself can find the time and also the dedication to keep on creating fantastic blog posts. This is wonderful and one of my have to read on the web. I simply want to say thanks

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>