Share Blog

Tuesday, May 20, 2014

How To Use Repeater Control in ASP.NET

Introduction
A Repeater is a Data-bound control. Data-bound controls are container controls. It creates a link between the Data Source and the presentation UI to display the data. The repeater control is used to display a repeated list of items.
A Repeater has five inline templates to format it:
1. <HeaderTemplate>
2. <AlternatingItemTemplate>
3. <Itemtemplate>
4. <SeperatorTemplate>
5. <FooterTemplate>

<HeaderTemplate>

Displays Header text for a Data Source collection and applies a different style for the Header text.

<AlternatingItemTemplate>

Changes the background color or style of alternating items in a Data Source collection.

<Itemtemplate>

It defines how the each item is rendered from the Data Source collection.
<SeperatorTemplate>

It will determine the separator element that separates each item in the item collection. It will be a <br> or <Hr> HTML element.

<FooterTemplate>

Displays a footer element for the Data Source collection.
First I created a database "Repeater". Then I created a table in the database.
create table Repeater(username varchar(100),Subject varchar(max),Commenton varchar(max),Post_date date )
select * from Repeater
Select * from Repeater Order By Post_Date desc

Complete Program
 Repeater_Example.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Repeater_example.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title> Repeater Example in Asp.net</title>
    <style type="text/css">
        .style1
        {
            width: 100%;
        }
        .style2
        {
            width: 162px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <h3>Repeater Control Example</h3>
    <div>
   
        <table class="style1">
            <tr>
                <td class="style2">
                    <asp:Label ID="Label1" runat="server" Text="Enter Name"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="style2">
                    <asp:Label ID="Label2" runat="server" Text="Enter Subject"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="TextBox2" runat="server" TextMode="MultiLine"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="style2" valign="top">
                    <asp:Label ID="Label3" runat="server" Text="Enter Comments"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="TextBox3" runat="server" Columns="20" Rows="5"
                        TextMode="MultiLine"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="style2">
                    <asp:Button ID="Button1" runat="server" Text="Submit Information"
                        onclick="Button1_Click" />
                </td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td class="style2">
                    <asp:Label ID="Label5" runat="server" Text="Label"></asp:Label>
                </td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td class="style2">
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
            </tr>
        </table>
   
    </div>
   
    <asp:Repeater ID="Repeater1" runat="server">
    <HeaderTemplate>
    <table style="border:1px solid:red ; width:500px" cellpadding ="0" >
    <tr style ="background-color :Yellow ">
    <td>
    <b> Comment</b>
    </td>
    </tr>
   
    </HeaderTemplate>
    <ItemTemplate>
    <tr style ="background :green"></tr>
    <table>
    <tr>
    <td>
    Subject
    <asp:Label ID ="label1" runat="server"  Font-Bold ="true" Text ='<%#Eval("Subject") %>'>'></asp:Label>
    </td>
    </tr>
    </table>
    <tr>
    <td>
    Comments
    <asp:Label ID ="label2" runat ="server" Font-Bold ="true" Text ='<%#Eval("commenton") %>'>'></asp:Label>
    </td>
    </tr>
    <tr>
    <td> post by
    <asp:Label ID ="label3" runat ="server" Font-Bold ="true" Text ='<%#Eval("Username") %>'>'></asp:Label>

    </td>
    </tr>
    <tr>
    <td> create date
    <asp:Label ID="label4" runat ="server" Font-Bold ="true" Text ='<%#Eval("Post_date") %>'>' ></asp:Label>
    </td>
    </tr>
    </ItemTemplate>
    <FooterTemplate>
    </table>
    </FooterTemplate>
   
    </asp:Repeater>
   
    </form>
</body>
</html>






Repeter_Example.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class _Default : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection("Data Source=SUNIL;Initial Catalog=adonetpractices;Integrated Security=True");
    SqlDataAdapter da;
   
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Repeater();
        }

    }
    public void Repeater()
    {
        da = new SqlDataAdapter("select * from repeater",con);
        DataSet ds = new DataSet();
        da.Fill(ds);
        Repeater1.DataSource = ds;
        Repeater1.DataBind();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("insert into repeater values(@username,@subject,@commenton,@post_date)", con);
            cmd.Parameters.AddWithValue("@username", TextBox1.Text);
            cmd.Parameters.AddWithValue("@subject", TextBox2.Text);
            cmd.Parameters.AddWithValue("@commenton", TextBox3.Text);
            cmd.Parameters.AddWithValue("@post_date", DateTime.Now.Date);
            cmd.ExecuteNonQuery();
            con.Close();
            Repeater();
        }
        catch (Exception ex)
        {
            Label5.Text = ex.Message;
        }
    }
   
}

RESULT

 

No comments:

Post a Comment