Prerequisites
Summary
Topics covered in this video;
-Differences between Lists and Arrays
-Dictionary example
-Keys
-Value
-Key/Value Pairs
-Strong Types
Video
Reference Materials
Information about List is available at http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx
ListExample.aspx Interface Code
<h1>List Example</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 List" 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 List:<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=""/>
ListExample.aspx.cs Code
public static List<string> list = new List<string>();
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];
}
DictionaryExample.aspx Code
<h1>Dictionary Example</h1>
Enter key-values pairs separated by a comma, one key-value per line:<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 Dictionary" 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 Dictionary<br/> (key-value pair):<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=""/>
DictionaryExample.aspx.cs Code
public static Dictionary<string,string> dictionary = new Dictionary<string, string>();
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)
{
string[] s = tbAddToList.Text.Split(',');
dictionary.Add(s[0],s[1]);
BindList();
}
protected void FillList()
{
string[] s1 = tbList.Text.Split('\n');
dictionary.Clear();
foreach (string s2 in s1)
{
string[] s3 = s2.Split(',');
dictionary.Add(s3[0],s3[1]);
}
}
protected void BindList()
{
lbArrayList.DataSource = dictionary;
lbArrayList.DataBind();
gvList.DataSource = dictionary;
gvList.DataBind();
}
protected void btnShow_Click(object sender, EventArgs e)
{
string s = tbIndex.Text;
lblIndex.Text = dictionary[s];
}