codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || RAM 
Search Site:



Home » ASP » Article

ASP.NET : The checkbox and checkboxlist control

Article by: David Sussman, et al (7/8/2002)
Bookmark us now! Add to Favourites
Email a friend! Tell a friend
Summary: Checkboxes are similar to radio buttons, and in HTML, they were used to allow multiple choices from a group of buttons.
Viewed: 433428 times Rating (79 votes): 
 3.2 out of 5
  Rate this Article   Read Comments   Post Comments


Previous Page  Page 1 Page 2 Page 3 Page 4 Page 5 Page 6 Page 7 Page 8 Page 9 Page 10 Page 11 Page 13 Next Page  

The <asp:checkbox> and <asp:checkboxlist> Controls

Checkboxes are similar to radio buttons, and in HTML, they were used to allow multiple choices from a group of buttons. With the <asp:checkboxlist> control it is possible to create them in groups, but unlike radio buttons, it isn't possible to restrict the user to selecting just one possible answer from a group of checkboxes: they can select as many as they like. The other fundamental difference between a checkbox and a radio button is that once you have selected a checkbox you are able to deselect it by clicking on it again.

 

We're not going to spend too long examining them, as most of the same principles that we followed in the <asp:radiobutton> and <asp:radiobuttonlist> examples apply.

 

A typical <asp:checkbox> looks like this:

 

<asp:checkbox id="check1" runat="server" />

 

If we want to use an array of checkboxes, we can contain them inside an <asp:checkboxlist> control. We need to set an id attribute for the <asp:checkboxlist> control itself, and create a <asp:listitem> control for each option inside the control:

 

<asp:checkboxlist id="check1" runat="server">

  <asp:listitem id="option1" runat="server" value="Madrid" />

  <asp:listitem id="option2" runat="server" value="Oslo" />

  <asp:listitem id="option3" runat="server" value="Lisbon" />

</asp:checkboxlist>

 

Checkboxes are typically used when you have single yes/no answers, or you wish the user to be able to make a multiple set of selections, and be able to deselect them as well.

 

In our next exercise, we're going to tweak our previous example, so that it uses our established holiday code to allow the user to select more than one option for a particular destination.


 

Try It Out – Using the <asp:checkbox> Control

1.       Open up the radiopage.aspx and amend the code highlighted in gray, as follows:

<script runat="server" language="C#">

  void Page_Load()

  {

    string msg = "You have selected the following items:<br />";

 

    if (check1.Items[0].Selected) {

    msg = msg + check1.Items[0].Text + "<br />";

    }

    if (check1.Items[1].Selected) {

    msg = msg + check1.Items[1].Text + "<br />";

    }

    if (check1.Items[2].Selected) {

    msg = msg + check1.Items[2].Text + "<br />";

    }

   

    Message.Text = msg;

  }   

</script>

<html>

<head>

  <title>Check Box List Example</title>

</head>

<body>

  <asp:label id="Message" runat="server" />

  <br /><br />

  Which city do you wish to look at hotels for?

  <br /><br />

  <form runat="server">

    <asp:checkboxlist id="check1" runat="server">

      <asp:listitem id="option1" runat="server" value="Madrid" />

      <asp:listitem id="option2" runat="server" value="Oslo" />

      <asp:listitem id="option3" runat="server" value="Lisbon" />

    </asp:checkboxlist>

    <br /><br />

    <input type="Submit">

  </form>

</body>

</html>

 

2.       Save this as checkpage.aspx.


 

3.       Open checkpage.aspx in your browser, and select more than one option:

 

 

4.       Then click on Submit Query:

 


 

How It Works

Very little has changed with our control – all we've done is change the HTML control to an <asp:checkboxlist>, and then change the name of the control to reflect this:

 

<asp:checkboxlist id="check1" runat="server">

  <asp:listitem id="option1" runat="server" value="Madrid" />

  <asp:listitem id="option2" runat="server" value="Oslo" />

  <asp:listitem id="option3" runat="server" value="Lisbon" />

</asp:checkboxlist>

 

Our ASP.NET code is the same as that we used for the listbox2.aspx example, earlier on, except that here it refers to a checkbox rather than a listbox:

 

    string msg = "You have selected the following items:<br />";

 

    if (check1.Items[0].Selected) {

    msg = msg + check1.Items[0].Text + "<br />";

    }

    if (check1.Items[1].Selected) {

    msg = msg + check1.Items[1].Text + "<br />";

    }

    if (check1.Items[2].Selected) {

    msg = msg + check1.Items[2].Text + "<br />";

    }

   

    Message.Text = msg;

 

