GridView的RowUpdating错误 - 索引超出范围 [英] Gridview RowUpdating Error - Index was out of range

查看:300
本文介绍了GridView的RowUpdating错误 - 索引超出范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我点击GridView中的更新按钮,触发RowUpdating事件,但它返回一个错误索引超出范围必须为非负值并小于集合的大小参数名称。:指数

When I click the "Update" button in the gridview, it fire the RowUpdating event, but it returns a error "Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index"

以下是VB code:

The following is the vb code:

Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs)
    If e.RowIndex >= 0 Then

        Dim row As GridViewRow = DirectCast(Gridview1.Rows(e.RowIndex), GridViewRow)
        Dim Col1_SL As CheckBox = DirectCast(row.FindControl("cb1_SL"), CheckBox)

.................       

        Dim cmd As New System.Data.SqlClient.SqlCommand
        Dim sql As String
        Dim reader As System.Data.SqlClient.SqlDataReader

        Using conn As New System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("hris_shiftdutyConnectionString").ConnectionString)
            conn.Open()
            cmd.Connection = conn
            sql = "SET DATEFORMAT dmy;UPDATE troster SET SL='" & Convert.ToInt32(Col1_SL.Checked) & "' where roster_key='" & Col1_RosterKey.Text & "';"
            cmd.CommandText = sql
            reader = cmd.ExecuteReader()
            conn.Close()
            reader.Close()
        End Using

        'Reset the edit index.
        Gridview1.EditIndex = -1

        'Bind data to the GridView control.
        BindData()
    End If

End Sub

请帮忙。谢谢

Please help. Thanks Joe

推荐答案

由于ViewState中被禁用,你是当页面没有被调回唯一绑定,这条线总是要失败的:

Since the ViewState is disabled and you are only databinding when the page is not being posted back, this line is always going to fail:

昏暗行作为GridViewRow = DirectCast(Gridview1.Rows(e.RowIndex),GridViewRow)

作为 Gridview1.Rows 计数将是0。

当页面被回传到服务器,ASP.NET需要为控制启用,因此它可以重新控制和正确确定提高的事件,它控制值ViewState中发生了变化,等等。你应该启用ViewState中,你就必须弄清楚是什么原因造成的控件树错误。有必须是发生在code一些其他修改。

When the page is posted back to the server, ASP.NET needs the ViewState to be enabled for the control so it can recreate the controls and properly determine which events to raise, which control values have changed, etc. You should enable the ViewState, and you'll have to figure out what is causing the control tree error. There must be some other modification that is taking place in the code.

请记住,只有数据绑定在GridView在Page_Load中时,页面不回发是正确的,但你不能,除非你的控制使ViewState中,妥善处理事件。

Keep in mind that only databinding the GridView in Page_Load when the page is not posted back is correct, but you won't be able to properly handle events unless you enable ViewState on the control.

这篇关于GridView的RowUpdating错误 - 索引超出范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