Click here to Skip to main content
15,867,860 members
Articles / Web Development / HTML
Article

Graphical ASP.NET Controls

Rate me:
Please Sign up or sign in to vote.
4.64/5 (57 votes)
11 Jul 2005Ms-PL4 min read 282.2K   7K   144   65
Graphical radio button and check box ASP.NET controls.

Graphical controls

Introduction

It is possible to create very nice web pages and you can even change the look of some form controls using CSS (for example, it is possible to change the look of textboxes and buttons), but there is no way to change the look of checkboxes and radio buttons. This article describes two custom controls with the same functionality as standard ASP.NET CheckBox and RadioButton and with properties that allow you to change the design of the controls using images. Another way of how to use controls like these is when you want to place an icon with checkbox functionality on your web page; for example, I wanted to mark some data as hidden, so I used checkbox control with lock and stricken lock icons.

These two controls are very simple to use, because all functionality is same as functionality of the standard controls, so if you want to replace the standard controls with these graphical controls, you can do it with Search/Replace tool. These controls use JavaScript, but it was tested with latest versions of IE, Opera and Mozilla (FireFox), so it should display correctly to majority of Internet users.

Another useful feature of the check box control is, that it supports three state. This can be very useful, because the standard HTML checkbox doesn't support a third state, so there is no way to create three state checkboxes in ASP.NET. If you want to allow a third state set ThreeState property to true. You will also need one or two (if you want to have a different image for active state) images. To get the state of a three state check box, you can use the CheckState property, which returns Checked, Unchecked or Indeterminate.

Background

Graphical checkbox control generates img tag, that is followed by span and hidden input control. The state of a control is saved in a hidden input control. When user clicks on image (or on text associated to checkbox), control changes its state (value of hidden control) and changes src attribute of the image element. (You can also set image that is displayed when user moves mouse over checkbox - this is done using onmouseover and onmouseout events.)

Graphical radio button is very similar to checkbox, but there is one complication: User should be able to check only one button in every group. Only solution to this problem is to use a bit more JavaScript. One JS function called radio{GroupName}Sel is generated for each group of controls (this is done using IsStartupScriptRegistered and RegisterStartupScript methods, so it is very simple to check if any control of the same group generated this function before). Each control in the group calls this function when it is clicked, so this function can uncheck the previously checked radio button.

Controls needs to implement interface IPostBackDataHandler to be able to read data after postback. This interface contains two methods. LoadPostData is called by ASP.NET after postback, and it processes postback data (reads data from POST collection and compares new value with the one stored in ViewState) and optionally calls second method (RaisePostDataChangedEvent) that raises the CheckedChanged event.

Using this control

Designer

Toolbox

Design time

Both controls support design-time, so you can simply add them to your page like any other ASP.NET controls. First, you'll need to add controls to the toolbox. To do this, right click on toolbox and select "Add/Remove Items". In "Customize Toolbox" dialog, click on Browse button and select EeekSoft.Web.Controls.dll. After clicking OK, you should be able to see two new controls in your toolbox.

You can modify properties in Properties window. Most important are properties that define location of images used by the control. After you enter image URL (CheckedImg or UncheckedImg), control will automatically update its look.

Code

Following code demonstrates how to access control properties from code:

ASP.NET
<!-- Demo1.aspx -->
<%@ Register TagPrefix="cc1" Namespace="EeekSoft.Web.Controls"
  Assembly="EeekSoft.Web.Controls" %>
  
<cc1:graphicalcheckbox id="check1" runat="server" 
  CheckedOverImg="checked-over.gif" UncheckedOverImg="unchecked-over.gif" 
  UncheckedImg="unchecked.gif" CheckedImg="checked.gif" Checked="False" 
  Text="First checkbox"></cc1:graphicalcheckbox>
<cc1:graphicalcheckbox id="check1" runat="server" 
  CheckedOverImg="checked-over.gif" UncheckedOverImg="unchecked-over.gif" 
  UncheckedImg="unchecked.gif" CheckedImg="checked.gif" Checked="False" 
  Text="First checkbox"></cc1:graphicalcheckbox>
    
