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

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 980
    Comment on it

    Binding ArrayList to DataList

    DataList is a web server data bound control which display data items in repeating list. DataList provides an optional facility of selecting and editing data items. In DataList,Templates defines the content and layout of the list items to be displayed. A DataList should at least define an ItemTemplate and can contain optional templates for customizing appearance of list.

     

    DataList.DataSource is a property that provides data to DataList and this data is binded to datalist through DataList.DataBind() method.

     

    ArrayList is a class which implements Ilist interface. ArrayList can define an array object which is capable of storing items of different types, the size of this array object can be changed as per requirement.

     

    Binding ArrayList to DataList:

     

    DataList control can be used to list items in ArrayList object without using loops. This can be done by initializing DataSource property of DataList to ArrayList variable and items of ArrayList variable are binded through DataBind method.

     

    Following is an example to demonstrate ArrayList Binding to DataList.

     

    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="formDataList" runat="server">
             <div>
                 <asp:datalist id="dlList" runat="server" cellpadding="3" width="50%" >
    
                  <itemtemplate>
                    <%# Container.DataItem %>
                  </itemtemplate>
    
                 </asp:datalist>
            </div>
        </form>
    </body>
    </html>
    

    ArrayListBinding.aspx.cs

    using System;
    using System.Collections;
    
    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");
                dlList.DataSource = list;
                dlList.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 datalist and items of “list” are binded to datalist through DataBind property.

     

    In ArrayListBinding.aspx file a datalist is defined with template called Itemtemplate whose content will be repeated for every item present in DataSource. The item from the iteration process of datalist 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: