Lecture – ArrayList Object

Prerequisites

C# Programming knowledge

Summary

Topics covered in this video;

-Collections
-Adding Array Elements
-ListBox/GridView
-List Index relative to Array
-FillList/BindList
-Object List
-Split method
-String

Video 

Reference Materials

ArrayListExample.aspx Page Content

 <h1>ArrayList</h1>
    Enter values separated by a commas:<br/>
    <div>
    <asp:TextBox ID="tbList" runat="server" TextMode="MultiLine" Height="80" Width="200" CssClass="vertical-align-top" ></asp:TextBox>
    <asp:Button ID="btnCreateArrayList" runat="server" Text="Create ArrayList" OnClick="btnCreateArrayList_Click" CssClass="vertical-align-top" />
    </div>
        <br/>
    <h1>ArrayList Data Bound to:</h1> <br/>
    <table style="float:left; margin:0% 0% 0% 10%">
        <tr>
            <td><h1>ListBox</h1></td>
            <td><h1>GridView</h1></td>
        </tr>
        <tr>
    <td> <asp:ListBox ID="lbArrayList" runat="server"></asp:ListBox></td>
    <td><asp:GridView ID="gvList" runat="server"></asp:GridView></td>
    </tr>
    </table>
    <br/><br/><br/><br/>
    Item to Add to ArrayList:<br/> <asp:TextBox ID="tbAddToList" runat="server" Width="80"></asp:TextBox>
    <asp:Button ID="btnAddToList" runat="server" Text="Add to List" OnClick="btnAddToList_Click" />
    <br/>Enter List Index<asp:TextBox ID="tbIndex" runat="server" TextMode="Number" Width="40" /><asp:Button runat="server" Text="Show" ID="btnShow" OnClick="btnShow_Click" />  <asp:Label ID="lblIndex" runat="server" Text=""/>

ArrayListExample.aspx.cs

using System;
using System.Web.UI;
using System.Collections;
namespace DatabaseSample1.Pages
{
    public partial class ArrayListExample : Page
    {
        public static ArrayList List = new ArrayList();
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        protected void btnCreateArrayList_Click(object sender, EventArgs e)
        {
            FillList();
            BindList();
        }
        protected void btnAddToList_Click(object sender, EventArgs e)
        {
            List.Add(tbAddToList.Text);
            BindList();
        }
        protected void FillList()
        {
            string[] s1 = tbList.Text.Split(',');
            List.Clear();
            foreach (string s2 in s1)
            {
                List.Add(s2);
            }
        }
        protected void BindList()
        {
            List.Sort();
            lbArrayList.DataSource = List;
            lbArrayList.DataBind();
            gvList.DataSource = List;
            gvList.DataBind();
        }
        protected void btnShow_Click(object sender, EventArgs e)
        {
            int i = Convert.ToInt32(tbIndex.Text);
            lblIndex.Text = List[i].ToString();
        }
    }
} 

Additional Information

COP 4834 Lectures Page