In this tutorial, i will tell you how to bind Repeater using Linq in asp.net. This is my First Example in Viusal Studio 2010 and Framework 4.O.
Overview
- Introduction
- Where to use Repeater
- Create Project
- Countries Classes
- Repeater Control
- Inheritance Hierarchy
- Framework version information
- Output
- Conclusion
- Download
- Demo
Introduction
Basic templated data bound list is called Repeater.In Repeater control you can use markup tags e.g table,tr,td. under the HeaderTemplate table tag <Table> is used. A table row and columns tag <tr>, <td> include ItemTemplate. and at the end of table tag </table> include in the FooterTemplate.Namespace of Repeater is System.Web.UI.WebControls.
Where to use Repeater
When you have to display a data in way like Table , Div Span tags then you have to use Repeater.DataList and Datagrid Automatically genrate HTML Markup according to devloper Specified.
Repeater have five template.
- AlternatingItemTemplate
- FooterTemplate
- HeaderTemplate
- ItemTemplate
- SeparatorTemplate
Syntax:
<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate>
<table>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td> </td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
Create Project
Open your Visual Studio 2010 and Create a Solution by selecting .Net Framework 4.O. Next Right Click on your Solution Explorer at Right top of Visual Studio Window, Now Create a New web site.

Classess
From Solution Explorer, Right click on your web site and create a App_code folder by selecting Add asp.net Folder. Create a CountriesBaseClass.cs which has three properties of different type,string and int. as given below.
public class CountriesBaseClass
{ private int _countryId;
private string _countryName;
private string _countryCode;
public CountriesBaseClass()
{}
public int CountryID
{
get { return _countryId; }
set { _countryId = value; }
}
public string CountryName
{
get { return _countryName; }
set { _countryName = value; }
}
public string CountryCode
{
get { return _countryCode; }
set { _countryCode = value; }
}

Right Click on App_code folder in your project. click on Add New Item a small window will be open as shown below figure.Countries class Will return all countries which will be populated with a number of country Object.

A List<CountriesBaseClass> is instantiated.First List<CountriesBaseClass> will be created. and return the list.
Create a class instance as given below. Here, I am using a Generics based List collection of type CountriesBaseClass

List _Countries = new List
{
new CountriesBaseClass { CountryID=1,CountryName= "Afghanistan", CountryCode= "9AF"},
new CountriesBaseClass { CountryID=2,CountryName= "Albania",CountryCode= "9AL"},
new CountriesBaseClass { CountryID=3,CountryName= "Algeria", CountryCode= "9AG"},
new CountriesBaseClass { CountryID=4,CountryName= "Andorra",CountryCode= "9AN"},
new CountriesBaseClass { CountryID= 5,CountryName= "Angola", CountryCode= "9AO"},
new CountriesBaseClass { CountryID= 6,CountryName= "Anguilla",CountryCode= "9AV"},
new CountriesBaseClass { CountryID=7,CountryName= "Antigua and Barbuda", CountryCode= "9AC"},
new CountriesBaseClass { CountryID=8,CountryName= "Argentina", CountryCode= "9AR"},
new CountriesBaseClass { CountryID=9,CountryName= "Ashmore Cartier Islands", CountryCode= "9AT"},
new CountriesBaseClass { CountryID=10,CountryName= "Australia",CountryCode= "9AS"},
new CountriesBaseClass { CountryID= 11,CountryName= "Austria", CountryCode= "9AU"},
new CountriesBaseClass { CountryID= 12,CountryName= "Bahamas, The", CountryCode= "9BF"},
new CountriesBaseClass { CountryID= 13,CountryName= "Bahrain", CountryCode= "9BA"},
new CountriesBaseClass { CountryID=14,CountryName= "Bangladesh",CountryCode= "9BG"},
new CountriesBaseClass { CountryID=15,CountryName= "Barbados", CountryCode= "9BB"},
new CountriesBaseClass { CountryID=16,CountryName= "Bassa da India", CountryCode= "9BS"},
new CountriesBaseClass { CountryID= 17,CountryName= "Belgium", CountryCode= "9BE"},
new CountriesBaseClass { CountryID=18,CountryName= "Belize", CountryCode= "9BH"},
new CountriesBaseClass { CountryID= 19,CountryName= "Benin", CountryCode= "9DM"},
new CountriesBaseClass { CountryID= 20,CountryName= "Bermuda",CountryCode= "9BD"},
new CountriesBaseClass { CountryID= 21,CountryName= "Bhutan", CountryCode= "9BT"},
new CountriesBaseClass { CountryID= 22,CountryName= "Bolivia",CountryCode= "9BL"},
new CountriesBaseClass { CountryID=23,CountryName= "Botswana", CountryCode= "9BC"},
new CountriesBaseClass { CountryID=24,CountryName= "Bouvet Island",CountryCode= "9BV"},
new CountriesBaseClass { CountryID= 25,CountryName= "Brazil", CountryCode= "9BR"},
new CountriesBaseClass { CountryID= 26,CountryName= "British Indian Ocean Territory", CountryCode= "910"},
new CountriesBaseClass { CountryID= 27,CountryName= "British Virgin Islands",CountryCode= "9VI"},
new CountriesBaseClass { CountryID= 28,CountryName= "Brunei", CountryCode= "9BX"},
new CountriesBaseClass { CountryID= 29,CountryName= "Bulgaria",CountryCode= "9BU"},
new CountriesBaseClass { CountryID= 30,CountryName= "Burma", CountryCode= "9BM"},
new CountriesBaseClass { CountryID= 31,CountryName= "Burundi", CountryCode= "9BY"},
new CountriesBaseClass { CountryID= 32,CountryName= "Cameroon",CountryCode= "9CM"},
new CountriesBaseClass { CountryID= 33,CountryName= "Canada",CountryCode= "9CA"},
new CountriesBaseClass { CountryID= 34,CountryName= "Cape Verde",CountryCode= "9CV"},
new CountriesBaseClass { CountryID= 35,CountryName= "Cayman Islands",CountryCode= "90"},
new CountriesBaseClass { CountryID= 36,CountryName= "Central African Republic", CountryCode= "9CT"},
new CountriesBaseClass { CountryID=37,CountryName= "Chad",CountryCode= "9CD"},
new CountriesBaseClass { CountryID=38,CountryName= "Chile",CountryCode= "9CI"},
new CountriesBaseClass { CountryID= 39,CountryName= "China",CountryCode= "9CH"},
new CountriesBaseClass { CountryID= 40,CountryName= "Christmas Island",CountryCode= "9KT"}
}
getAllCountries function, Linq Query will return All countries as given Below.
public List getAllCountries()
{
var r = from u in _Countries
select u;
return r.ToList();
}
HTML Mark Up
Now In Default.aspx Page, Drag and Drop Repeater from Toolbox as shown in belwo figure.


Server Side Coding
Open your Default.aspx.cs Page, and on Page Load,Create a Countries Class Object pass getAllCountries Function Pass to data source and Bind it as show below.
Countries Obj = new Countries();
Repeater1.DataSource = Obj.getAllCountries();
Repeater1.DataBind();
Inheritance
System.Web.UI.WebControl.Repeater inherit from System.Web.UI.Control and System.Web.UI.Control inherit from System.Object.
Framework version information
Repeater is support in .Net framework 1.0, 1.1, 2.O, 3.O, 3.5 and 4.O.
Output

Download:
Repeater.rar (129.18 kb)
Demo:
Click here to View online sample.
For More Detail visit msdn
Repeater Class