The <asp:listbox> Control
The next HTML server control that we'll look at, <asp:listbox>, is very much related to <asp:dropdownlist>. In fact, we've almost mentioned it already. Remember the <select> HTML form control that creates drop-down listboxes? Well, the <asp:listbox> is a server-side equivalent of using that <select> tag with the size
attribute set to the maximum number of options possible. In fact the only
difference with this control is the fact that it doesn't drop down, and that it
is capable of multiple selections.
The <asp:listbox> has the following format:
<asp:listbox
id="list1" runat="server" selection mode =
"multiple">
 <asp:listitem>Madrid</asp:listitem >
 <asp:listitem >Oslo</asp:listitem >
 <asp:listitem >Lisbon</asp:listitem >
</asp:listbox>
There is one point of interest, however:
the selectionmode attribute is used to
determine whether you can select multiple or only single items from the
listbox. By default, it is set to single,
but you have the option to change it.
Let's take a look at a quick example, where
we alter our previous example to use a listbox instead of a drop-down list
control. We'll also alter it to allow multiple selections.
Try It Out – Using the <asp:listbox> Control
1.
Enter the following code into your editor:
<script
runat="server" language="C#">
   void Page_Load()
   {Â
 string msg = "You have selected: <br />";
     if (list1.Items[0].Selected) {
   msg = msg + list1.Items[0].Text + "<br />";
 }
       if (list1.Items[1].Selected) {Â
    msg = msg + list1.Items[1].Text +
"<br />";
 }
       if (list1.Items[2].Selected) {
   msg = msg + list1.Items[2].Text +
"<br />";
 }
    Message.Text = msg;
   }
</script>
<html>
 <head>
   <title>Drop Down List
Example</title>
 </head>
 <body>
   <asp:label id="Message"
runat="server"/>
   <br />
   <form runat="server">
   Which city do you wish to look at hotels
for?<br /><br />
   <asp:listbox id="list1"
runat="server" selectionmode="multiple">
    Â
<asp:listitem>Madrid</asp:listitem>
    Â
<asp:listitem>Oslo</asp:listitem>
    Â
<asp:listitem>Lisbon</asp:listitem>
   </asp:listbox>
   <br /><br /><br /><br
/>
   <input type="Submit">
   </form>
 </body>
</html>
2.
Save this as listpage2.aspx.
3.
Run this page in your browser, and
use the Ctrl, or shift keys to select multiple choices:
4. Click on Submit Query to
see the following:
How It Works
The controls in this example have hardly changed from the previous listpage.aspx example. All we've done is add a selectionmode attribute to allow us to make multiple selections:
<asp:listbox id="list1"
runat="server" selectionmode="multiple">
 <asp:listitem>Madrid</asp:listitem>
 <asp:listitem>Oslo</asp:listitem>
 <asp:listitem>Lisbon</asp:listitem>
</asp:listbox>
However, we've had to completely overhaul
our ASP.NET code:
   string msg = "You have selected: <br />";
     if (list1.Items[0].Selected) {
   msg = msg + list1.Items[0].Text + "<br />";
 }
       if (list1.Items[1].Selected) {Â
    msg = msg + list1.Items[1].Text + "<br />";
 }
       if (list1.Items[2].Selected) {
   msg = msg +
list1.Items[2].Text + "<br />";
 }
    Message.Text = msg;
In fact, we've introduced a whole new
format to determine the options to display.
We use an If construct (we'll talk more about these in Chapter 6) to determine
which items have been selected, and then use a variable
assignment to create the display output. The first line in our code creates a String variable (again, we'll be looking at variables in depth very
shortly). This simply declares a label 'msg' which refers to a portion in the computer's memory that we can use
to hold a sequence of characters, or string. We set it up with a simple heading, and then add city names on the
end, according to which list elements have been selected. Finally, we assign
its final value (a long string of HTML) to the Text attribute of the Message
label, so that it can be seen on the page.
That was a bit more complicated, wasn't it?
Don't worry, it will become clearer once you've familiarized yourself with the
topics in the following few chapters. For now, let's move on to some different
types
of control.
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.
|
|