在启用分页的情况下在 Asp.net Gridview rowcommand 事件中使用 row.FindControl 时出现索引超出范围错误
以下代码在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试将行替换
为:
Try replacing the lines:
with
我面临着同样的问题。 后来发现是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 :)