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.

Written by Tim on June 2nd, 2006 with 12 comments.
Read more articles on asp.net.

Related articles

12 comments

Read the comments left by other users below, or:

Get your own gravatar by visiting gravatar.com Jon Limjap
#1. July 11th, 2006, at 7:22 AM.

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?

Trackback Mention from Andre.usednewsdigest.info
#2. June 12th, 2008, at 7:55 PM.

asp net confirm delete gridview: 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 ...

Get your own gravatar by visiting gravatar.com Damien
#3. February 9th, 2009, at 3:32 PM.

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

Less code lines and same result.

Get your own gravatar by visiting gravatar.com Seth Kaufmann
#4. March 23rd, 2009, at 7:15 PM.

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?’)”

Get your own gravatar by visiting gravatar.com Tim
#5. March 23rd, 2009, at 7:22 PM.

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

Get your own gravatar by visiting gravatar.com ZK@Web Marketing Blog
#6. June 29th, 2009, at 7:57 AM.

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

Get your own gravatar by visiting gravatar.com Shared Web Hosting
#7. December 20th, 2009, at 11:22 AM.

Good Post….

Shared Web Hosting

Get your own gravatar by visiting gravatar.com Love Poems
#8. December 29th, 2009, at 3:51 PM.

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?

Get your own gravatar by visiting gravatar.com Sex Problems
#9. February 9th, 2010, at 9:27 AM.

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

Get your own gravatar by visiting gravatar.com hyper marketing
#10. February 21st, 2014, at 12:42 PM.

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

Get your own gravatar by visiting gravatar.com Wholesale Towel Supplier
#11. February 26th, 2014, at 4:07 PM.

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

Get your own gravatar by visiting gravatar.com luxury gulet cruise
#12. April 23rd, 2014, at 10:37 AM.

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

Leave your comment...

If you want to leave your comment on this article, simply fill out the next form:




You can use these XHTML tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> .