Saturday, June 14, 2014

Repeater Control

Repeater control can display multiple rows similar to GridView and DataList. But unlike GridView and DataList the data in repeater is repeater is ReadOnly. Similar to DataList it has no predefined layout and you can design it with any layout. As repeater is read-only, it has very limited set of properties, methods and events and hence it is very light weight and provides fast access. It has the important properties DataSourceId and DataSource. It has only one important event ItemCommand that will be raised when user click on any button within the repeater.
Templates Supported by the Repeater Control:-

Ø  HeaderTemplate:  Determines the heading of the final output. It is rendered only once and prior to any row. It cannot contain data bound information.
Ø  ItemTemplate: Determines the output format of each row in the data source. This template is called for each item in the list and can contain data binding expressions.
Ø  AlternatingItemTemplate: Functions similarly to ItemTemplate. This template applies only to rows with an odd ordinal position.
Ø  SeparatorTemplate: Determines the HTML content that goes between each row. It cannot contain data bound information.
Ø  FooterTemplate: Rendered only once when all items have been rendered. It cannot contain data bound information.

Example:-The following example demonstrates how to display the data in repeater.
1.      Add a page to the website, take a repeater control on it and design it as follows within the html source of the page.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Repeater.aspx.cs" Inherits="Repeater" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
  <title>Repeater Control Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <div>
            <asp:Label ID="Label4" runat="server" Font-Size="Larger" Text="Repeater Control Example"></asp:Label>
        </div>
        <asp:Repeater ID="Repeater1" runat="server">
            <HeaderTemplate>
                <table>
                    <tr>
                        <th>
                            <asp:Label ID="Label1" runat="server" Text="username"></asp:Label>
                        </th>
                        <th>
                            <asp:Label ID="Label2" runat="server" Text="email"></asp:Label>
                        </th>
                        <th>
                            <asp:Label ID="Label3" runat="server" Text="regdate"></asp:Label>
                        </th>
                    </tr>
                    </HeaderTemplate>
            <ItemTemplate>
                <tr>
                    <td>
                          <asp:Label ID="Label1" runat="server" Text='<%#Eval("username") %>'></asp:Label>
                    </td>
                    <td>
                          <asp:Label ID="Label5" runat="server" Text='<%#Eval("email") %>'></asp:Label>
                    </td>
                    <td>
                          <asp:Label ID="Label6" runat="server" Text='<%#Eval("regdate") %>'></asp:Label>
                    </td>
                </tr>
            </ItemTemplate>
            <FooterTemplate>
                </table>
            </FooterTemplate>          
        </asp:Repeater>
    </div>
    </form>
</body>
</html>

2.      Write the connection string in web.config file.
<configuration> 
  <connectionStrings>
    <add name="constr" connectionString="uid=sa;pwd=password;database=mydb;" providerName="System.Data.SqlClient"/>
  </connectionStrings>
</configuration>

3.      Write the following code within the page load event.
protected void Page_Load(object sender, EventArgs e)
{
     string c =   WebConfigurationManager.ConnectionStrings["constr"].ToString();
    SqlConnection con = new SqlConnection(c);
    SqlCommand cmd = new SqlCommand("select * from account", con);
    con.Open();
    SqlDataReader dr = cmd.ExecuteReader();
    Repeater1.DataSource = dr;
    Repeater1.DataBind ();
    con.Close();
    }

No comments:

Post a Comment