我将数据库中的数据绑定到gridview组合框值。以下是gridview和rowdatabound事件的编码部分。
<asp:GridView ID="workingdaygrid" runat="server"
onrowdeleting="branchgrid_RowDeleting"
onrowediting="branchgrid_RowEditing"
onrowcancelingedit="branchgrid_RowCancelingEdit"
onrowupdating="branchgrid_RowUpdating" DataKeyNames="Workingday_id"
onpageindexchanged="workingdaygrid_PageIndexChanged" AllowPaging="True"
CellPadding="4" ForeColor="#333333" GridLines="None"
AutoGenerateColumns="False" onrowdatabound="workingdaygrid_RowDataBound">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" CancelImageUrl="~/images/Cancel.jpg"
DeleteImageUrl="~/images/delete.jpg" EditImageUrl="~/images/Edit.jpg"
UpdateImageUrl="~/images/update.jpg" ButtonType="Image"/>
<asp:BoundField DataField="Workingday_id" HeaderText="WorkingDayID" />
<asp:BoundField DataField="Working_date" HeaderText="WorkingDayID" />
<asp:BoundField DataField="Working_day" HeaderText="WorkingDayID" />
<asp:TemplateField HeaderText="WorkingdayType">
<ItemTemplate>
<asp:Label runat="server" Text='<%# Eval("Workingday_type") %>'></asp:Label></ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="Workingdaytype" runat="server" Width="100px">
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
并且行数据绑定事件代码为
protected void workingdaygrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList dl = (DropDownList)e.Row.FindControl("Workingdaytype");
DataTable worktype = inter.bindworkdaytype();
dl.DataSource = worktype;
dl.DataTextField = "Workingday_type";
dl.DataValueField = "Time_id";
dl.DataBind();
}
}
在下面的行中,当使用defugger(f11)运行以下行时,它返回null值 DropDownList dl =(DropDownList)e.Row.FindControl(“Workingdaytype”);
答案 0 :(得分:0)
DropDownList位于EditItemTemplate内部,因此仅在网格处于编辑模式时可用。更改您的代码:
if (e.Row.RowType == DataControlRowType.DataRow)
到此:
if (e.Row.RowType == DataControlRowType.DataRow && workingdaygrid.EditIndex > -1)
它应该有用。