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

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

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

  2. Wonderful post. I appreciate your attention to this subject and I learned a good deal.

  3. I was looking for something like this…I found it quiet interesting, hopefully you will keep posting such blogs….Keep sharing.

  4. Online reading is not my thing. But after reading your blog I am really pleased. I don’t know about other blogs but this I will definitely keep coming back to.

  5. I really like your site.and really enjoyed this post.Very helpful,, the style of this article I’m like, really pretty good.

  6. nice blog,can you teach me how to creat it!

  7. Welcome our Coach Outlet stores,Coach is known to all that all the Coach handbags were designed for the customers who seek for the fashion.Coach Factory Outlet Store Offer Many beautiful Coach Outlet Online with the classic pattern that warmly welcomed by customers-40% OFF.coachfactory-outlet.in.net are the most professional COACH online shopping mall, COACH products provide the latest offer, Coach purses pictures.

  8. Hi my friend! I wish to say that this post is amazing, nice written and include approximately all important infos.

  9. Very efficiently written post. It will be supportive to everyone who utilizes it, including yours truly :) . Keep up the good work – i will definitely read more posts….

  10. Click here says:

    OMG…This is just what I need. Flabuless where have you been the last ten years of my life? I need you. Thanks!

  11. Click here says:

    The article has really peaks my interest. I am going to bookmark your site and preserve checking for new information.

  12. Revenue nighttime trends other sorts of breakthroughs pick monthly payments mutually together with commonly encounter superior ingredient certification regarding ingredient value related to getting. Your own personal hard attained cash improve recognizing is usually coasted declare for example a wise part round the profits improve. Your individual current wise part coasted commonly relating to 2% mutually together with 4%. Quite a few playing cards organizations value obtaining a unveiled recognizing no matter what her or his training relating to another person’s cash items.

  13. What a excellent work you have post here Thanks.

  14. This conent is simply exciting and creative. I have been deciding on a institutional move and this has helped me with one aspect. surf camp Tenerife

  15. Both the MDI and Blue Hill tours are bi-annual events—in the off years, the Blue Hill Garden Club has a garden tour, and the Bar Harbor Club has a garden tour. So something in each town each year.marketing knowledge

  16. Citing is one in every of the simplest strategies to stop plagiarism. To report methodology for carries with it the list details for any details or ideas you employ from sources, whether or not quoted or paraphrased.

  17. There square measure as several reasons to put in writing as there square measure writers. Every people have our own reasons why we tend to sit ahead of a keyboard or scribble our thoughts on a pad. Even so, there square measure some key reasons why you must frequently write. Whether or not its articles, essays, poems, compositions, papers, books, e-books, ad copy, literature or the other of the myriad of writing forms, it is a vocation that serves virtually everybody in sensible role. Here square measure seven sensible reasons why you must be a part of the good bequest of writers.

  18. This is such a great information for me, because i do an online job thats why i need fast Internet connection, i search how to bost internet speed then ireach at a right place. Boscovs coupons

  19. douchebak says:

    Great explanation about this topic and i am new guy to this job thanks to sharing the wonderful articles

  20. douchebak says:

    It is nice to read the information provided in your blog and i like this information because it is based on reality and i like this information. And it provides knowledge and useful information to the visitors of this site and i would like to visit this site again.

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>