Populate Repeater using Business Class in ASP.Net

Last Reply on Apr 10, 2018 03:07 AM By pandeyism

Posted on Apr 10, 2018 02:46 AM

Sir,

As you can see that the above html has a repeater control and that need to bind with separate class file as BindRepeater.cs. I am not getting any idea how to do it. Kindly show a demo.

 HTML code : 

<asp:Repeater ID="Repeater1" runat="server">
      <HeaderTemplate>
         <table id="tblCustomers" class="footable" border="0" cellpadding="0" cellspacing="0">
           <thead>  <tr>
                <th data-class="expand" align="center" style="text-align:center !important;">
                    Sr.No.
                </th>
                <th data-class="expand" align="center" style="text-align:center !important;">
                     Username
                </th>
                <th align="center" style="text-align:center !important;">
                     Time Stamp
                </th>
                <th style="display:table-cell;text-align:center !important;"  align="center" >
                    IP Address
                </th>
                <th style="display:table-cell;text-align:center !important;"  align="center" >
                     Activities 
                </th>
                <th scope="col" align="center" style="text-align:center !important;">
                      Login Status
                </th>
                </tr></thead>                                                                                           
      </HeaderTemplate>
      <ItemTemplate>
             <tbody><tr>
                <td align="center" style="text-align:center">
                
                                <%# Server.HtmlEncode(  Eval("id").ToString() )%>
                            
                <%--<%#Container.ItemIndex+1 %>--%></td>
                <td height="23px" style="text-align:center">
                     <%#Server.HtmlEncode(Eval("[username]").ToString())%> 
                </td> 
                <%----%>
                <td height="23px" style="text-align:left;word-wrap: normal; word-break: break-all;" >
                    <%#Server.HtmlEncode(Eval("[logintime]").ToString())%>  
                </td>
                <td height="23px" style="text-align:center">
                      <%#Server.HtmlEncode(Eval("[ipaddress]").ToString())%> </td>
                <td height="23px" style="text-align:center">
                  <%#Server.HtmlEncode(Eval("[action_performed]").ToString())%></td>
 <td height="23px" style="text-align:center" >
                                                                                                             <%#Server.HtmlEncode(Eval("[status]").ToString())%>
                                                                                                            </td>
             </tr></tbody> 
      </ItemTemplate>
      <FooterTemplate>
    </Table>
   </FooterTemplate>
 </asp:Repeater>

BindRepeater.cs 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;

/// <summary>
/// Summary description for BindRepeater
/// </summary>
public class BindRepeater
{
    private Int32 _pageIndex, _pageSize, _recordCount;

    public Int32 pageIndex
    {
        get{ return _pageIndex; }
        set{ _pageIndex = value; }
    }
    public Int32 pageSize
    {
        get { return _pageSize; }
        set { _pageSize = value; }
    }
    public Int32 pageSize
    {
        get { return _recordCount; }
        set { _recordCount = value; }
    }

    public static void RepeaterBinding()
    {

        SqlConnection conn = MyConnection.ConnectionStr();
        SqlCommand cmd = new SqlCommand("sp_PagedAuditTrails", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@pageIndex", pageIndex);
                cmd.Parameters.AddWithValue("@pageSize", int.Parse(ddlPageSize.SelectedValue));
                cmd.Parameters.Add("@recordCount", SqlDbType.Int, 4);
                cmd.Parameters["@recordCount"].Direction = ParameterDirection.Output;
                con.Open();
                IDataReader idr = cmd.ExecuteReader();
                Repeater1.DataSource = idr;

                // SqlDataAdapter myAdapter = new SqlDataAdapter(cmd);
                // DataTable dt = new DataTable();
                // myAdapter.Fill(dt);
                // rptAuditTrail.DataSource = dt;
                Repeater1.DataBind();
                idr.Close();
    }

}

 

You are viewing reply posted by: pandeyism on Apr 10, 2018 03:07 AM.
Posted on Apr 10, 2018 03:07 AM Modified on one year ago

Hi Sumeet,

Check this example. Now please take its reference and correct your code.

Create a class file and write your business logic and based on data you want to return from the class file assign the return type for the method.

HTML

<asp:Repeater ID="rptCustomers" runat="server">
    <HeaderTemplate>
        <table>
            <tr>
                <th>
                    CustomerID
                </th>
                <th>
                    CompanyName
                </th>
                <th>
                    City
                </th>
            </tr>
    </HeaderTemplate>
    <ItemTemplate>
        <tr>
            <td>
                <asp:Label ID="lblID" runat="server" Text='<%#Eval("CustomerID") %>'></asp:Label>
            </td>
            <td>
                <asp:Label ID="lblName" runat="server" Text='<%#Eval("CompanyName") %>'></asp:Label>
            </td>
            <td>
                <asp:Label ID="lblCity" runat="server" Text='<%#Eval("City") %>'></asp:Label>
            </td>
        </tr>
    </ItemTemplate>
    <FooterTemplate>
        </table>
    </FooterTemplate>
</asp:Repeater>

Namespaces

C#

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

VB.Net

Imports Microsoft.VisualBasic
Imports System.Data.SqlClient

Code

BusinessData.cs

public SqlDataReader PopulateRepeater()
{
    string str = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
    SqlConnection conn = new SqlConnection(str);
    conn.Open();
    SqlCommand cmd = new SqlCommand("SELECT TOP 5 CustomerID,CompanyName,City FROM Customers", conn);
    SqlDataReader dr = cmd.ExecuteReader();
    return dr;
}

BusinessData.vb

Public Function PopulateRepeater() As SqlDataReader
 Dim str As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
 Dim conn As SqlConnection = New SqlConnection(str)
 conn.Open()
 Dim cmd As SqlCommand = New SqlCommand("SELECT TOP 5 CustomerID,CompanyName,City FROM Customers", conn)
 Dim dr As SqlDataReader = cmd.ExecuteReader()
 Return dr
End Function

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        BusinessData business = new BusinessData();
        System.Data.SqlClient.SqlDataReader dr = business.PopulateRepeater();
        this.rptCustomers.DataSource = dr;
        this.rptCustomers.DataBind();
    }
}

 VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Dim business As BusinessData = New BusinessData()
        Dim dr As System.Data.SqlClient.SqlDataReader = business.PopulateRepeater()
        Me.rptCustomers.DataSource = dr
        Me.rptCustomers.DataBind()
    End If
End Sub

Screenshot