Writing a Days of the Week Selector Control
page 1 of 2
Published: 13 Sep 2004
Unedited - Community Contributed
Abstract
In this article I'll describe the process I went through to create a control for choosing days of the week. The need driving this is the creation of an admin program that allows certain tasks to be scheduled for certain days of the week, a la Windows Scheduled Tasks.
by Steven Smith
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 20072/ 58

No Control Required

[ Download Code ]
My first stab at this, keeping things as simple as possible, was just to do it on a web form without the added complication of creating a custom control.  I'm storing the resulting selection in a database as in integer bitmask, where Sunday is 1 and Saturday is 64 (so values between 0000000 and 1111111 binary or 0 and 127 decimal are allowed).  To do this, all that's required is a simple CheckBoxList, with everything declared declaratively:

        <asp:CheckBoxList ID="CheckBoxList1" Runat="server">
            <asp:ListItem Value="1">Sunday</asp:ListItem>
            <asp:ListItem Value="2">Monday</asp:ListItem>
            <asp:ListItem Value="4">Tuesday</asp:ListItem>
            <asp:ListItem Value="8">Wednesday</asp:ListItem>
            <asp:ListItem Value="16">Thursday</asp:ListItem>
            <asp:ListItem Value="32">Friday</asp:ListItem>
            <asp:ListItem Value="64">Saturday</asp:ListItem>
        </asp:CheckBoxList>
        <asp:Button ID="Button1" Runat="server" Text="Button" OnClick="Button1_Click" />
        <br />
        <asp:Label ID="Label1" Runat="server"></asp:Label>

Combine that with a bit of code to extract the bitmask and display it:

    int DaysOfWeekBitMask = 0;


    void Button1_Click(object sender, EventArgs e)
    {
        Label1.Text = "";
        foreach (ListItem item in CheckBoxList1.Items)
        {
            if (item.Selected)
            {
                DaysOfWeekBitMask += Int32.Parse(item.Value);
                Label1.Text += item.Text + ", ";
            }
        }
        Label1.Text += "Bitmask: " + DaysOfWeekBitMask + " (" + 
System.Convert.ToString(DaysOfWeekBitMask, 2).PadLeft(7, '0') + ")";
    }

The result will look something like this:
Days of Week - No Custom Control

Simple.  However, I intend to use this functionality on several different pages, and potentially in different applications, so I want to make it a custom control so that I can just drag and drop it wherever I need it.  We'll look at how to do that next, with just a bare-bones simple example.

In a later article, I hope to take this a step further and make the control more flexible, so that a variety of listing options are supported (e.g. single vs. multi select using dropdown, ListBox, RadioButtonList, or CheckBoxList display modes).  For now, I'm simply extending the CheckBoxList, as you'll see on the next page.

 


View Entire Article

User Comments

Title: Improvement   
Name: JSx
Date: 2004-09-13 7:47:43 AM
Comment:
It is better to initialize ListItems in OnInit instead of OnLoad page processing phase. Adding days into the checkbox list in an OnInit will save some ViewState bytes, adding them in OnLoad results in the "ViewState garbage problem".

Istead of

protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
if (this.Items.Count == 0)
{
this.Items.Add(new ListItem("Sunday", "1"));
this.Items.Add(new ListItem("Monday", "2"));
this.Items.Add(new ListItem("Tuesday", "4"));
this.Items.Add(new ListItem("Wednesday", "8"));
this.Items.Add(new ListItem("Thursday", "16"));
this.Items.Add(new ListItem("Friday", "32"));
this.Items.Add(new ListItem("Saturday", "64"));
}
}

add all days in OnInit for smaller ViewState.






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-10-09 1:50:23 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search