Thursday, August 8, 2013

Example of Repeater Control in Asp.Net







Source Code

<div>
        <asp:Repeater ID="myRepeater" runat="server">
            <HeaderTemplate>
                <table width="400" border="1">
                    <tr>
                        <th>
                            Roll </th>
                        <th>
                            Age  </th>
                        <th>
                            Date </th>
                        <th>
                            Name </th>
                    </tr>
            </HeaderTemplate>
            <ItemTemplate>
                <tr>
                    <td>
                        <%# Eval("roll") %> </td>
                    <td>
                        <%# Eval("age") %>  </td>
                    <td>
                        <%# Eval("infdate") %> </td>
                    <td>
                        <%# Eval("name") %>  </td>
                </tr>
            </ItemTemplate>
            <FooterTemplate>
                <tr>
                    <td colspan="4">
                        Example of Repeater Control
                    </td>
                </tr>
                </table>
            </FooterTemplate>
        </asp:Repeater>
    </div>

C# Code

using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;


public partial class FrmRepeaterControl : System.Web.UI.Page
{
    SqlConnection con;
    SqlCommand cmd;
    SqlDataReader dr;
    SqlDataAdapter da;
    DataSet ds;
    protected void Page_Load(object sender, EventArgs e)
    {
        //Declare connection in web.config
        con = new SqlConnection(ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString);
        getRepeater();
    }

    private void getRepeater()
    {

        con.Open();
        cmd = new SqlCommand("select * from fms_demo", con);
        dr = cmd.ExecuteReader();
        myRepeater.DataSource = dr;
        myRepeater.DataBind();
        con.Close();
       
    }
}

No comments:

Post a Comment