我试图通过ObjectDataSource更新带有大量记录的GridView。我只更新已更改的记录,因此我使用ViewState检查原始数据表是否已更改。它在我的localhost / IIS上工作正常,但是当我在部署服务器上尝试相同的解决方案时,它给了我一个错误:
索引超出了数组的范围。
在这行代码中:
Dim row As System.Data.DataRow = originalDataTable.Select(String.Format("ID = '{0}'", currentID))(0)
以下是我的代码示例。服务器上的ViewState有问题吗?
Private ProdNum As String
Private tableCopied As Boolean = False
Private originalDataTable As System.Data.DataTable
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
If Not tableCopied Then
originalDataTable = CType(e.Row.DataItem, System.Data.DataRowView).Row.Table.Copy()
ViewState("originalValuesDataTable") = originalDataTable
tableCopied = True
End If
End If
End Sub
Protected Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click, btnUpdate1.Click
originalDataTable = CType(ViewState("originalValuesDataTable"), System.Data.DataTable)
For Each r As GridViewRow In GridView1.Rows
If IsRowModified(r) Then GridView1.UpdateRow(r.RowIndex, False)
Next
' Rebind the Grid to repopulate the original values table.
tableCopied = False
GridView1.DataBind()
End Sub
Protected Function IsRowModified(ByVal r As GridViewRow) As Boolean
Dim currentID As Guid
Dim currentNyhed as Boolean
currentID = Guid.Parse(GridView1.DataKeys(r.RowIndex).Value.ToString())
currentNyhed = CType(r.FindControl("CheckBoxNews"), CheckBox).Checked
Dim row As System.Data.DataRow = originalDataTable.Select(String.Format("ID = '{0}'", currentID))(0)
If Not currentNyhed.Equals(row("News").ToString()) Then Return True
Return False
End Function
GridView绑定到具有以下基本设置的ObjectDataSource:
<asp:ObjectDataSource ID="ObjectDataSource1" ViewStateMode="Enabled" EnableViewState="true" runat="server" InsertMethod="Insert" OldValuesParameterFormatString="original_{0}" SelectMethod="GetDataByOverruled" TypeName="DSoverskrivslisteTableAdapters.Shop_Products_Egenskaber_OverrulingTableAdapter" UpdateMethod="Update">
整个过程基于以下教程。而不是SqlDataSource我使用的是ObjectDataSource - 如上所述 - 在我的localhost上运行时工作正常,但在上传到部署服务器时给我错误。
http://msdn.microsoft.com/en-us/library/aa992036(v=vs.90).aspx
请帮忙。