在网格视图中以编程方式绑定编辑项模板中的下拉列表
本文关键字:编辑 下拉列表 方式 视图 网格 编程 绑定 | 更新日期: 2023-09-27 18:32:14
我正在尝试绑定 RowBound 事件下 EditItemTemplate 下的 Gridview 中的下拉列表。但它在下拉列表中给了我空白行:这是我的设计
<asp:TemplateField HeaderText="Description">
<ItemTemplate>
<asp:Label ID="lblDescription" runat="server" Text='<%# Bind("Description") %>' Width="400px"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="lblCity" runat="server" Text='<%# Bind("Description") %>'></asp:Label>
<asp:DropDownList ID="ddlCities" runat="server" Width="400px"></asp:DropDownList>
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddlNewDescFooter" runat="server" Width="400px" OnSelectedIndexChanged="ddlCitiesFooter_SelectedIndexChanged">
</asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
现在这是我的行数据绑定事件:
protected void grdFerries_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowState == DataControlRowState.Edit)
{
DropDownList ddlCities = (DropDownList)e.Row.FindControl("ddlCities");
DataAccessClass DAC = new DataAccessClass();
string Query = "select description from JoursFeries where Year(JoursFeries.date) >= Year(GETDATE()) order by date asc";
DataTable dtddl = DAC.ReturnDatatablefromQuery(Query, DBConnectionString);
ddlCities.DataSource = dtddl;
ddlCities.DataTextField = "description";
ddlCities.DataValueField = "description";
ddlCities.DataBind();
ddlCities.Items.Insert(0, new ListItem("--Select--", "0"));
}
}
我得到空白下拉列表。 请帮忙。
这是因为你编写的代码块永远不会被执行。您需要检查该行是否为DataRow
而不是状态。您必须检查被限定的行是否处于编辑模式。这应该适合您:-
if (e.Row.RowType == DataControlRowType.DataRow && grdFerries.EditIndex == e.Row.RowIndex)
{
DropDownList ddlCities = (DropDownList)e.Row.FindControl("ddlCities");
DataAccessClass DAC = new DataAccessClass();
..and so on
}