Home All Groups Group Topic Archive Search About
Author
15 Feb 2005 3:15 AM
Maziar Aflatoun
Hi everyone,

Does anyone how to add a javascript confirmation popup to a link button?

<asp:ButtonColumn HeaderText="Del" ButtonType="LinkButton" Text="Del"
CommandName="Delete" />

Thank you
Maz.

Author
15 Feb 2005 1:44 PM
Ken Cox [Microsoft MVP]
Hi Maz,

The easiest way is to convert it to a template column, give it an ID and
then add the JavaScript when the item is created. The code below should get
you started. Let us know if it helps?

Ken
Microsoft MVP [ASP.NET]
Toronto

            <asp:datagrid id="dgAppStatus" Runat="server"
AutoGenerateColumns="False" DataKeyField="EmployID">
                <columns>
                    <asp:boundcolumn DataField="EmployID"
SortExpression="EmployID" HeaderText="Employee ID"></asp:boundcolumn>
                    <asp:boundcolumn DataField="LASTNAME"
SortExpression="LASTNAME" HeaderText="Last Name"></asp:boundcolumn>
                    <asp:boundcolumn DataField="FRSTNAME"
SortExpression="FRSTNAME" HeaderText="First Name"></asp:boundcolumn>
                    <asp:templatecolumn>
                        <itemtemplate>
                            <asp:button ID="btnDelete"
CommandName="DeleteIt" Text="Delete" Runat="server"></asp:button>
                        </itemtemplate>
                    </asp:templatecolumn>
                    <asp:templatecolumn HeaderText="Del">
                        <itemtemplate>
                            <asp:linkbutton ID="lnkDelete" runat="server"
Text="Del" CommandName="Delete" CausesValidation="false"></asp:linkbutton>
                        </itemtemplate>
                    </asp:templatecolumn>
                </columns>
            </asp:datagrid>

    Private Sub Page_Load(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles MyBase.Load
        If Not IsPostBack Then
            dgAppStatus.DataSource = CreateDataSource()
            dgAppStatus.DataBind()
        End If
    End Sub

    Function CreateDataSource() As DataTable
        Dim dt As New DataTable
        Dim dr As DataRow
        dt.Columns.Add(New DataColumn _
        ("EmployID", GetType(Int32)))
        dt.Columns.Add(New DataColumn _
        ("LASTNAME", GetType(String)))
        dt.Columns.Add(New DataColumn _
        ("FRSTNAME", GetType(String)))

        Dim i As Integer
        For i = 0 To 1
            dr = dt.NewRow()
            dr(0) = i
            dr(1) = "FirstName " + i.ToString()
            dr(2) = "LastName " + i.ToString()
            dt.Rows.Add(dr)
        Next i
        Return dt
    End Function 'CreateDataSource

    Private Sub dgAppStatus_ItemCommand _
    (ByVal source As Object, ByVal e _
    As System.Web.UI.WebControls.DataGridCommandEventArgs) _
    Handles dgAppStatus.ItemCommand
        If e.CommandName = "Delete" Then
            ' Call delete routine here and rebind to the dataset
            Response.Write("Would have deleted row index: " & _
            e.Item.ItemIndex.ToString)
        End If
    End Sub

    Private Sub dgAppStatus_ItemCreated(ByVal sender As Object, _
    ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) _
    Handles dgAppStatus.ItemCreated
        Dim btnDel As LinkButton
        Dim drow As DataGridItem
        drow = e.Item
        btnDel = drow.FindControl("lnkDelete")
        If Not IsNothing(btnDel) Then
            btnDel.Attributes.Add("onclick", _
            "return confirm ('Are you sure you wish to delete this
record?')")
        End If
    End Sub

Show quoteHide quote
"Maziar Aflatoun" <ma***@rogers.com> wrote in message
news:xu2dnaCCNYLO9IzfRVn-uw@rogers.com...
> Hi everyone,
>
> Does anyone how to add a javascript confirmation popup to a link button?
>
> <asp:ButtonColumn HeaderText="Del" ButtonType="LinkButton" Text="Del"
> CommandName="Delete" />
>
> Thank you
> Maz.
>
>