在启用了分页的Asp.net Gridview rowcommand事件中使用row.FindControl时,索引超出范围错误

时间:2009-04-15 13:09:28

标签: asp.net gridview findcontrol rowcommand

以下代码适用于Asp.net Gridview控件中的数据第1页:

     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

但是,如果用户点击任何后续页面上的“Void”按钮,则会引发以下错误:

“索引超出范围。必须是非负数且小于集合的大小。参数名称:索引”

它不像索引是null或其他东西。它有价值。想法?

2 个答案:

答案 0 :(得分:0)

尝试更换行:

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

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

答案 1 :(得分:0)

我遇到了同样的问题。然后事实证明是SubString()方法的问题。 我在做什么我是使用索引从字符串中获取子字符串。像

myString.SubString(3,6);

在myString中,我传递“abc”表示长度为3的字符串。查找一些使用子字符串或集合的代码并尝试调试它。干杯:)