As you can see, checkboxes work in a slightly different way from radio buttons. Each time you add a value, rather than replacing it, the value is added to the contents of check1. However, for all other intents and purposes, you use them in the same way.

 

This operation will become clearer once you're familiar with Chapters 4 and 6 in which we deal with Data and Control Structures respectively.

 

One last point to note about checkboxes, though, is that you might want to treat each checkbox within a group as a separate entity, rather than have them all grouped together, in which case you could set all of them as separate <asp:checkbox> controls to reflect this:

 

<asp:checkbox id="check1" runat="server" Text="Madrid"/>

<asp:checkbox id="check2" runat="server" Text="Oslo"/>

<asp:checkbox id="check3" runat="server" Text ="Lisbon"/>

 

The text attribute here specifies the text that will appear next to the checkbox. The checkbox itself will not return a value. To find out whether it is checked or not we need to add some ASP.NET code to test if the Checked attribute is true or false: it will be true if the checkbox is checked.

 

Given that we've introduced a number of new concepts, in variables, we will stop here, as the subject of variables warrants a chapter in its own right. We've looked at the most basic server controls, and in order to make any more of them, we need to introduce new and more complex features.

Previous Page  Page 1 Page 2 Page 3 Page 4 Page 5 Page 6 Page 7 Page 8 Page 9 Page 10 Page 11 Page 13 Next Page  




Click here to Buy!

Buy Beginning ASP.NET with C# here

© Copyright 2002 Wrox Press This chapter is written by David Sussman, et al and taken from "Beginning ASP.NET with C#" published by Wrox Press Limited in June 2002; ISBN 1861007345; copyright © Wrox Press Limited 2002; all rights reserved.

No part of these chapters may be reproduced, stored in a retrieval system or transmitted in any form or by any means -- electronic, electrostatic, mechanical, photocopying, recording or otherwise -- without the prior written permission of the publisher, except in the case of brief quotations embodied in critical articles or reviews.





Useful Links


CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums
Rate this article:     Poor Excellent
View highlighted Comments
User Comments on 'ASP.NET : The checkbox and checkboxlist control'
RELATED ARTICLES
ASP FilesystemObject
by Jeff Anderson
An introduction to the Filesystemobject
ASP GetTempName
by Jeff Anderson
Use the GetTempName method to create a randomly generated temporary file on the server.
ASP Format Date and Time Script
by Jeff Anderson
An ASP script showing the variety of date and time formats possible using the FormatDateTime Function.
ASP OpenTextFile
by Jeff Anderson
An introduction to the OpenTextFile Method of the FileSystemObject
Email validation using Regular Expression
by Jeff Anderson
Using regular expression syntax is an exellent way to thoroughly validate an email. It's possible in ASP.
Add or Subtract Hours in SQL or ASP using DateAdd
by Jeff Anderson
A beginners guide to using the SQL DATEADD function to add or subtract hours. Particularly useful when setting the time displayed on the ASP page to a different time zone (eg when the server is in the US, and the site is for a UK audience).
The asp:radiobutton and asp:radiobuttonlist control
by David Sussman, et al
In HTML, radio buttons are used when we need to make multiple sets of choices available, but we want the user to select only one of them.
The asp:checkbox and asp:checkboxlist control
by David Sussman, et al
Checkboxes are similar to radio buttons, and in HTML, they were used to allow multiple choices from a group of buttons.
ASP FileExists
by Jeff Anderson
An introduction to the FileExistsMethod of the FileSystemObject
Concatenate strings in sql
by Jeff Anderson
A brief introduction to concatenating strings in an sql query (using SQL server or access databases).








Recent Forum Threads
•  Run a program both on windows and linux
•  VERO.SurfCAM.v2014
•  Schlumberger.Petrel.V2013.2
•  Petrel.V2013.2
•  Altair.HyperWorks.v12
•  VoluMill.v6.1
•  VoluMill.NEXION.6
•  VERO.SurfCAM.v2014
•  Schlumberger.Petrel.V2013.2


Recent Articles
ASP GetTempName
Decode and Encode UTF-8
ASP GetFile
ASP FolderExists
ASP FileExists
ASP OpenTextFile
ASP FilesystemObject
ASP CreateFolder
ASP CreateTextFile
Javascript Get Selected Text


© Copyright codetoad.com 2001-2015