Bind Repeater from Code Behind using DataTable in ASP.Net

How to Bind Repeater from Code Behind using DataTable in ASP.Net?

The data from the SQL Server database table will be populated into a DataTable in code behind and then the DataTable will be used to populate the Repeater control in ASP.Net. 





The Repeater control makes use of the following templates.
HeaderTemplate – The content of this template will not be repeated and will be placed in the top most position i.e. head section of the Repeater control.
ItemTemplate – The content of this template will be repeated for each record present in its DataSource.
AlternatingItemTemplate – AlternatingItemTemplate is used for adding alternate items. It is used along with ItemTemplate, generally for displaying a different design for alternating items.
SeparatorTemplate - This template is used to add a separator between two items of the Repeater control.
FooterTemplate – The content of this template will not be repeated and will be placed in the bottom most position i.e. footer section of the Repeater control.



HTML Markup:
The following HTML Markup consists of an ASP.Net Repeater control.
In order to render the Repeater control in HTML Table layout, you will need to design an HTML Table and then place the Table start tag and the Header Row inside the Repeater’s HeaderTemplate section, the Data Rows i.e. the Rows that will hold the data inside the ItemTemplate section and finally the Table end tag inside the FooterTemplate section.
The HeaderTemplate and the FooterTemplate sections are not repeated. The ItemTemplate section will repeat itself based on the data fetched from the database.


<asp:Repeater ID="rptCustomers" runat="server">
<HeaderTemplate>
<table cellspacing="0" rules="all" border="1">
<tr>
<th scope="col" style="width: 80px">
Customer Id
</th>
<th scope="col" style="width: 120px">
Customer Name
</th>
<th scope="col" style="width: 100px">
Country
</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:Label ID="lblCustomerId" runat="server" Text='<%# Eval("CustomerId") %>' />
</td>
<td>
<asp:Label ID="lblContactName" runat="server" Text='<%# Eval("ContactName") %>' />
</td>
<td>
<asp:Label ID="lblCountry" runat="server" Text='<%# Eval("Country") %>' />
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>





Behind Code:




using System.Data;
using System.Data.SqlClient;
using System.Configuration;



Inside the Page load event handler 


protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindRepeater();
}
}

private void BindRepeater()
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT TOP 10 * FROM Customers", con))
{
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
sda.Fill(dt);
rptCustomers.DataSource = dt;
rptCustomers.DataBind();
}
}
}
}

No comments:

Post a Comment