【问题标题】:Call TemplateField DropDownList in asp.net - code behind with FindControl()在 asp.net 中调用 TemplateField DropDownList - FindControl() 后面的代码
【发布时间】:2017-01-12 15:13:10
【问题描述】:

这就是我在 GridView 中创建 DropDownListLehrbeginn 的方式。 sss

<asp:TemplateField HeaderText="Lehrbeginn" SortExpression="lehrbeginn" HeaderStyle-Width="40px"> 
       <EditItemTemplate>
            <asp:DropDownList ID="DropDownListLehrbeginn" runat="server"></asp:DropDownList>
       </EditItemTemplate>
       <ItemTemplate>
            <asp:Label ID="LabelLehrbeginn" runat="server" Text='<%# Bind("lehrbeginn") %>'></asp:Label>
       </ItemTemplate>
</asp:TemplateField>

我想像这样用 c# 添加 ListItems:

DropDownListLehrbeginn.Items.Add(new ListItem(DateTime.Now.Year.ToString()));
DropDownListLehrbeginn.Items.Add(new ListItem(DateTime.Now.AddYears(1).Year.ToString()));
DropDownListLehrbeginn.Items.Add(new ListItem(DateTime.Now.AddYears(2).Year.ToString()));
DropDownListLehrbeginn.Items[1].Selected = true;

不幸的是,它不起作用。我该如何解决这个问题? DropDownListLehrbeginn 在后面的代码中不可用

【问题讨论】:

标签: c# asp.net code-behind findcontrol


【解决方案1】:

您的代码看起来不错。因为您的下拉列表在另一个元素中,您可能只需要使用类似这样的东西

DropDownList ddlList = (DropDownList)NameOfGridView.FindControl("DropDownListLehrbeginn");

【讨论】:

  • 它对我不起作用。下拉列表保持为空
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-04-30
  • 1970-01-01
  • 2018-01-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多