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

发布于 2024-07-17 06:56:07 字数 1025 浏览 11 评论 0原文

以下代码在 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”按钮,则会引发以下错误:

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

这不像索引为空之类的。 它有一个价值。 想法?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

桜花祭 2024-07-24 06:56:07

尝试将行替换

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)

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)
铁轨上的流浪者 2024-07-24 06:56:07

我面临着同样的问题。 后来发现是SubString()方法的问题。
我正在做的事情是使用索引从字符串中获取子字符串。 像

myString.SubString(3, 6);

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

I was facing the same problem. Then it turned out to be the problem with SubString() Method.
What I was doing I was getting the substring from a string using index. Like

myString.SubString(3, 6);

and in myString, I was passing "abc" means the string of length was 3. Look for some code which is using substring or collection and try to debug it. Cheers :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文