gpt4 book ai didi

c# - 以编程方式绑定(bind) gridview 中的 edititemtemplate 中的下拉列表

转载 作者:行者123 更新时间:2023-11-30 23:30:11 24 4
gpt4 key购买 nike

我正在尝试在 RowBound 事件下的 EditItemTemplate 下绑定(bind) 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>

现在这是我的 Row DataBound 事件:

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 而不是状态。您将必须检查被绑定(bind)的行是否处于编辑模式。这应该适合你:-

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
}

关于c# - 以编程方式绑定(bind) gridview 中的 edititemtemplate 中的下拉列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35032910/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com