<asp:label id="lblInfo" runat="server" ></asp:label>
<asp:button id="btnPostback" runat="server" Text="Do postback!"></asp:button>
C#
// Display what checkboxes are checked
private void btnPostback_Click(object sender, System.EventArgs e)
{
  if (check1.Checked&&check2.Checked)
    lblInfo.Text="Both checkboxes are checked!";
  else if (check1.Checked)
    lblInfo.Text="First checkbox is checked!";
  else if (check2.Checked)
    lblInfo.Text="Second checkbox is checked!";
  else 
    lblInfo.Text="No checkbox is checked!";
}

ThreeState chcekbox

As I described above, checkbox supports third state, so you can use it when you want to allow users to choose from three states (third state usually indicates that value can't be distinctly determined). Following code shows how to create a three state check box and how to get its state from code:

ASP.NET
<!-- Demo2.aspx -->
<%@ Register TagPrefix="cc1" Namespace="EeekSoft.Web.Controls"
  Assembly="EeekSoft.Web.Controls" %>
  
<cc1:graphicalcheckbox id="threeCheck" runat="server"
  ThreeState="True" CheckState="Indeterminate" 
  IndeterminateOverImg="indet-over.gif" IndeterminateImg="indet.gif" 
  CheckedOverImg="checked-over.gif" UncheckedOverImg="unchecked-over.gif" 
  UncheckedImg="unchecked.gif" CheckedImg="checked.gif" 
  Text="Three state checkbox"></cc1:graphicalcheckbox>
    
<asp:label id="lblInfo" runat="server" ></asp:label>
<asp:button id="btnPostback" runat="server" Text="Do postback!"></asp:button>
C#
// Display value of three state checkbox
private void btnPostback_Click(object sender, System.EventArgs e)
{
  switch(threeCheck.CheckState)
  {
    case CheckState.Checked:
      lblInfo.Text="Checkbox is checked!"; break;
    case CheckState.Unchecked:
      lblInfo.Text="Checkbox is unchecked!"; break;
    case CheckState.Indeterminated:
      lblInfo.Text="Value is set to indeterminated!"; break;
  }
}

Setting graphical look globally

Setting location of images for each state of control is a bit exhausting if you have more than one control on page, so I created an object called GraphicalControlsSettings that allows you to set location of images only once in the whole application. This object stores location of all images (both for checkbox and radio button) and is stored in the application state (Application property). After you save settings for controls using this object, all controls with property LoadGlobalSettings set to true will automatically load these settings, so you don't have to set location of particular images.

Following example shows how to use this object (to learn more, see design.aspx.cs file in demo application):

ASP.NET
<!-- Demo3.aspx -->
<%@ Register TagPrefix="cc1" Namespace="EeekSoft.Web.Controls"
  Assembly="EeekSoft.Web.Controls" %>
  
<cc1:graphicalcheckbox LoadGlobalSettings="true" id="check1" 
  runat="server" Text="Checkbox 1"></cc1:graphicalcheckbox>
<cc1:graphicalcheckbox LoadGlobalSettings="true" id="check2" 
  runat="server" Text="Checkbox 2"></cc1:graphicalcheckbox>
C#
// Demo3.aspx.cs
using EeekSoft.Web.Controls;

// Save checkbox settings in Page_Load or in Application_Start event handler
GraphicalControlsSettings grs=new GraphicalControlsSettings();
string skin="green/";

// Set images for chcekbox buttons
grs.CheckboxCheckedImg=skin+"check-checked.gif";
grs.CheckboxUncheckedImg=skin+"check.gif";
grs.CheckboxCheckedOverImg=skin+"check-checked-over.gif";
grs.CheckboxUncheckedOverImg=skin+"check-over.gif";

// Set images for radio buttons
grs.RadioCheckedImg=skin+"radio-checked.gif";
grs.RadioUncheckedImg=skin+"radio.gif";
grs.RadioCheckedOverImg=skin+"radio-checked-over.gif";
grs.RadioUncheckedOverImg=skin+"radio-over.gif";

// Save settings to Application
grs.Save();

History

  • 7/9/2005 - A few minor bugs fixed.

    (Thanks to Andreas Menges for his feedback.)

  • 10/12/2004 - Graphical check box now supports three state checkbox properties ThreeState, CheckState and images for third state added.

    (Thanks to bubák for this suggestion.)

  • 10/2/2004 - First version of article.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


Written By
Czech Republic Czech Republic
I live in Prague, the capital city of Czech republic (most of the time Smile | :) ). I've been very interested in functional programming recently and I have a passion for the new Microsoft F# language. I'm writing a book about Functional Programming in the Real World that shows the ideas using examples in C# 3.0 and F#.

I've been Microsoft MVP (for C#) since 2004 and I'm one of the most active members of the F# community. I'm a computer science student at Charles University of Prague. My hobbies include photography, fractals and of course many things related to computers (except fixing them). My favorite book writers are Terry Pratchett and Philip K Dick and I like paintings by M. C. Escher.

PS: My favorite codeproject icon is Sheep | [baah] .

Comments and Discussions

 
QuestionAdd Controls at runtime (CodeBehind) ? Pin
Happy Hippo12-Dec-05 2:00
Happy Hippo12-Dec-05 2:00 
AnswerRe: Add Controls at runtime (CodeBehind) ? Pin
Godwin Lang12-Jan-06 3:05
Godwin Lang12-Jan-06 3:05 
GeneralRe: Add Controls at runtime (CodeBehind) ? Pin
Happy Hippo17-Jan-06 9:01
Happy Hippo17-Jan-06 9:01 
GeneralProblems Pin
dzonson2-Dec-05 15:09
dzonson2-Dec-05 15:09 
GeneralFeedback Pin
Skoder18-Oct-05 6:21
Skoder18-Oct-05 6:21 
QuestionRe: Feedback Pin
wise99026-Oct-05 2:38
wise99026-Oct-05 2:38 
GeneralCan't tab through!!! Pin
Vitaly Tomilov8-Sep-05 0:32
Vitaly Tomilov8-Sep-05 0:32 
GeneralRe: Can't tab through!!! Pin
Tomas Petricek10-Sep-05 1:54
Tomas Petricek10-Sep-05 1:54 
GeneralRe: Can't tab through!!! [modified] Pin
noice30-Jun-06 2:59
noice30-Jun-06 2:59 
GeneralWeb Usability Pin
Anonymous19-Jul-05 22:56
Anonymous19-Jul-05 22:56 
GeneralI love it Pin
Eng. Jalal4-Jun-05 0:29
Eng. Jalal4-Jun-05 0:29 
GeneralRadio Buttons / Group / Datagrid Pin
Anonymous21-Apr-05 20:27
Anonymous21-Apr-05 20:27 
GeneralTab Focus Pin
Jay Dubal10-Jan-05 1:29
Jay Dubal10-Jan-05 1:29 
GeneralControl doesn't work in ascx Pin
lulos3-Jan-05 5:43
lulos3-Jan-05 5:43 
GeneralViewstate problem in Databound controls (Datagrid, repeater..) Pin
dusty davidson15-Dec-04 9:30
dusty davidson15-Dec-04 9:30 
GeneralRe: Viewstate problem in Databound controls (Datagrid, repeater..) Pin
Jay Dubal23-Dec-04 0:47
Jay Dubal23-Dec-04 0:47 
GeneralRe: Viewstate problem in Databound controls (Datagrid, repeater..) Pin
fulgeras4-Jan-05 23:55
fulgeras4-Jan-05 23:55 
GeneralRe: Viewstate problem in Databound controls (Datagrid, repeater..) Pin
wise99027-Oct-05 1:59
wise99027-Oct-05 1:59 
GeneralRe: Viewstate problem in Databound controls (Datagrid, repeater..) Pin
danesparza1-Feb-05 7:03
danesparza1-Feb-05 7:03 
GeneralRe: Viewstate problem in Databound controls (Datagrid, repeater..) Pin
Eng. Jalal9-Jun-05 2:15
Eng. Jalal9-Jun-05 2:15 
GeneralRe: Viewstate problem in Databound controls (Datagrid, repeater..) Pin
Eng. Jalal9-Jun-05 2:16
Eng. Jalal9-Jun-05 2:16 
GeneralRe: Viewstate problem in Databound controls (Datagrid, repeater..) Pin
kmccalley16-Nov-06 6:24
kmccalley16-Nov-06 6:24 
QuestionDoesn't anyone else here think that it is SLOW? Pin
Anonymous3-Dec-04 12:03
Anonymous3-Dec-04 12:03 
AnswerRe: Doesn't anyone else here think that it is SLOW? Pin
jsharsky4-May-05 13:22
jsharsky4-May-05 13:22 
GeneralOnline sample doesn't work !! Pin
Tomas Petricek15-Nov-04 0:52
Tomas Petricek15-Nov-04 0:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.