【问题标题】:ASP.Net: Gridview throws error 'fired event RowUpdating which wasn't handled'ASP.Net:Gridview 抛出错误“触发事件 RowUpdating 未处理”
【发布时间】:2015-07-07 07:41:13
【问题描述】:

我在绑定到 SQL 表的 ASP 页面上有一个 Gridview。我已经将网格配置为允许多次更新,方法是使用此处描述的方法将默认标签控件替换为 TemplateFields: Bulk Updates to Rows Bound to a GridView

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

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

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

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

我已经阅读了许多关于类似问题的主题,但我似乎无法找到解决我的错误的方法。我不知道为什么动态绑定 Gridview 会导致行更新错误。感谢任何支持以解决此问题。谢谢。

这是代码:

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>

【问题讨论】:

  • 某事导致RowUpdatingRowEditing 被解雇,因此您需要在代码中处理它们。带有CommandName="Update"CommandName="Edit" 的按钮会做类似的事情。 the-gridview-fired-event-rowupdating-which-wasnt-handled-c-sharp-code-behi
  • 您不仅可以发布代码隐藏,还可以发布 asp 代码。
  • 为了避免触发任何内置函数,我将按钮名称从“更新”更改为“向上”,但我仍然遇到问题。
  • 我编辑帖子添加了asp代码,显然它还没有显示。有人告诉我,任何编辑都排在队列中,需要先获得批准才能显示....与此同时,这是我的 Gridview 部分代码
  • 前面和后面的代码你还有SQLDatasource吗?如果您要转移到后面代码中的绑定,您肯定只需要一个吗?

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


【解决方案1】:

您在Up_Click 中调用“GridView1.UpdateRow()”

来自 MSDN 文档:

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

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

【讨论】:

  • Fnostro,你说得对。似乎 Gridview1.rowupdate 也在触发事件 RowUpdated 和 Rowupdating。尽管我必须承认,如果这些事件是 Gridview 类的内置函数,为什么需要显式处理这些事件的逻辑?在任何情况下,我都添加了如下的空处理程序: Protected Sub Gridview1_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView1.RowUpdating End Sub
  • Protected Sub Gridview1_RowUpdated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdatedEventArgs) Handles GridView1.RowUpdated End SubNow 当我单击更新按钮时,我不再收到错误消息“触发了未处理的事件 RowUpdating。”万岁!但是,现在 Gridview 不会使用输入的新值进行更新。如果这是有道理的,我怀疑我添加的处理程序可能会覆盖内置的更新过程?任何其他想法为什么它现在不更新?
  • 顺便说一句,我试图对您的回答以及从下面的先生们那里收到的非常有帮助的回答给予信任/投票,但显然作为一个新成员,我还没有这个特权:(
  • 不用担心功劳,但要回答您关于事件为何触发的问题,这是因为您使用的是 gridview 的 UpdateRow 函数,这需要在数据源中有一个 Update 命令。您可以绕过它并“自己动手”,或者如果您想花时间学习如何,您可以通过填充参数并调用 it's 更新函数来直接使用 SqlDataSource 控件。
【解决方案2】:

添加到 fnostro 关于GridView1.UpdateRow() 提高 RowUpdated 和 RowUpdating 的答案,像你一样显示编辑按钮:

<asp:CommandField ShowEditButton="True" />

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

附带说明,由于您的“更新”按钮在您的 GridView 之外CommandName 属性将不负责自动触发 GridView 事件。因此,不仅将ID 从“更新”更改为“向上”没有任何影响,而且更改CommandName 也不会有任何影响。

【讨论】:

  • @jf 澄清一下,单击Edit 将引发RowEditing 事件,将GridView 置于编辑模式并更改命令字段以显示Update/Cancel。单击 Update 将引发 RowUpdatingRowUpdated 事件,仅供参考 - 所有 CommandField 点击都会引发 RowCommand
  • @fnostro - 正确,谢谢。因此,一旦 OP 处理了RowEditing,如果单击新的“更新”命令字段按钮并且这些事件均未处理,他将遇到与RowUpdatingRowUpdated 相同的错误。
  • 新手的代码似乎从昨天开始发生了变化,但如果他现在正在实现 SqlDataSource2 和编辑命令字段,他不再需要“向上”
【解决方案3】:

我看到代码隐藏中的 SqlDataSource1 定义缺少 UpdateCommand 定义。 老实说,我一直使用 SqlDataSource2 方法,就像添加 DataSourceID="SqlDataSource2" 一样简单,标记为:

   <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
   AllowSorting="True" AllowPaging="True" DataKeyNames = "ID"
   DataSourceID="SqlDataSource2" Width="502px">

这样您就可以忘记是否必须在回发中绑定 gridview。除非您将 gridview 放在 UpdatePanel 中,否则我会说它必须绑定,然后 If Not IsPostBack Then 放置不正确。

代码&lt;asp:TextBox runat="server" Text='&lt;%# Bind ("Project") %&gt;' id="TextBox4"&gt;&lt;/asp:TextBox&gt; 看起来位于数据绑定控件之外。

【讨论】:

  • 关于更新面板的一句话。无论您如何初始化 UpdatePanel,任何触发回发、内部或外部、全部或部分的控件都会导致 完整页面生命周期。这包括由于填充了 DataSourceID 属性的数据绑定控件而自动发生的所有绑定。更新面板在配置部分更新时只会更新,这意味着将其自己的内容传输回浏览器。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-25
  • 2019-10-20
  • 2019-01-08
  • 2019-05-04
  • 2018-10-03
  • 1970-01-01
相关资源
最近更新 更多