Brendan Tompkins [MVP]

Sponsors

The Lounge

News

Advertisement

Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
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());

        }

    }

}


Posted 11-10-2005 12:19 PM by Brendan Tompkins

[Advertisement]

Comments

BlackTigerX wrote re: A Simple Alphabet Select List
on 11-10-2005 1:31 PM
perhaps you could use:

for (char i='A'; i<'Z'; i++)
Brendan Tompkins wrote re: A Simple Alphabet Select List
on 11-10-2005 1:52 PM
BTX .. Ah yes. That does make more sense, huh? Fixed.

Thanks!
Darrell Norton wrote re: A Simple Alphabet Selection Control
on 11-11-2005 8:33 AM
Nice.
balaramesh wrote re: A Simple Alphabet Selection Control
on 07-30-2007 12:17 AM

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).

Bart wrote re: A Simple Alphabet Selection Control
on 05-22-2008 8:09 AM

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!

David wrote re: A Simple Alphabet Selection Control
on 07-03-2008 1:57 PM

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

Add a Comment

(required)  
(optional)
(required)  
Remember Me?