Click here to Skip to main content
6,595,444 members and growing! (19,936 online)
Email Password   helpLost your password?
Web Development » Custom Controls » General     Intermediate

Graphical ASP.NET Controls

By Tomas Petricek

Graphical radio button and check box ASP.NET controls.
C#, HTML, Windows, .NET 1.1, ASP.NET, GDI+, VS.NET2003, Dev
Posted:7 Oct 2004
Updated:11 Jul 2005
Views:167,324
Bookmarked:131 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
Prize winner in Competition "ASP.NET Sep 2004"
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
49 votes for this article.
Popularity: 7.66 Rating: 4.53 out of 5
2 votes, 4.1%
1
1 vote, 2.0%
2
1 vote, 2.0%
3
9 votes, 18.4%
4
36 votes, 73.5%
5

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:

<!-- 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>
// 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:

<!-- 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>
// 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):

<!-- 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>
// 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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Tomas Petricek


Member
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.
Location: Czech Republic Czech Republic

Other popular Custom Controls articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 60 (Total in Forum: 60) (Refresh)FirstPrevNext
GeneralIssue PinmemberSheetal19827:54 10 Jul '09  
GeneralLicense? PinmemberAlexander Bender0:00 10 Jul '09  
QuestionError. Pinmembermedasatheesh7:28 27 May '09  
GeneralHey any one knows how to use this control in gridview, oncheckedchanged event is not raising PinmemberMember 378735211:30 31 Oct '08  
GeneralError when adding control in code behind Pinmembercoogee4:26 4 Jun '08  
GeneralRe: Error when adding control in code behind Pinmemberernest_elias8:12 2 Sep '08  
GeneralRe: Error when adding control in code behind Pinmemberernest_elias21:47 2 Sep '08  
General.Net 2.0 Master pages Pinmembergraham.lock4:17 26 Nov '07  
GeneralRe: .Net 2.0 Master pages Pinmembercoogee7:22 4 Jun '08  
QuestionJavascript error when using with Ajax updatepanel PinmemberFranzS6686:21 19 Oct '07  
GeneralProblem in designmode when globalsettings are used Pinmemberroecki2:01 16 Sep '07  
Generalvery nice work Pinmemberpeter gabris9:10 7 Sep '07  
Generaladding Graphical Radio button with Rich list PinmemberRajesh.N.V20:26 5 Nov '06  
QuestionPosition popup relative to a control on the page Pinmemberdjlandino3:28 15 Oct '06  
GeneralError about startIndex after importing dll Pinmemberjusmeig4:14 3 Aug '06  
GeneralRe: Error about startIndex after importing dll Pinmembermohd_osman8:11 9 Aug '06  
AnswerRe: Error about startIndex after importing dll Pinmemberncanen13:04 26 Oct '06  
AnswerRe: Error about startIndex after importing dll Pinmembervlad.pitaru2:39 8 Nov '06  
QuestionHow to inherit this control to Checkboxlist or radiobuttonlist PinmemberwonderJAM5:14 1 Jul '06  
Generaljavascript events Pinmemberjuantek19:30 14 Jan '06  
GeneralAdd Controls at runtime (CodeBehind) ? PinmemberHappy Hippo3:00 12 Dec '05  
GeneralRe: Add Controls at runtime (CodeBehind) ? PinmemberGoddyD4:05 12 Jan '06  
GeneralRe: Add Controls at runtime (CodeBehind) ? PinmemberHappy Hippo10:01 17 Jan '06  
GeneralProblems Pinmemberdzonson16:09 2 Dec '05  
GeneralFeedback PinmemberSkoder7:21 18 Oct '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 11 Jul 2005
Editor: Smitha Vijayan
Copyright 2004 by Tomas Petricek
Everything else Copyright © CodeProject, 1999-2009
Web09 | Advertise on the Code Project