Friday 9 August 2013

Example of Repeater Control in Asp.Net







Description:-

            In this Example we Explain that How to Create Repeater Control or Example of Repeater Control in Asp.Net.

Repeater Control are used to Bind the data that are Repeated.that is same like Gridview but Repeater are used when data is Repeated same and Gridview are used when Data is Displayed in Table Format.

Repeater Control Defines the number of Templater are as follow:

·         ItemTemplate

Are used to Define the item which is rendered from data source collection.


·         AlternatingItemTemplate

Are used to Change the Style of the Repeater Control like Background color etc…

·         HeaderTemplate
Are used to Display the Coloum Name as an Header.

·         FooterTemplate
Are used to Display the Coloum as an Footer of the Reapeter Control.

·         SeparatorTemplate

Are used to Separte the Each item in a Repeater Control.the separte element are <br> and <hr> html element.

First you have to Create the Following Table with name comment



ModalPopup Example For Insert,Update,Delete in Popup CRUD Operation in ModalPopup

Export Gidview Data to PDF File Transfer Gridview Rows to PDF File

Sorting Data in Gridview Sorting in Gridview based on Coloumn name


repeter.aspx:-



<%@ Page Language="C#" AutoEventWireup="true" CodeFile="repetar.aspx.cs" Inherits="repetar" %>

<!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 id="Head1" runat="server">
<title>Repeator Control Example</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>Enter your Name: </td>
<td><asp:TextBox ID="txtnm" runat="server"/></td>
</tr>

<tr>
<td valign="top">Enter your Comments:</td>
<td><asp:TextBox ID="txtcmt" runat="server" Rows="7" Columns="25" TextMode="MultiLine"/></td>
</tr>
<tr>
<td></td>
<td><asp:Button ID="btnSubmit" runat="server" Text="Submit" onclick="btnSubmit_Click" /></td>
</tr>
</table>
</div>
<div>
<asp:Repeater ID="RepDetails" runat="server">
<HeaderTemplate>
<table style=" border:1px solid #cf3050; width:500px" cellpadding="0">
<tr style="background-color:#cf3050; color:White">
<td colspan="2">
<b>Comments same like in Facebook Application</b>
</td>
</tr>
</HeaderTemplate>
<ItemTemplate>

<tr>
<td>
<asp:Label ID="lblComment" runat="server" Text='<%#Eval("comment") %>'/>
   
</td>
</tr>
<tr>
<td>
<table style="background-color:#cf3050;border-top:1px dotted #cf3050;border-bottom:1px solid #cf3050; width:500px" >
<tr>
<td>Post By: <asp:Label ID="lblUser" runat="server" Font-Bold="true" Text='<%#Eval("unm") %>'/>
 
</td>
<td>Created Date:<asp:Label ID="lblDate" runat="server" Font-Bold="true" Text='<%#Eval("postdate") %>'/></td>
</tr>
</table>
</td>
</tr>

</table>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater></div>
     <div style="overflow: hidden;">
        <asp:Repeater ID="rptPaging" runat="server" onitemcommand="rptPaging_ItemCommand">
            <ItemTemplate>
                                <asp:LinkButton ID="btnPage"
                 style="background:#cf3050; border:solid 1px #510;padding:6px; margin:5px; "
                CommandName="Page" CommandArgument="<%# Container.DataItem %>"
                 runat="server" ForeColor="White" Font-Bold="True"><%# Container.DataItem %>
                                </asp:LinkButton>
           </ItemTemplate>
        </asp:Repeater>
        </div>

</form>
</body>
</html>



repeter.aspx.cs:-


using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.WebControls;
using System.Data.Linq;
using System.Collections;

public partial class repetar : System.Web.UI.Page
{
    private SqlConnection con = new SqlConnection("Data Source=SQLDB;Initial Catalog=Demo;User ID=Demoh;Password=Demo1@");
    protected void Page_Load(object sender, EventArgs e)
    {
      
        if (!IsPostBack)
        {
            bindcomment();
        }
    }
   
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        con.Open();
        SqlCommand cmd = new SqlCommand("insert into comment (unm,comment,postdate) values(@userName,@comment,@postedDate)", con);
        cmd.Parameters.AddWithValue("@userName", txtnm.Text);

        cmd.Parameters.AddWithValue("@comment", txtcmt.Text);
        cmd.Parameters.AddWithValue("@postedDate", DateTime.Now);
        cmd.ExecuteNonQuery();
        con.Close();
        txtnm.Text ="";

        txtcmt.Text = "";
        bindcomment();
    }
  
    //Bind Data to Repeater Control
    protected void bindcomment()
    {
         DataClasses2DataContext dc = new DataClasses2DataContext();
      
        SqlCommand cmd = new SqlCommand("select * from comment Order By postdate desc", con);
        DataTable dt = new DataTable();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        da.Fill(dt);
      

    PagedDataSource pgitems = new PagedDataSource();
        DataView dv = new DataView(dt);
        pgitems.DataSource = dv;
        pgitems.AllowPaging = true;
        pgitems.PageSize = 2;
        pgitems.CurrentPageIndex = PageNumber;
        if (pgitems.PageCount > 1)
        {
            rptPaging.Visible = true;
            ArrayList pages = new ArrayList();
            for (int i = 0; i < pgitems.PageCount; i++)
                pages.Add((i + 1).ToString());
            rptPaging.DataSource = pages;
            rptPaging.DataBind();
        }
        else
        {
            rptPaging.Visible = false;
        }
            RepDetails.DataSource = pgitems;
            RepDetails.DataBind();  
    }
    public int PageNumber
    {
        get
        {
            if (ViewState["PageNumber"] != null)
                return Convert.ToInt32(ViewState["PageNumber"]);
            else
                return 0;
        }
        set
        {
            ViewState["PageNumber"] = value;
        }
     }
    protected void rptPaging_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        PageNumber = Convert.ToInt32(e.CommandArgument) - 1;
        bindcomment();
    }


}
 

5 comments:

  1. very informative. thanks

    take a look into this article for beginners http://www.etechpulse.com/2014/04/aspnet-repeater-control-c-example.html

    ReplyDelete
  2. What is the use of DataClasses2DataContext dc = new DataClasses2DataContext();
    This Class?

    ReplyDelete
  3. this is the name of linq to sql dataclasses.

    ReplyDelete