Home Asp.net Bind Asp.net Repeater Control in C# or Vb.net Example

Bind Asp.net Repeater Control in C# or Vb.net Example

66
0
How to Bind Asp.net Repeater Control Example using C# or Vb.net?
How to Bind Asp.net Repeater Control Example using C# or Vb.net?

Now here in this tutorial, I’ll explain how you can load the data and binding repeater control in asp .net using ado.net executereader with sample example code in .net using c# as well as vb.net.

In my previous tutorials, I’d explained about custom asp.net repeater control with datapager, exporting gridview data to word excel text and pdf, gridview inline insert update delete, and other more cracking tutorials on Repeater, Gridivew, Asp.net here.

To explain further about how to bind asp.net repeater control using execute reader, we need to create a database table to read data and bind retrieved resultset to repeater, so simply execute following script to SQL query editor to create database table and then add few records manually.

Here is the script to create “SubjectDetails” table:

CREATE TABLE [dbo].[SubjectDetails]
(
    [SubjectId] [int] IDENTITY(1,1) NOT NULL PRIMARY KEY,
    [SubjectName] [nvarchar](100) NULL,
    [Marks] [nvarchar](50) NULL
)

HTML Markup To Bind Asp.net Repeater Control – [.aspx]

Following is the complete HTML Markup code that I used for this demonstration to data binding repeater control in asp net
for my .aspx page:

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Bind Repeater Control Example in Asp.net</title>
    <style type="text/css">
        /*Style your Repeater Control*/
        #rptCustom tr td {
            text-align: center;
            vertical-align: middle;
            padding: 3px;
        }

        #rptCustom tr {
            border: 1px solid #e2e2e2;
        }

        .rptHeader > th {
            background: #ff6600;
            font-weight: bold;
            color: White;
            width: 120px;
            height: 28px;
        }

        .rptDataRows:nth-child(odd) {
            background: #f5f5f5;
        }
        /*Style your DataPager Control*/
        .pager {
            float: left;
            height: 40px;
            margin-left: -5px;
            margin-top: 15px;
        }

            .pager ul li a, .pagerbuttons, .selected {
                float: left;
                font-size: 12px;
                font-weight: bold;
                height: 18px;
                margin: 0 5px;
                min-width: 20px;
                padding: 5px;
                text-align: center;
                vertical-align: middle;
                color: White;
            }

            .pager ul li a, .pagerbuttons {
                background: #ff6600;
            }

                .pager ul li a.active, .pager ul li a:hover, .selected, .pager a:hover {
                    background: #000;
                }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <div style="padding: 10px;">
            <h4>Bind Repeater Control Example in Asp.net</h4>
            <asp:Repeater ID="rptCustomRepeater" runat="server">
                <HeaderTemplate>
                    <table id="rptCustom">
                        <tr class="rptHeader">
                            <th>SubjectId</th>
                            <th>SubjectName</th>
                            <th>Marks</th>
                        </tr>
                </HeaderTemplate>
                <ItemTemplate>
                    <tr class="rptDataRows">
                        <td><%#DataBinder.Eval(Container.DataItem, "SubjectId")%></td>
                        <td><%#DataBinder.Eval(Container.DataItem, "SubjectName")%></td>
                        <td><%#DataBinder.Eval(Container.DataItem, "Marks")%></td>
                    </tr>
                </ItemTemplate>
                <FooterTemplate>
                    </table>
                </FooterTemplate>
            </asp:Repeater>
        </div>
    </form>
</body>
</html>

Now check the below sample code snippet to binding repeater control in asp net with executereader.

Binding Repeater Control in C# – [.cs]

Add the following namespace that is required to connect with SQL server:

using System.Data.SqlClient;

After that add the following code to .aspx.cs page:

protected void Page_Load(object sender, EventArgs e) {
    if (!IsPostBack) {
        BindData();
    }
}

//bind subject details to repeater control
private void BindData() {
    try {
        string strConn = @"Data Source=datasource;Integrated Security=true;Initial Catalog=yourDB";
        using (SqlConnection sqlConn = new SqlConnection(strConn)) {
            using (SqlCommand sqlCmd = new SqlCommand()) {
                sqlCmd.CommandText = "SELECT * FROM SubjectDetails";
                sqlCmd.Connection = sqlConn;
                sqlConn.Open();
                SqlDataReader objDataReader = sqlCmd.ExecuteReader();
                rptCustomRepeater.DataSource = objDataReader;
                rptCustomRepeater.DataBind();
                sqlConn.Close();
            }
        }
    } catch { }
}

Binding Repeater Control in Asp .Net Vb.net – [.vb]

Add the following namespace that is required to connect with SQL server:

Imports System.Data.SqlClient

After that add the following code to .aspx.vb page:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not IsPostBack Then
        BindData()
    End If
End Sub

'bind subject details to repeater control
Private Sub BindData()
    Try
        'specify your connection string here..
        Dim con As String = "Data Source=datasource;Integrated Security=true;Initial Catalog=yourDB"
        Using sqlConn As New SqlConnection(con)
            Using sqlCmd As New SqlCommand()
                sqlCmd.CommandText = "SELECT * FROM SubjectDetails"
                sqlCmd.Connection = sqlConn
                sqlConn.Open()
                Dim objDataReader As SqlDataReader = sqlCmd.ExecuteReader()
                rptCustomRepeater.DataSource = objDataReader
                rptCustomRepeater.DataBind()
                sqlConn.Close()
            End Using
        End Using
    Catch
    End Try
End Sub

Custom Repeater Control With DataPager in Asp.net Example

You may also like, how to add custom data pager to asp.net repeater control example here.

Example Result

binding repeater control in asp .net
How to Bind Asp.net Repeater Control Example using C# or Vb.net?

Download Example

Git Repo

git clone https://github.com/immayankmodi/bind-repeater-control-asp-net-c-vb.git

Previous articleAsp.net Repeater Control With DataPager Example
Next articleWebsite Hosting via Smarterasp.net – 50% Off Coupon Code
Hi there, I am Mayank, the man behind Technical Mack. I started AspnetO with a motive to educate people on various programming languages ranging from beginners to expert level. Through this blog, I aim to provide more insightful content.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

seventeen + nine =