ASP.NET GridView Dual Function Edit/Delete Button

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
BindGrid()

Open in new window

GADOIAsked:
Who is Participating?
 
Alan WarrenConnect With a Mentor Applications DeveloperCommented:
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.

Alan
0
 
Obadiah ChristopherCommented:
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?
0
 
CodeCruiserCommented:
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
GADOIAuthor Commented:
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     
        Else
            btnAction.Text = "Delete"
        End If

End If

Open in new window

0
 
GADOIConnect With a Mentor Author Commented:
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.
0
 
GADOIAuthor Commented:
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.