• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1036
  • Last Modified:

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

0
GADOI
Asked:
GADOI
2 Solutions
 
informaniacCommented:
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
 
Alan WarrenCommented:
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
GADOIAuthor 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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now