Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old July 12th, 2006, 10:23 PM
Friend of Wrox
Join Date: Apr 2004
Posts: 204
Thanks: 0
Thanked 0 Times in 0 Posts
Default Adding Properties to Buttons in a GridView

Can you add an onClientClick property to a command button in a gridview?

For example, I'd like to display a "confirm delete" dialog when someone goes to delete a row in the gridview. Now if it were a normal button, I'd add this property to it in the declaraton.

OnClientClick="return ConfirmDelete()"

which would call this JavaScript function...

function ConfirmDelete() {
    if (confirm ("Are you sure you want to delete this record?")) {
        return true;}
    else {
        return false;

So if you can add that to a gridview command button, where would you do it? Thanks.


Old July 23rd, 2006, 02:11 AM
Friend of Wrox
Join Date: Apr 2004
Posts: 204
Thanks: 0
Thanked 0 Times in 0 Posts


Old July 23rd, 2006, 04:40 AM
Imar's Avatar
Wrox Author
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts

Hi Aaron,

You can change the column into a TemplateField and then set the OnClientClick in the markup.

Alternatively, you can add the onclick attribute in the code behind for the page on the RowDataBound event.

These two posts should give you some idea how to do this:



Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Old July 23rd, 2006, 01:48 PM
Friend of Wrox
Join Date: Apr 2004
Posts: 204
Thanks: 0
Thanked 0 Times in 0 Posts

Ah, Brilliant! Thank you, Imar!

I changed it just a bit, to account for the fact that the control changes from a "Delete" button to a "Cancel" button depending on what mode the row is in. Here's the full code:

    Protected Sub AddDeleteConfirmation(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles gvSearchResults.RowDataBound

        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim lb As LinkButton
            lb = e.Row.Cells(0).Controls(2)
            If lb.Text = "Delete" Then
                lb.Attributes.Add("onclick", "javascript:return confirm('Are you sure you want to delete this row?');")
            End If

        End If

    End Sub

Similar Threads
Thread Thread Starter Forum Replies Last Post
Over-using Lazy properties in GridView? kalel_4444 BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 2 November 5th, 2008 01:43 AM
Dynamic Buttons,SaveToolStrip, adding records KeviJay VB Databases Basics 2 May 28th, 2008 12:46 PM
Adding columns to Gridview gagansharma7 ADO.NET 1 February 15th, 2008 10:54 AM
Adding to Export Of GridView to Excel rsearing ASP.NET 2.0 Professional 17 January 25th, 2007 02:41 AM
Gridview custom buttons wirerider ASP.NET 2.0 Basics 2 February 5th, 2006 02:49 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.