ASP.NET GridView Dual Function Edit/Delete Button

Posted on 2012-08-30
Last Modified: 2012-09-05
I'm trying to create a dual function CommandButton in an ASP.NET GridView.  The reason for this is that the records will come in with an either/or scenario--if they have no dependencies, the record can only be deleted, but if dependencies exist, it can only be updated.

On the GridView I have a CommandButton, and on RowDataBound I set its text to either "Edit" or "Delete".   Then in the RowCommand, I have the following (below) code.  

The delete part works, great; however, the edit portion puts the row in edit mode, but does not show the update/cancel links.  Any idea how to accomplish this programmatically?  Thank you.

Dim Row As GridViewRow = gv.Rows(e.CommandArgument)
Dim btnAction As New LinkButton
btnAction = CType(Row.Cells(2).Controls(0), LinkButton)

Select Case btnAction.Text

    Case "Edit"
        gv.EditIndex = Row.RowIndex

    Case "Delete"
        Dim lblID As Label = CType(Row.FindControl("lblID"), Label)
        Dim iError As Integer = DeleteItem(cint(lblID).text)

End Select

Open in new window

Question by:GADOI
    LVL 20

    Expert Comment

    WHen you call BindGrid()

    it must be goin inside the RowDataBound method and inside that you are having logic to set the text to Edit.

    Can you show us the code?
    LVL 83

    Expert Comment

    LVL 26

    Accepted Solution

    Hi GADOI,

    When you are setting the text property of your commandbutton in RowDatabound, also set the CommandName property to either "Edit" or "Delete", then the RowCommand event will take care of the actions.


    Author Comment

    Still no luck, but appreciate the input, so far.

    Informaniac, please see code, below.

    CodeCruiser, I'm running Framework 3.5, so SetEditRow is not available, but thanks for the suggestion.

    Alan,  I added your line (in the code, below), thinking & hoping it would resolve (and changed my case to "Action", "Edit" in the RowCommand, but nothing changed.
    If e.Row.RowType = DataControlRowType.DataRow Then
        Dim iTotal As Integer = CInt(CType(e.Row.Cells(1).FindControl("lblTotal"), Label).Text)
        Dim btnAction As New LinkButton
        btnAction = CType(e.Row.Cells(2).Controls(0), LinkButton)
            If iTotal > 0 Then
                btnAction.Text = "Edit"
                btnAction.CommandName = "Edit"  'Just added, per Alan's suggestion     
                btnAction.Text = "Delete"
            End If
    End If

    Open in new window


    Assisted Solution

    I resolved this by going a different route, altogether.  I created a template field with Edit & Delete linkbuttons in the ItemTemplate.  Then I put Update & Cancel linkbuttons in the EditItemTemplate.  I'm awarding some of the points to Alan, as I was able to set the CommandName property in the actual linkbuttons to Edit/Delete/Update/Cancel.

    With that, I was able to show or hide the edit or delete linkbutton in RowDataBound, skip using the RowCommand event handling entirely, and rely solely on the built-in "RowEditing", "RowUpdating", "RowDeleting", "RowCancelingEdit" methods.

    Thank you all.

    Author Closing Comment

    Alan caused me to think a little differently about the issue, and while his solution didn't directly work, it gave me the idea for the workaround I used to accomplish what I needed.

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Join & Write a Comment

    Suggested Solutions

    The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (…
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now