ASP.Net:Gridview抛出错误'触发事件RowUpdating未处理'

时间:2015-04-28 12:35:16

标签: asp.net .net vb.net gridview

我在绑定到SQL表的ASP页面上有一个Gridview。我已使用此处描述的方法将Grid更改为使用TemplateFields的默认Label控件,从而将Grid配置为允许多个更新: Bulk Updates to Rows Bound to a GridView

一切正常,直到我进行了更改,以编程方式在页面加载时绑定Gridview查询(目的是使Gridview显示不同的数据,具体取决于当前正在查看页面的用户),如下所述: Bind Gridview programmatically。 进行此更改后,当用户进行更改并单击更新按钮时,页面现在会抛出以下错误:

GridView'GridView1'触发了未处理的事件RowUpdating。

此外,当我尝试进行单行更新时,我收到此错误:

异常详细信息:System.Web.HttpException:GridView“GridView1”触发了未处理的事件RowEditing。

我已经读过很多关于similair问题的线索,但我似乎无法找到解决我的错误的方法。我不知道为什么动态绑定Gridview会导致rowupdating错误。感谢任何支持来解决这个问题。感谢。

以下是代码:

Public Class Input
Inherits System.Web.UI.Page

 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    If Not IsPostBack Then

        gvbind()


    End If



End Sub

Public Sub gvbind()

    Dim SqlDataSource1 As New SqlDataSource()
    SqlDataSource1.ID = "SqlDataSource1"
    Me.Page.Controls.Add(SqlDataSource1)
    SqlDataSource1.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString

    SqlDataSource1.SelectCommand = "SELECT [ID], [Project], [Description], [CAPEX], [Team] FROM [CAPEX]"
    'Add Conditional Statements to change view for users/Teams
    'SqlDataSource1.SelectCommand = "SELECT [ID], [Project], [Description], [CAPEX] FROM [CAPEX] where [Team] = 'Team1'"

    GridView1.DataSource = SqlDataSource1
    GridView1.DataBind()

End Sub


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 Up_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Up.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 Integer
    Dim currentProject As String
    Dim currentDescription As String
    Dim currentCAPEX As String

    currentID = Convert.ToInt32(GridView1.DataKeys(r.RowIndex).Value)

    currentProject = CType(r.FindControl("ProjectTextBox"), TextBox).Text
    currentDescription = CType(r.FindControl("DescriptionTextBox"), TextBox).Text
    currentCAPEX = CType(r.FindControl("CAPEXTextBox"), TextBox).Text

    Dim row As System.Data.DataRow = _
        originalDataTable.Select(String.Format("ID = {0}", currentID))(0)

    If Not currentProject.Equals(row("Project").ToString()) Then Return True
    If Not currentDescription.Equals(row("Description").ToString()) Then Return True
    If Not currentCAPEX.Equals(row("CAPEX").ToString()) Then Return True
    Return False
End Function

这是标记:

    <%@ Page Title="Input" Language="vb" MasterPageFile="~/Site.Master" AutoEventWireup="false"
CodeBehind="Input.aspx.vb" Inherits="WebApplication5.Input" %>

<asp:Content ID="HeaderContent" runat="server"  ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server"   ContentPlaceHolderID="MainContent">
<h2>
    Data Entry
</h2>
<p>
    Enter Inputs Here<asp:TextBox runat="server" Text='<%# Bind ("Project") %>' 
        id="TextBox4"></asp:TextBox>
&nbsp;<asp:Table ID="Entry" runat="server">
    </asp:Table>
    <asp:Button ID="TestButton" runat="server" Text="Test" />
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
       AllowSorting="True" AllowPaging="True" datakeynames = "ID" Width="502px">

        <Columns>
            <asp:CommandField ShowEditButton="True" />
            <asp:BoundField DataField="ID" readOnly = "true" HeaderText="ID" SortExpression="ID"/>
            <asp:TemplateField HeaderText="Project" SortExpression="Project">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Project") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:TextBox ID="ProjectTextBox" runat="server" MaxLength="30" 
                        Text='<%# Bind("Project") %>'></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Description" SortExpression="Description">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Description") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:TextBox ID="DescriptionTextBox" runat="server" MaxLength="150" 
                        Text='<%# Bind("Description") %>'></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="CAPEX" SortExpression="CAPEX">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("CAPEX") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:TextBox ID="CAPEXTextBox" runat="server" MaxLength="10" 
                        Text='<%# Bind("CAPEX") %>'></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>


    <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
        ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 

        SelectCommand="SELECT [ID], [Project], [Description], [CAPEX] FROM [CAPEX]" 
        DeleteCommand="DELETE FROM [CAPEX] WHERE [ID] = @ID" 
        InsertCommand="INSERT INTO [CAPEX] ([ID], [Project], [Description], [CAPEX], [Team]) VALUES (@ID, @Project, @Description, @CAPEX, @Team)" 
        UpdateCommand="UPDATE [CAPEX] SET [Project] = @Project, [Description] = @Description, [CAPEX] = @CAPEX,[Team] = @Team WHERE [ID] = @ID">
        <DeleteParameters>
            <asp:Parameter Name="ID" Type="String" />
        </DeleteParameters>
        <InsertParameters>
            <asp:Parameter Name="ID" Type="String" />
            <asp:Parameter Name="Project" Type="String" />
            <asp:Parameter Name="Description" Type="String" />
            <asp:Parameter Name="CAPEX" Type="Decimal" />
        </InsertParameters>
        <UpdateParameters>
            <asp:Parameter Name="Project" Type="String" />
            <asp:Parameter Name="Description" Type="String" />
            <asp:Parameter Name="CAPEX" Type="Decimal" />
            <asp:Parameter Name="ID" Type="String" />
            <asp:Parameter Name="Team" Type="String" />
        </UpdateParameters>
    </asp:SqlDataSource>
    <asp:Button ID="Up" runat="server" Text="Up" />
    <asp:SqlDataSource ID="SqlDataSource1" runat="server"></asp:SqlDataSource>
    <asp:Table ID="Table1" runat="server" BorderWidth="1" BorderStyle="Solid">
    </asp:Table>
</p>

3 个答案:

答案 0 :(得分:1)

您在Up_Click拨打电话'GridView1.UpdateRow()'

来自MSDN文档:

  

调用此方法还会引发RowUpdated和RowUpdating事件。

所以你需要做的就是提供空处理程序

答案 1 :(得分:0)

添加fnostro关于GridView1.UpdateRow()提升RowUpdated和RowUpdating的答案,显示编辑按钮:

<asp:CommandField ShowEditButton="True" />

将生成一个CommandName="Edit"的按钮。单击此按钮将引发RowEditing事件,您还需要在代码中处理该事件。

作为旁注,由于您的“更新”按钮位于GridView 之外,因此CommandName属性不会自动触发GridView事件。因此,将ID从“更新”更改为“向上”不会产生任何影响,但更改CommandName也不会有任何影响。

答案 2 :(得分:0)

我看到代码隐藏中的SqlDataSource1定义缺少UpdateCommand定义。 说实话,我总是使用SqlDataSource2方法,它就像添加MyDelegate myDeleg = x => myFunc(x); 一样简单,标记就是:

DataSourceID="SqlDataSource2"

这样你就可以忘记gridview是否必须在回发中绑定。除非您将gridview放在UpdatePanel中,否则我必须将其绑定,然后 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" AllowSorting="True" AllowPaging="True" DataKeyNames = "ID" DataSourceID="SqlDataSource2" Width="502px"> 放置不正确。

代码If Not IsPostBack Then看起来位于数据绑定控件之外。