这就是我在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
答案 0 :(得分:1)
您的代码看起来很好。因为你的下拉列表在另一个元素中,你可能只需要使用类似这样的东西
DropDownList ddlList = (DropDownList)NameOfGridView.FindControl("DropDownListLehrbeginn");