views:

620

answers:

1

The following code works great on Page 1 of data inside an Asp.net Gridview control:

     If e.CommandName = "Void" Then

        'Read the status of the ticket currently
        Dim RowIndex As Integer = CInt(e.CommandArgument)
        Dim row As GridViewRow = grdTradeTickets.Rows(RowIndex)

        Dim lblTransactionID As Label = DirectCast(row.FindControl("lblTransactionID"), Label)
        Dim lblTtStatus As Label = DirectCast(row.FindControl("lblTtStatus"), Label)
        Dim lblTradeTicketID As Label = DirectCast(row.FindControl("lblTradeTicketID"), Label)

        'If already void, show "Already Void" message to user. Else continue "Are you sure you want to void this Trade Ticket?"
        If lblTtStatus.Text = "Void" Then

            mdlPopupAlready.show()

        Else

            mdlPopup.Show()
            lblTradeTicketIdToVoid.Text = lblTradeTicketID.Text

        End If

    End If

However if the user clicks the "Void" button on any later page, the following error is thrown:

"Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index"

It's not like the Index is null or something. It has a value. Thoughts?

A: 

Try replacing the lines:

Dim RowIndex As Integer = CInt(e.CommandArgument)
Dim row As GridViewRow = grdTradeTickets.Rows(RowIndex)

with

Dim row As GridViewRow = DirectCast(DirectCast(e.CommandSource, Control).Parent.Parent, GridViewRow)
Macros