Sunday 4 March 2012

C# - ASP.NET - How to load data to Repeater with Literal and Bulleted List

Watch on youtube:



            // this example will show how to use Repeater with Literal and Bulleted List
            // I have 2 tables - Department and Course
            // 1 Department can have 0, 1 or more Courses
            // I am using Entity Data Source in this example







        protected void Page_Load(object sender, EventArgs e)
        {
            using (SchoolEntities schoolContext = new SchoolEntities())
            {
                var dpt = from d in schoolContext.Department
                          orderby d.Name
                          select new
                          {
                              DepartmentName = d.Name,
                              Courses = d.Course
                          };

                Repeater1.DataSource = dpt;
                Repeater1.DataBind();
            }
        }

--Code Behind:

    <div>
        <asp:Repeater ID="Repeater1" runat="server">
            <ItemTemplate>
                <h3>
                    <asp:Literal ID="Literal1" Text='<%# Eval("DepartmentName") %>' runat="server"></asp:Literal>
                </h3>
                <asp:BulletedList ID="BulletedList1" DataSource='<%# Eval("Courses") %>' DataTextField="Title" runat="server">
                </asp:BulletedList>
            </ItemTemplate>
        </asp:Repeater>
    </div>

No comments:

Post a Comment