【问题标题】:ASP.NET GridView Multiple Edit Rows At Same TimeASP.NET GridView 同时编辑多个行
【发布时间】:2026-02-03 09:00:01
【问题描述】:

在 ASP.NET 4.0 GridView 中,是否可以同时在编辑模式下拥有多行?

我在属性中使用编辑模式控制行:

Private Property Editing As List(Of Integer)
   Get
       If ViewState("Editing") Is Nothing Then ViewState("Editing") = New List(Of Integer)
       Return CType(ViewState("Editing"), List(Of Integer))
   End Get
   Set(value As List(Of Integer))
       ViewState("Editing") = value
   End Set
End Property

当用户点击编辑按钮时填充它:

Protected Sub GridView1_RowCommand(sender As Object, e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
    If e.CommandName = "Edit" Then
        Dim row = CType(CType(e.CommandSource, LinkButton).NamingContainer, GridViewRow)
        Editing.Add(row.RowIndex)
    End If
End Sub

并在 RowDataBound 事件中手动更改 RowState 属性:

Protected Sub GridView1_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
    If e.Row.RowType = DataControlRowType.DataRow Then
        If Editing.Contains(e.Row.RowIndex) Then
            Then e.Row.RowState = DataControlRowState.Edit
        End If
    End If
End Sub

但它不起作用,行正在以正常状态呈现......有什么想法吗?


编辑 2:属性

MultipleEditGridView.vb:

Namespace ClubeCheckIn.UI

        Public Class MultipleEditGridView
            Inherits GridView

            Protected Property IsRowInEditMode(rowIndex As Int32) As Boolean
                Get
                    If ViewState("GridRowEditIndices") Is Nothing Then
                        Return False
                    Else
                        Dim indices = DirectCast(ViewState("GridRowEditIndices"), List(Of Int32))
                        Return indices.Contains(rowIndex)
                    End If
                End Get
                Set(value As Boolean)
                    If ViewState("GridRowEditIndices") Is Nothing Then
                        ViewState("GridRowEditIndices") = New List(Of Int32)
                    End If
                    Dim indices = DirectCast(ViewState("GridRowEditIndices"), List(Of Int32))
                    indices.Remove(rowIndex)
                    indices.Add(rowIndex)
                End Set
            End Property

        End Class

    End Namespace

web.config:

<controls>
    <add tagPrefix="clube" namespace="ClubeCheckIn.UI" />
</controls>

ASPX:

<clube:MultipleEditGridView ID="GridView1" runat="server">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:TextBox ID="txtEdit" runat="server" Visible="<%# IsRowInEditMode(Container.DataItemIndex) %>" />
            </ItemTemplate>
    </Columns>
</clube:MultipleEditGridView>

错误:

错误:BC30451:未声明“IsRowInEditMode”。由于保护级别,它可能无法访问

【问题讨论】:

    标签: asp.net gridview edit rowstate


    【解决方案1】:

    我很确定GridView 在编辑模式下不支持多行。

    作为一种变通方法,您可以将ItemTemplate 用于两种状态(例如LabelTextBox)。然后你可以使用属性EditModeRowIndex 作为参数。您可以在ViewState 中以编辑模式存储行。

    (未测试)

    Protected Property IsRowInEditMode(rowIndex As Int32) As Boolean
        Get
            If ViewState("GridRowEditIndices") Is Nothing Then
                Return False
            Else
                Dim indices = DirectCast(ViewState("GridRowEditIndices"), List(Of Int32))
                Return indices.Contains(rowIndex)
            End If
        End Get
        Set(value As Boolean)
            If ViewState("GridRowEditIndices") Is Nothing Then
                ViewState("GridRowEditIndices") = New List(Of Int32)
            End If
            Dim indices = DirectCast(ViewState("GridRowEditIndices"), List(Of Int32))
            indices.Remove(rowIndex)
            indices.Add(rowIndex)
        End Set
    End Property
    

    您可以直接从标记中调用它,例如 f.e.对于编辑控件:

    Visible='<%# IsRowInEditMode(Container.DataItemIndex) %>
    

    【讨论】:

    • 您的意思是创建两个 asp:panel,一个包含标签,另一个包含文本框?
    • @Fernando:不。您只需要为每种模式设置两个控件并根据模式切换可见性,您甚至可以从 aspx 中调用该属性,例如:Visible='&lt;%# IsRowInEditMode(Container.DataItemIndex) %&gt;'(未测试)。每列的 ItemTemplate 都包含两个控件,但只有一个可见。
    • 我在自定义服务器控件中插入了此属性,但页面无法识别它...有办法吗?
    • @Fernando:“它无法识别”是什么意思?您可以从代码隐藏中设置它并从 aspx 中读取它,不是吗?
    • 它不在后面的代码中,它在自定义服务器控件中: Public Class MultipleEditGridView Inherits GridView Protected Property IsRowInEditMode(rowIndex As Int32) As Boolean...
    【解决方案2】:

    你可以用这个对击球手有用...

       Get
            If ViewState("GridRowEditIndices") Is Nothing Then
                Return False
            Else
                Dim indices = DirectCast(ViewState("GridRowEditIndices"), List(Of Int32))
                Return indices.Contains(rowIndex)
            End If
        End Get
        Set(value As Boolean)
            If ViewState("GridRowEditIndices") Is Nothing Then
                ViewState("GridRowEditIndices") = New List(Of Int32)
            End If
            Dim indices = DirectCast(ViewState("GridRowEditIndices"), List(Of Int32))
            indices.Remove(rowIndex)
            indices.Add(rowIndex)
        End Set
    End Property
    

    【讨论】: