

Navigator : Home > Tutorials > Controls Tutorials > ...
Using Reuseable Elements with User Controls in C#
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> |
We migrated our web sites to Server Intellect over one weekend and the setup was so smooth that we were up and running right away. They assisted us with everything we needed to do for all of our applications. With Server Intellect's help, we were able to avoid any headaches!
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; } } |
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> |
ASPX code for these buttons should look something like this:
Yes, it is possible to find a good web host. Sometimes it takes a while. After trying several, we went with Server Intellect and have been very happy. They are the most professional, customer service friendly and technically knowledgeable host we've found so far.
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 = ""; } |
Looking for the VB.NET 2005 Version? Click Here!
Looking for more ASP.NET Tutorials? Click Here!