Saturday, 7 July 2012

How To create a Repeater and give the Field name?

Here I will explain how to create a repeater .....


Definition of Repeater Control?
Repeater -It is used to display the repeated list of items that is called Repeater.
Here Explain Repeater control and Templates
The Repeater control makes use of the following templates.
Ø  ItemTemplate – It is use this template repeated for each record present in its DataSource.

Ø  SeparatorTemplate – Separator Template add a separator between two items of the Repeater control.


Ø  FooterTemplate – Footer template will not be repeated and will be placed in the bottom most position i.e. footer section of the Repeater control.

Ø  AlternatingItemTemplate – It is used for adding alternate items. It is used along with ItemTemplate.


Ø  HeaderTemplate – Header Content template will not be repeated and will be placed in the top most position (head section of the Repeater control).

 For Example:- 

Create Database 
Database Table Name: Employee_Details
Create New Table -Employee
Table Field Name- ID Int
                                 EmployeeName Varchar(50)
                                 Salary float
Here i am designing repeater in asp.net page.... 

<div>
<
asp:Repeater ID="rpt" runat
="server">
<
HeaderTemplate
>
<
table
>
<
tr
>
<
b
>
<
td
>
ID</td
>
<
td
>
Employee Name
</td
>
<
td
>
Salary
</td
>
</
b
>
</
tr
>
</
HeaderTemplate
>
<
ItemTemplate
>
<
tr
>
<
td
>
<%#DataBinder.Eval(Container,"DataItem.ID"
)%>
</td>
<
td
>
<%#DataBinder.Eval(Container,"DataItem.
Employee Name
"
)%>
</td>
<
td
>
<%#DataBinder.Eval(Container,"DataItem.Salary"
)%>
</td>
</
tr
>
</
ItemTemplate
>
<
SeparatorTemplate
>
<
tr
>
<
td
>
<
hr
/>
</
td
>
<
td
>
<
hr
/>
</
td
>
<
td
>
<
hr
/>
</
td
>
</
tr
>
</
SeparatorTemplate
>
<
AlternatingItemTemplate
>
<
tr
>
<
td
>
<%#DataBinder.Eval(Container,"DataItem.ID"
)%>
</td>
<
td
>
<%#DataBinder.Eval(Container,"DataItem.EmployeeName"
)%>
</td>
<
td
>
<%#DataBinder.Eval(Container,"DataItem.Salary"
)%>
</td>
</
tr
>
</
AlternatingItemTemplate
>
<
SeparatorTemplate
>
<
tr
>
<
td
>
<
hr
/>
</
td
>
<
td
>
<
hr
/>
</
td
>
<
td
>
<
hr
/>
</
td
>
</
tr
>
</
SeparatorTemplate
>
<
FooterTemplate
>
<
tr
>
<
td
>
Employee Record Display
</td
>
</
tr
>
</
table
>
</
FooterTemplate
>
</
asp:Repeater
>
</
div
>
</
form
>
</
body
>
</
html>


Code behind file code


using
System;
using
System.Configuration;
using
System.Data;
using
System.Linq;
using
System.Web;
using
System.Web.Security;
using
System.Web.UI;
using
System.Web.UI.HtmlControls;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using
System.Xml.Linq;
using
System.Data.SqlClient;

public partial class _Default : System.Web.UI.
Page
{
SqlConnection
con;
SqlCommand cmd = new SqlCommand
();
protected void Page_Load(object sender, EventArgs
e)
{
con =
new SqlConnection(ConfigurationManager.ConnectionStrings["con"
].ConnectionString);
cmd.Connection = con;
cmd.CommandText =
"select * from Employee"
;
con.Open();
rpt.DataSource = cmd.ExecuteReader();
rpt.DataBind();
con.Close();
}
}


Now Complete Repeater .....

No comments:

Post a Comment

What is ASP.NET? Components of ASP.NET

ASP.Net Definition -It is used for creating web-based applications.ASP.Net is a web development platform provided by Microsoft. ASP.NET i...