Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My html code is like this
ASP.NET
<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"></asp:TextBox></td></tr>
            <tr><td>
     <asp:Repeater ID="Repeater2" runat="server">
                        <HeaderTemplate></HeaderTemplate>
                        <ItemTemplate>
      <asp:DropDownList ID="ddlWeek" runat="server"></asp:DropDownList>
                        </ItemTemplate>
                    </asp:Repeater>
                </td></tr>          
            </table>
        </ItemTemplate>
    </asp:Repeater>

I'm able to bind "Repeater1" by this code in .cs
C#
public void BindRepeater1()
  {
      List<TimePeriodName> TN = new List<TimePeriodName>()
      {
          new TimePeriodName(){Name="BreakFast"},
          new TimePeriodName(){Name="Lunch"},
          new TimePeriodName(){Name="Dinner"},
      };
      Repeater1.DataSource = TN;
      Repeater1.DataBind();
  }
  class TimePeriodName
  {
      public string Name { get; set; }

  }

Now i have to bind "ddlWeek". I'm doing like this
C#
   protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
   {
      LoadWeeks();
   }
public void LoadWeeks()
   {
       List<Weeks> week = new List<Weeks>()
       {
       new Weeks(){WeekNo=1,Week="M"},
       new Weeks(){WeekNo=2,Week="Tu"},
       new Weeks(){WeekNo=3,Week="W"},
       new Weeks(){WeekNo=4,Week="Th"},
       new Weeks(){WeekNo=5,Week="F"},
       new Weeks(){WeekNo=6,Week="S"},
       };
       DropDownList ddlweek = Repeater1.FindControl("ddlWeek ") as DropDownList;

           ddlweek.DataSource = week;
           ddlweek.DataTextField = "Week";
           ddlweek.DataValueField = "WeekNo";
           ddlweek.DataBind();
   }
   class Weeks
   {
       public Int16 WeekNo { get; set; }
       public string Week { get; set; }
   }

I'm getting null reference error at "ddlweek.DataSource = week;"
Can you show me a way to bind ddlWeek?

Thnx in advance.
Posted

1 solution

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:
C#
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:
aspx
<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>

C#
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:
aspx
<asp:DropDownList ID="ddlWeek" runat="server"
    onInit="ddlWeek_Init"
/>

C#
protected void ddlWeek_Init(object sender, EventArgs e)
{
    LoadWeeks((DropDownList)sender);
}

public void LoadWeeks(DropDownList ddlWeek)
{
    ...
}

Alternatively, define the items in the markup:
aspx
<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>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900