GridView RowEditing事件仅在备用行上触发

时间:2013-12-11 11:55:11

标签: asp.net vb.net gridview

我有一个GridView如下:

<asp:GridView ID="gvChain" runat="server" Font-Size="Small" CellPadding="3" CellSpacing="1" GridLines="None"
                AutoGenerateColumns="False">
                <HeaderStyle BackColor="#CCCCCC" />
                <Columns>                       
                    <asp:TemplateField HeaderText="Department" ControlStyle-Width="175px">
                        <EditItemTemplate>
                            <asp:Label ID="lblDepartment" runat="server" Text='<%# Bind("Department") %>'></asp:Label>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="lblDepartment" runat="server" Text='<%# Bind("Department") %>'></asp:Label>
                        </ItemTemplate>
                        <ControlStyle Width="175px" />
                    </asp:TemplateField>

                    <asp:TemplateField HeaderText="Manager Level 1" ControlStyle-Width="175px">
                        <ItemTemplate>
                            <asp:Label ID="lblManager1" runat="server" Text=""></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:DropDownList ID="cbManager1" runat="server"></asp:DropDownList>
                        </EditItemTemplate>
                        <ControlStyle Width="175px" />
                    </asp:TemplateField>

                    <asp:TemplateField HeaderText="Manager Level 2" ControlStyle-Width="175px">
                        <ItemTemplate>
                            <asp:Label ID="lblManager2" runat="server" Text=""></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:DropDownList ID="cbManager2" runat="server"></asp:DropDownList>
                        </EditItemTemplate>
                        <ControlStyle Width="175px" />
                    </asp:TemplateField>

                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:LinkButton ID="btnEdit" CommandName="Edit" runat="server" Text="Edit" CssClass="buttonStyle" CausesValidation="false"/>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:LinkButton ID="btnSave" CommandName="Update" runat="server" Text="Save" CssClass="buttonStyle" CausesValidation="false"/>
                            <asp:LinkButton ID="btnCancel" CommandName="Cancel" runat="server" Text="Cancel" CssClass="buttonStyle" CausesValidation="false" />
                        </EditItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:LinkButton ID="btnDelete" CommandName="Delete" runat="server" Text="Delete" CssClass="buttonStyle" CausesValidation="false" />
                        </ItemTemplate>      
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>

BoundField填充在Page_Load事件中,如下所示:

Dim dHandler As DepartmentHandler = New DepartmentHandler
    Dim depts As New List(Of Department)
    depts = dHandler.GetDepartmentList
    gvChain.DataSource = depts.

If Not Page.IsPostBack Then
       gvChain.DataBind()
End If

然后在RowDataBound事件中填充TemplateFields,如下所示:

If e.Row.RowType = DataControlRowType.DataRow Then

        If e.Row.RowState = DataControlRowState.Edit Then

            Dim cbManager1 As DropDownList = TryCast(e.Row.FindControl("cbManager1"), DropDownList)
            Dim cbManager2 As DropDownList = TryCast(e.Row.FindControl("cbManager2"), DropDownList)

            Dim eHandler As EmployeeHandler = New EmployeeHandler

            ' Get the list of managers
            Dim mgrs As New List(Of Manager)
            mgrs = eHandler.GetManagerList
            cbManager1.DataSource = mgrs
            cbManager2.DataSource = mgrs
            cbManager1.DataValueField = "Name"
            cbManager1.DataTextField = "Name"
            cbManager2.DataValueField = "Name"
            cbManager2.DataValueField = "Name"
            cbManager1.DataBind()
            cbManager2.DataBind()

            ' Get the department name from the label in cell 0
            Dim lbl As Label = e.Row.Cells(0).FindControl("lblDepartment")
            Dim dept As Department = New Department
            dept.Department = lbl.Text

            ' Pass the department name to the getManager1/2 functions to return the correct manager for that department
            Dim mgr1 As Manager = eHandler.getManager1(dept)
            Dim mgr2 As Manager = eHandler.getManager2(dept)

            ' Upper case the manager name to search the DDLs for item
            Dim mgr1Name As String = mgr1.Name.ToUpper()
            Dim mgr2Name As String = mgr2.Name.ToUpper()

            ' Find the manager in the DDL and select
            cbManager1.SelectedValue = mgr1Name
            cbManager2.SelectedValue = mgr2Name
        Else
            ' If GV isnt in edit mode then populate labels in itemtemplate with manager 1 and 2 values
            Dim lblManager1 As Label = DirectCast(e.Row.FindControl("lblManager1"), Label)
            Dim lblManager2 As Label = DirectCast(e.Row.FindControl("lblManager2"), Label)


            Dim eHandler As EmployeeHandler = New EmployeeHandler
            Dim lbl As Label = e.Row.Cells(0).FindControl("lblDepartment")
            Dim dept As Department = New Department
            dept.Department = lbl.Text
            Dim mgr1 As Manager = eHandler.getManager1(dept)
            Dim mgr2 As Manager = eHandler.getManager2(dept)

            lblManager1.Text = mgr1.Name
            lblManager2.Text = mgr2.Name
        End If
    End If

