Page

How to use repeater in ASP.net

// default.aspx //


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>ASP.net how to use repeater</title>
    <style type="text/css">
        #divEmployees
        {
            font-family:Arial, Verdana, Sans-Serif;
            font-size:12px;
            padding:10px;
            border:solid 1px #0066CC;
        }
        
        #divEmployees .detail
        {
            border-bottom:dashed 1px #0066CC;
            margin-bottom:10px;
            padding:10px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <fieldset>
            <h2>How to Use Repeater Control</h2>
        <div>
        <asp:Repeater ID="rptEmployees" runat="server">
            <HeaderTemplate>
                <div id="divEmployees">
            </HeaderTemplate>
            <ItemTemplate>
                <div class="detail">
                   <div>Name: <strong><%# Eval("FirstName") %> <%# Eval("LastName") %></strong></div>
                   <div>Address: <strong><%# Eval("Address") %></strong></div>
                   <div>State: <strong><%# Eval("State")%></strong></div>
                   <div>City: <strong><%# Eval("City")%></strong></div>
                   <div>Country: <strong><%# Eval("Country")%></strong></div>
                </div>
            </ItemTemplate>
            <AlternatingItemTemplate>
                <div class="detail">
                   <div>Name: <strong><%# Eval("FirstName") %> <%# Eval("LastName") %></strong></div>
                   <div>Address: <strong><%# Eval("Address") %></strong></div>
                   <div>State: <strong><%# Eval("State")%></strong></div>
                   <div>City: <strong><%# Eval("City")%></strong></div>
                   <div>Country: <strong><%# Eval("Country")%></strong></div>
                </div>
            </AlternatingItemTemplate>
            <FooterTemplate>
                </div>
            </FooterTemplate>
        </asp:Repeater>
        </div>
        </fieldset>
    </div>
    </form>
</body>
</html>


// default.aspx.cs //

using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class index : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //You need to include using system.collections.generic
        List<Employee> lstEmployees = new List<Employee>();

        //Add Employee info to the collection
        lstEmployees.Add(new Employee()
        {
            FirstName = "Mark",
            LastName = "Arton",
            HomePhone = "000-000-0000",
            CellPhone = "000-000-0000",
            Address = "Lane 1 Suite # 2",
            State = "Connecticut",
            City = "New Hamden",
            Zipcode = "06514",
            Country = "USA"
        });

        lstEmployees.Add(new Employee()
        {
            FirstName = "Tony",
            LastName = "Jacob",
            HomePhone = "000-000-0000",
            CellPhone = "000-000-0000",
            Address = "Lane 1 Suite # 2",
            State = "N/A",
            City = "London",
            Zipcode = "2314",
            Country = "United Kingdom"
        });

        lstEmployees.Add(new Employee()
        {
            FirstName = "Robert",
            LastName = "Brown",
            HomePhone = "000-000-0000",
            CellPhone = "000-000-0000",
            Address = "Lane 1 Suite # 2",
            State = "N/A",
            City = "Melbourne",
            Zipcode = "AB8799",
            Country = "Australia"
        });

        //Assign data source to the repeater
        rptEmployees.DataSource = lstEmployees;

        //You need to rebind the repeater
        rptEmployees.DataBind();
    }
}

public class Employee
{
    public string FirstName { get; set; }

    public string LastName { get; set; }

    public string HomePhone { get; set; }

    public string CellPhone { get; set; }

    public string Address { get; set; }

    public string City { get; set; }

    public string Zipcode { get; set; }

    public string State { get; set; }

    public string Country { get; set; }
}

No comments:

Post a Comment