Create Auto Generate Serial No or Number in Repeater Control Asp.Net C# VB.Net

To represent Data more often we use Repeater Control. And in certain times we need to generate auto serial number for Repeater rows due to reporting purposes. In Repeater control we could do the job simply. Here in this post I will describe “How to Create Auto Generate Serial Number for Asp.Net Repeater Control in C# and VB.Net“.

Objective/Outcome:
Create Auto Generate Serial No or Number in Repeater Control Asp.Net C# VB.Net

To Add a Serial Number in Repeater Control the HTML will be:

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
 <asp:Repeater ID="Repeater1" runat="server">
    <HeaderTemplate>
        <table border="1px" cellpadding="5" cellspacing="0" style="text-align:center;border-style:solid;border-collapse:collapse;">
        <tr style="background:CornflowerBlue;Color:White;">
            <th>SL No.</th>
            <th>Code</th>
            <th>Name</th>
            <th>Address</th>
            <th>Contact No</th>
        </tr>
        </HeaderTemplate>
        <ItemTemplate>
        <tr>
            <td><%#Container.ItemIndex+1 %></td>
            <td><%# Eval("Code")%></td>
            <td><%#Eval("Name")%></td>
            <td><%#Eval("Address")%></td>
            <td><%#Eval("Contact")%></td>
        </tr>
        </ItemTemplate>
        <AlternatingItemTemplate>
        <tr style="background:#99CCFF;">
            <td><%#Container.ItemIndex+1 %></td>
            <td><%# Eval("Code")%></td>
            <td><%#Eval("Name")%></td>
            <td><%#Eval("Address")%></td>
            <td><%#Eval("Contact")%></td>
        </tr>
        </AlternatingItemTemplate>
        <FooterTemplate>
        </table>
        </FooterTemplate>
    </asp:Repeater>
</asp:Content>

Now just Bind the Repeater Control with Data:
C# Code:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataTable dtSupplier = new DataTable("Supplier");
            dtSupplier.Columns.Add(new DataColumn("Code"));
            dtSupplier.Columns.Add(new DataColumn("Name"));
            dtSupplier.Columns.Add(new DataColumn("Address"));
            dtSupplier.Columns.Add(new DataColumn("Contact"));
            dtSupplier.Rows.Add("st0001", "S.R. Steel", "Uttara, Dhaka", "01711xxxxxx");
            dtSupplier.Rows.Add("ir0039", "Shadesh builders", "Rampura, Dhaka", "01711yyyyyy");
            dtSupplier.Rows.Add("cr0042", "Orchard confec.", "Shahabag, Dhaka", "01711zzzzzz");
            dtSupplier.Rows.Add("er0078", "Windblow", "Mirpur, Dhaka", "01711qqqqqq");
            dtSupplier.Rows.Add("bd0301", "Rahimkarim", "Badda, Dhaka", "01711oooooo");

            Repeater1.DataSource = dtSupplier;
            Repeater1.DataBind();
        }
    }

Note: Don’t forget to add “System.Data” namespace. For simplicity here DataTable is used. You can Bind other Data Sources as well.

VB.Net:

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim dtSupplier As DataTable = New DataTable("Supplier")
            dtSupplier.Columns.Add(New DataColumn("Code"))
            dtSupplier.Columns.Add(New DataColumn("Name"))
            dtSupplier.Columns.Add(New DataColumn("Address"))
            dtSupplier.Columns.Add(New DataColumn("Contact"))
            dtSupplier.Rows.Add("st0001", "S.R. Steel", "Uttara, Dhaka", "01711xxxxxx")
            dtSupplier.Rows.Add("ir0039", "Shadesh builders", "Rampura, Dhaka", "01711yyyyyy")
            dtSupplier.Rows.Add("cr0042", "Orchard confec.", "Shahabag, Dhaka", "01711zzzzzz")
            dtSupplier.Rows.Add("er0078", "Windblow", "Mirpur, Dhaka", "01711qqqqqq")
            dtSupplier.Rows.Add("bd0301", "Rahimkarim", "Badda, Dhaka", "01711oooooo")

            Repeater1.DataSource = dtSupplier
            Repeater1.DataBind()
        End If
    End Sub

Note: Don’t forget to add “System.Data” namespace. For simplicity here DataTable is used. You can Bind other Data Sources as well.

Hope now you can add Serial Number in Repeater Control Rows whatever data sources is.

Download Code Example C#        Download Code Example VB.Net

Leave a Reply

Your email address will not be published. Required fields are marked *

AlphaOmega Captcha Classica  –  Enter Security Code