ASP.NET GridView多个编辑行同时

时间:2012-12-27 22:15:09

标签: asp.net gridview edit rowstate

在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>

ERROR:

错误:BC30451:未声明'IsRowInEditMode'。由于保护水平,它可能无法实现

2 个答案:

答案 0 :(得分:1)

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

作为一种解决方法,您可以将ItemTemplate用于两种状态(例如,LabelTextBox。然后,您可以使用EditMode作为参数的属性RowIndex。您可以在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) %>

答案 1 :(得分:0)

你可以使用这个有效的击球手......

   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