Server Intellect
 
Home   Asp.Net Tutorials   What's New   Newsletter   More Resources
 
 
  Categories
Advanced Technologies
AJAX
Internet Browsers
Controls
Database
Email
Error Handling
File
Graphics
Website Navigation
Network
Performance
User Interface and Themes
Validation
Visual Web Developer
Web Services
XML
Suggest Tutorial


Navigator: Home - Tutorials - Controls - Using Reuseable Elements with User Controls in C#
Using Reuseable Elements with User Controls in C#


ASP.NET Controls Tutorial

This tutorial will allow you to move data from one list box to another, as well as remove them again - either one by one, or altogether. Also, you can choose to allow duplicates or not, as well as output the chosen selection. C# version.

We need a Web User Control, which will have two list boxes, and four buttons: Add All, Add One, Delete One (from Selection List), and also Delete One (from Source List).

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="ListPicker.ascx.cs" Inherits="ListPicker" %>
<table>
<tr>
<td style="width: 100px">
Available<br /> <asp:ListBox ID="SourceList" runat="server" Height="200px" Width="200px"></asp:ListBox></td>
<td style="width: 100px">
<asp:Button ID="AddAll" runat="server" OnClick="AddAll_Click" Text=">>" />
<asp:Button ID="AddOne" runat="server" OnClick="AddOne_Click" Text=" > " />
<asp:Button ID="Remove" runat="server" OnClick="Remove_Click" Text=" X " /><br />
<br />
<br />
<asp:Button ID="RemoveSource" runat="server" OnClick="RemoveSource_Click" Text="< X" /></td>
<td style="width: 100px">
Selected<br />
<asp:ListBox ID="TargetList" runat="server" Height="200px" Width="200px"></asp:ListBox></td>
</tr>
</table>

Try Server Intellect for Windows Server Hosting. Quality and Quantity!

Now we add code to the controls:

protected void AddAll_Click(object sender, EventArgs e)
{
TargetList.SelectedIndex = -1;
foreach(ListItem li in SourceList.Items)
{
AddItem(li);
}
}
protected void AddOne_Click(object sender, EventArgs e)
{
if(SourceList.SelectedIndex >= 0)
{
AddItem(SourceList.SelectedItem);
}
}
protected void Remove_Click(object sender, EventArgs e)
{
if(TargetList.SelectedIndex >= 0)
{
TargetList.Items.RemoveAt(TargetList.SelectedIndex);
TargetList.SelectedIndex = -1;
}
}
protected void AddItem(ListItem li)
{
TargetList.SelectedIndex = -1;
if(this.AllowDuplicates == true)
{
TargetList.Items.Add(li);
}
else
{
if(TargetList.Items.FindByText(li.Text) == null)
{
TargetList.Items.Add(li);
}
}
}
public ListItemCollection SelectedItems
{
get { return TargetList.Items; }
}
public Boolean AllowDuplicates
{
get
{
return (Boolean)ViewState["allowDuplicates"];
}
set
{
ViewState["allowDuplicates"] = value;
}
}
public void AddSourceItem(String sourceItem)
{
SourceList.Items.Add(sourceItem);
}
public void ClearAll()
{
SourceList.Items.Clear();
TargetList.Items.Clear();
}
protected void RemoveSource_Click(object sender, EventArgs e)
{
if (SourceList.SelectedIndex >= 0)
{
SourceList.Items.RemoveAt(SourceList.SelectedIndex);
SourceList.SelectedIndex = -1;
}
}

We used over 10 web hosting companies before we found Server Intellect. Their dedicated servers and add-ons were setup swiftly, in less than 24 hours. We were able to confirm our order over the phone. They respond to our inquiries within an hour. Server Intellect's customer support and assistance are the best we've ever experienced.

Add the ListPicker Control onto an ASPX page and add a text box and button to allow user to add to the source list, a button to allow population of source list with list of files in the directory, a button to clear both list boxes, an Allow Duplicates check box, and a button & label to output the current selection:

<form id="form1" runat="server">
<div>
<uc1:ListPicker id="ListPicker1" Runat="server" AllowDuplicates="true" />
<asp:TextBox ID="NewItem" runat="server">
<asp:Button ID="AddItem" runat="server" OnClick="AddItem_Click" Text="Add Item" /><br />
<asp:Button ID="LoadFiles" runat="server" OnClick="LoadFiles_Click" Text="File List" /> <asp:Button
ID="ClearSelection" runat="server" OnClick="ClearSelection_Click" Text="Clear All" /><br />
<asp:CheckBox ID="AllowDuplicates" runat="server" AutoPostBack="True" Checked="True"
OnCheckedChanged="AllowDuplicates_CheckedChanged" Text="Allow Duplicates" /><br />
<asp:Button ID="ShowSelection" runat="server" OnClick="ShowSelection_Click" Text="Show Selection" />
<asp:Label ID="Selection" runat="server"></asp:Label>
</div>
</form>

We chose Server Intellect for its dedicated servers, for our web hosting. They have managed to handle virtually everything for us, from start to finish. And their customer service is stellar.

ASPX code for these buttons should look something like this:

protected void AllowDuplicates_CheckedChanged(object sender, EventArgs e)
{
ListPicker1.AllowDuplicates = AllowDuplicates.Checked;
}
protected void AddItem_Click(object sender, EventArgs e)
{
ListPicker1.AddSourceItem(Server.HtmlEncode(NewItem.Text));
}
protected void LoadFiles_Click(object sender, EventArgs e)
{
String path = Server.MapPath(Request.ApplicationPath);
String[] files = System.IO.Directory.GetFiles(path);
foreach(String filename in files)
{
ListPicker1.AddSourceItem(filename);
}
}
protected void ShowSelection_Click(object sender, EventArgs e)
{
String selectedItemsString = "";
foreach(ListItem lItem in ListPicker1.SelectedItems)
{
selectedItemsString += "<br>" + lItem.Text;
}
Selection.Text = selectedItemsString;
}
protected void ClearSelection_Click(object sender, EventArgs e)
{
ListPicker1.ClearAll();
Selection.Text = "";
}

Download the Full Working Version of this Project written with Visual Studio.NET C# 2005 Here!


Looking for the VB.NET 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!







 
  Developer Resources







Server Intellect Rocks