ASP.NET GridView同时编辑多个行



. 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

但是它不工作,行正在正常状态下呈现…的念头吗?


EDIT 2: PROPERTY

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'未声明。由于保护级别

,它可能无法访问

我很确定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

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

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

你可以用这个更好…

   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

相关内容

  • 没有找到相关文章

最新更新