CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Brendan Tompkins [MVP]

Blog First. Ask Questions Later.

A Simple Alphabet Selection Control

Recently, I decided that our user administration pages here at work for our public website was due for an overhaul.   The old version had become very painful to work with indeed, since depended on a categorized tree view of all of our registered companies and users on our public web site, it looked sorta like this:

Our membership is in the thousands now, and the tree view paradigm has completely become unusable with this amount of data. In fact the HTML page size alone for this page was nearly a megabyte!  Ack!   I think there’s some UI rule here about tree views and expected data size, but I had to find a better way.   After some experimentation, the new design allows the admin to group the company list by alphabet.  The new design looks like this, and works much better. Total page size is under 150K now.  Not great, I know, but a vast improvement.  And for the small user population that uses this, it'll do fine for a while ;) .

Anyhow, the alphabet selection control I thought may be something that will see some re-use, so I’ve implemented it as a simple custom control that can be dragged onto a web form.  It supports one event, ItemSelected, and has one property SelectedItem.  The idea is very simple, and chances are this has been done over and over again by many, but I thought I’d share this snippet of code here:

Good Luck!

-Brendan

using System;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.ComponentModel;

 

namespace YourNamespace.Web.UI.WebControls

{

 

    [DefaultEvent("ItemSelected")]

    public class AlphabetList : System.Web.UI.WebControls.WebControl

    {       

        public event System.EventHandler ItemSelected;

 

        protected override void OnInit(EventArgs e)

        {

            for(char i='A'; i < 'Z'; i++)

            {

                LinkButton lb = new LinkButton();

                string alphaChar = i.ToString();

                lb.Text = alphaChar;

                lb.CommandArgument = alphaChar;

                lb.Click += new EventHandler(lb_Click);

                lb.ID = alphaChar;

                this.Controls.Add(lb);

                this.Controls.Add(new LiteralControl("&nbsp;"));

            }

 

            LinkButton lbAll = new LinkButton();

            lbAll.Text = "ALL";

            lbAll.CommandArgument = "ALL";

            lbAll.Click += new EventHandler(lb_Click);

            lbAll.ID = "ALL";

            this.Controls.Add(lbAll);

 

            base.OnInit(e);

 

        }

 

        /// <summary>

        /// Gets the selected item.

        /// </summary>

        /// <value>The selected item.</value>

        public string SelectedItem

        {

            get

            {

                return this.ViewState["selected"] as string;   

            }

        }

 

        /// <summary>

        /// Handles the Click event of the lb control.

        /// </summary>

        /// <param name="sender">The source of the event.</param>

        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>

        private void lb_Click(object sender, EventArgs e)

        {

            if(this.SelectedItem != null)

            {

                (this.FindControl(this.SelectedItem) as LinkButton).Enabled = true;

            }

 

            this.ViewState["selected"] = (sender as LinkButton).CommandArgument;

            (sender as LinkButton).Enabled = false;

 

            if(this.ItemSelected != null)

                this.ItemSelected(this, new EventArgs());

        }

    }

}



Comments

BlackTigerX said:

perhaps you could use:

for (char i='A'; i<'Z'; i++)
# November 10, 2005 1:31 PM

Brendan Tompkins said:

BTX .. Ah yes. That does make more sense, huh? Fixed.

Thanks!
# November 10, 2005 1:52 PM

darrell said:

Nice.
# November 11, 2005 8:33 AM

balaramesh said:

ASPNET: Make sure that the class defined in this code file matches the 'inherits' attribute, and that it extends the correct base class (e.g. Page or UserControl).

# July 30, 2007 12:17 AM

Bart said:

I know it's an old article, but a few minutes of googling saved me a few hours of reinventing the wheel, so thanks a bunch!

# May 22, 2008 8:09 AM

David said:

Is it possiable to mak this into a toolbox dragable control?

# July 3, 2008 1:57 PM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add

About Brendan Tompkins

Brendan has been programming with .NET since the first public beta and is owner and operator of Port Technology Services, a consultancy company providing .NET application development services to the Maritime industry. In July, 2007, he was awarded the Microsoft MVP award for ASP.NET. He's also a proud co-founder of failed .COM startup Intrinsigo, and has had a hand in the failure of numerous other businesses. He currently runs CodeBetter.Com and Devlicio.us, and lives in Norfolk, Virgina with his wife Tiara and son Ian.

View Brendan's profile on LinkedIn

Check out Devlicio.us!

Our Sponsors

Free Tech Publications