然后我在RowEditing事件中有以下内容

gvChain.EditIndex = e.NewEditIndex
gvChain.DataBind()

当GridView中只显示1条记录时,我单击编辑按钮,GridView进入编辑模式我可以使用RowUpdating事件更新该记录,这样可以正常工作。但是,如果我向GridView添加另一条记录,则单击该记录的编辑按钮会产生NullReference异常。如果我再添加另一条记录,那么第3条记录将进入编辑模式。第4条记录将再次因NullReference异常而失败,依此类推。

调试时,似乎在行索引1,3,5等上,GridView不会进入编辑模式,但在行索引0,2,4等处,GridView将切换到编辑模式。

这似乎很奇怪。可能导致GridView不在这些行上进入编辑模式的任何想法?

2 个答案:

答案 0 :(得分:0)

行的状态可以是DataControlRowState值中的一个或组合,因此使用按位操作来确定行的状态是否包含DataControlRowState值。 here

如果使用位逻辑检查RowState属性,则RowState包含多个值(Alternate | Edit)。

尝试

 If (e.Row.RowState And DataControlRowState.Edit) > 0 Then

答案 1 :(得分:0)

我通过修改getDepartmentList函数来返回Manager1和Manager 2字段,然后将它们数据绑定在我的绑定'Department'中。

然后我将RowDataBound事件修改为以下内容:

If e.Row.RowType = DataControlRowType.DataRow Then

        If e.Row.RowState And DataControlRowState.Edit Then

            Dim cbManager1 As DropDownList = TryCast(e.Row.FindControl("cbManager1"), DropDownList)
            Dim cbManager2 As DropDownList = TryCast(e.Row.FindControl("cbManager2"), DropDownList)

            Dim eHandler As EmployeeHandler = New EmployeeHandler

            ' Get the list of managers
            Dim mgrs As New List(Of Manager)
            mgrs = eHandler.GetManagerList
            cbManager1.DataSource = mgrs
            cbManager2.DataSource = mgrs
            cbManager1.DataValueField = "Name"
            cbManager1.DataTextField = "Name"
            cbManager2.DataValueField = "Name"
            cbManager2.DataValueField = "Name"
            cbManager1.DataBind()
            cbManager2.DataBind()

            ' Get the department name from the label in cell 0
            Dim lbl As Label = e.Row.Cells(0).FindControl("lblDepartment")
            Dim dept As Department = New Department
            dept.Department = lbl.Text

            ' Pass the department name to the getManager1/2 functions to return the correct manager for that department
            Dim mgr1 As Manager = eHandler.getManager1(dept)
            Dim mgr2 As Manager = eHandler.getManager2(dept)

            ' Upper case the manager name to search the DDLs for item
            Dim mgr1Name As String = mgr1.Name.ToUpper()
            Dim mgr2Name As String = mgr2.Name.ToUpper()

            ' Find the manager in the DDL and select
            cbManager1.SelectedValue = mgr1Name
            cbManager2.SelectedValue = mgr2Name
        End If

    End If