的rowEditing事件下拉列表我有一个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>
的绑定列“系”被填充在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)
任何想法?
如果你想在最后两个声明我的意思是在这种情况下? –
对不起,他们在RowEditing事件 – Jimsan
@Jimsan - 你应该检查:[DataBinding DropDownList里面的GridView EditItemTemplate](http://stackoverflow.com/a/6922403/690329)。 – afzalulh