ASP.Net Repeater Databound Control Databinding feature displays the data items retrieved from any DataSource in the specified repeated list layout. Here we will discuss the ASP.Net C# code to connect with SQL Server Database and Repeater control databinding using its DataSource object that gets or sets the data provided by the data source for populating the repeated list items. In the previous article about ASP.Net Repeater Control we discussed about 5 types of templates that can be sued to customize the layout of Repeater DataBound Control. ItemTemplate field is a required template to display the data retrieved from the DataSource. You can specify the heading for data list items in the HeaderTemplate.
In this Repeater Control Tutorial we will use Northwind SQL database to bind the data with Repeater Control. Define the Following connectionstring in the web.config file’s ConnectionStrings section:
<add name="ConStr"
connectionString="Server=localhost; Database=northwind; uid=sa; pwd=;"
</add>
<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate>
<h4>Emplyee Names</h4>
</HeaderTemplate>
<ItemTemplate>
<br />
</ItemTemplate>
<SeparatorTemplate>
<hr />
</div>
DataBinder class provides the Eval function that accepts two parameters. First parameter value as a reference against the DataItem object using Container variable of as RepeaterItem that is to be evaluated. Second parameter is a string type expression that passes the name of Data Field retrieved from the DataSource.
// string variable to store the connection string
// defined in connectionstrings section of web.config file.
// object created for SqlConnection Class.
// if condition that can be used to check the sql connection
// whether it is already open or not.
{
mySQLconnection.Open();
}
// SQL Select Command
mySqlSelect.CommandType = CommandType.Text;
mySqlAdapter.Fill(myDataSet);
// Repeater Control Databinding using Datasource
Repeater1.DataSource = myDataSet;
Repeater1.DataBind();
// if it is open then close it.
mySQLconnection.Close();
Above Code will generate the following HTML source code
<div>
Nancy Davolio
Andrew Fuller
Janet Leverling
Margaret Peacock
Steven Buchanan
Michael Suyama
Robert King
Laura Callahan
Anne Dodsworth
Output:
Above output result of ASP.Net repeater control shows that Repeater control does not render extra HTML tags like <table> HTML tag in DataList or GridView Databound controls of ASP.Net
Be the first to rate this post
Tags: asp.net 2.0, c#, asp.net repeater control, asp.net repeater databinding, c# code, asp.net repeater datasource, asp.net sample, asp.net sql connectionstring, asp.net repeater dynamic, asp.net nested repeater, asp.net repeater table, asp.net repeater div, web.config connectionstrings, c# repeater databinding
10/11/2008 3:24:17 AM