在GridView的rowEditing事件中填充下拉列表

时间:2013-12-10 13:55:26

标签: 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:BoundField DataField="Department" HeaderText="Department" />
                    <asp:TemplateField HeaderText="Manager Level 1">
                        <ItemTemplate>
                            <asp:Label ID="lblManager1" runat="server" Text=""></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:DropDownList ID="cbManager1" runat="server"></asp:DropDownList>
                        </EditItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Manager Level 2">
                        <ItemTemplate>
                            <asp:Label ID="lblManager2" runat="server" Text=""></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:DropDownList ID="cbManager2" runat="server"></asp:DropDownList>
                        </EditItemTemplate>
                    </asp:TemplateField>
                    <asp:CommandField ShowEditButton="True">
                    <ControlStyle ForeColor="#009EDD" />
                    </asp:CommandField>
                </Columns>
            </asp:GridView>

BoundField'Department'正在如下的Page_Load中填充,工作正常,并使用部门列表填充GridView。

Dim dhandler As DepartmentHandler = New DepartmentHandler
    Dim depts As New List(Of Department)
    depts = dhandler.GetDepartmentList

    gvChain.DataSource = depts
    gvChain.DataBind()

然后我在RowDAtaBound事件中填充TemplateFields的ItemTemplates,如下所示。这也很好。

If e.Row.RowType = DataControlRowType.DataRow Then

        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 deptCell As TableCell = e.Row.Cells(0)
        Dim dept As Department = New Department
        dept.Department = deptCell.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

我现在想要实现的是在GridView上单击行的“编辑”字段时,使用管理器列表填充cbManager1和cbManager2,并将每个DDL的SelectedValue设置为与我检索的值相同的值。项目模板。我可以在GridView的RowEditing事件中使用以下代码获取数据:

' Get the list of managers
    Dim mgrs As New List(Of Manager)
    mgrs = eHandler.GetManagerList

    ' Get the department name from the BoundField
    Dim deptCell As TableCell = gvChain.Rows(e.NewEditIndex).Cells(0)
    Dim dept As Department = New Department
    dept.Department = deptCell.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)

我设置断点来检查数据是从函数返回的,数据是否符合预期,但我遇到的问题是在EditItemTemplate中获取DDL的引用,这样我就可以对它们进行数据绑定并设置SelectedValue。我已经尝试过以下但是这给出了一个NullReference异常:

Dim cbManager1 As DropDownList = TryCast(gvChain.Rows(e.NewEditIndex).FindControl("cbManager1"), DropDownList)
    Dim cbManager2 As DropDownList = TryCast(gvChain.Rows(e.NewEditIndex).FindControl("cbManager2"), DropDownList)

有什么想法吗?

2 个答案:

答案 0 :(得分:2)

最后我绕过了这个。我将以下代码添加到RowEditing事件

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

然后我在RowDataBound事件中执行了以下操作

 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


            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()

            ' Got rid of the bound field in the end and did it with a templatefield so needed to get the department from the label
            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)


            Dim mgr1Name As String = mgr1.Name.ToUpper()
            Dim mgr2Name As String = mgr2.Name.ToUpper()


            cbManager1.SelectedValue = mgr1Name
            cbManager2.SelectedValue = mgr2Name

答案 1 :(得分:0)

你可以试试这样的事情

If e.Row.RowState = DataControlRowState.Edit Then

   Dim cbmanager As DropDownList = e.Row.FindControl("cbmanager1")

   If Not cbmanager Is Nothing Then cbmanager.Items.Add(New ListItem("Test1", "Test1"))


End If