Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • ASP.NET : Binding ArrayList to Repeater in C#

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.57k
    Comment on it

    Binding ArrayList to Repeater in C#

    The repeater is a web server data-bound control which repeats a list of items that are binded to the control. Repeater.DataSource is a property that provides data to Repeater and this data is binded to repeater through Repeater.DataBind() method.

     

    ArrayList class implements Ilist interface using array whose size can be increased dynamically as per requirement. Object type is the datatype of ArrayList which is capable of storing items of different data type.

     

    Binding ArrayList to Repeater:

     

    The items in ArrayList can be listed through Repeater control without using loops by initializing DataSource property of Repeater to ArrayList variable and binding the items of ArrayList variable through DataBind method.

     

    Following is an example to demonstrate ArrayList Binding to Repeater.

     

    ArrayListBinding.aspx

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ArrayListBinding.aspx.cs" Inherits="RegistrationForm.ArrayListBinding1" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="formListRepeater" runat="server">
             <div>
                <asp:Repeater ID="repeatArrayList" runat="server">
                    <ItemTemplate>
                        <table border="1">
                            <tr>
                                <td>
                                    <asp:label id="lblItem" text=<%# Container.DataItem %>  runat="server" />
                               </td> 
                            </tr>
                        </table>
                    </ItemTemplate>
                </asp:Repeater>
            </div>
        </form>
    </body>
    </html>

     

    ArrayListBinding.aspx.cs

    
    using System;
    using System.Collections;
    using System.Web;
    
    
    namespace RegistrationForm
    {
        public partial class ArrayListBinding1 : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                BindArrayList();
            }
            protected void BindArrayList()
            {
                ArrayList list = new ArrayList();
                list.Add("Uttarakhand");
                list.Add("Bihar");
                list.Add("Assam");
                list.Add("Delhi");
                list.Add("West Bengal");
                repeatArrayList.DataSource = list;
                repeatArrayList.DataBind();
            }
        }
    }
    

    Output:

    Explanation of above Program:

     

    In ArrayListBinding.aspx.cs file an ArrayList variable “list” is defined which contains a list of strings. The “list” is assigned to DataSource property of repeater and items of “list” are binded to repeater through DataBind method.

     

    In ArrayListBinding.aspx file a repeater is defined with a template called Itemtemplate whose content will be repeated for every item present in DataSource. The item from the iteration process of Repeater control is called by<%# Container.DataItem %>. Finally, the “list” can be displayed through Itemtemplate.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: