First problem: Your
Repeater
will contain three copies of its
<ItemTemplate>
. Asking the repeater to find a control defined in its item template won't work, as it doesn't know which copy of the control you want. You need to call
FindControl
on the
RepeaterItem
, which is passed in the
RepeaterItemEventArgs
:
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
var ddlWeek = e.Item.FindControl("ddlWeek") as DropDownList;
....
}
Second problem: Your
DropDownList
is inside a nested
Repeater
control, which doesn't have a data source.
If you don't need the nested repeater, remove it:
<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound">
<HeaderTemplate>
Welcome
</HeaderTemplate>
<ItemTemplate>
<table>
<tr><td>
<asp:TextBox ID="txtTimePeriodName" Text='<%# Eval("Name") %>' runat="server" />
</td></tr>
<tr><td>
<asp:DropDownList ID="ddlWeek" runat="server" />
</td></tr>
</table>
</ItemTemplate>
</asp:Repeater>
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
LoadWeeks((DropDownList)e.Item.FindControl("ddlWeek"));
}
public void LoadWeeks(DropDownList ddlWeek)
{
...
}
If you
do need the nested repeater, then you'll need to change your code to assign a data source to it, and bind the list from the
Repeater2_ItemDataBound
event.
Since you're using a static list of items, a better approach would be to bind the list in the control's
Init
event:
<asp:DropDownList ID="ddlWeek" runat="server"
onInit="ddlWeek_Init"
/>
protected void ddlWeek_Init(object sender, EventArgs e)
{
LoadWeeks((DropDownList)sender);
}
public void LoadWeeks(DropDownList ddlWeek)
{
...
}
Alternatively, define the items in the markup:
<asp:DropDownList ID="ddlWeek" runat="server">
<asp:ListItem Value="1" Text="M" />
<asp:ListItem Value="2" Text="Tu" />
<asp:ListItem Value="3" Text="W" />
<asp:ListItem Value="4" Text="Th" />
<asp:ListItem Value="5" Text="F" />
<asp:ListItem Value="6" Text="S" />
</asp:DropDownList>