Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 1.0 and 1.1 > BOOK: Beginning ASP.NET 1.0
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Beginning ASP.NET 1.0
This is the forum to discuss the Wrox book Beginning ASP.NET 1.0 with C# by Chris Goode, John Kauffman, Christopher L. Miller, Neil Raybould, S. Srinivasa Sivakumar, Dave Sussman, Ollie Cornes, Rob Birdwell, Matt Butler, Gary Johnson, Ajoy Krishnamoorthy, Juan T. Llibre, Chris Ullman; ISBN: 9780764543708
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 1.0 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Display Modes
  #1 (permalink)  
Old September 10th, 2007, 09:59 AM
Registered User
 
Join Date: Sep 2007
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Ch 11 Pg459 Exercise 5 C#

I'm trying to search through a sorted list to find an item that is entered into a textbox. However, when I try to add an else statement to the Find() method such as:
void Find(object sender, EventArgs e)
{
    foreach(DictionaryEntry Item in mySortedList)
    {
        if(findBox.Text == Item.Key.ToString())
        {
            findLbl.Text = "You have found " + "<b>" + Item.Key.ToString() + ".</b><br/>";
            findLbl.Text +="<b> Definition: </b>" + Item.Value.ToString();
        }
               else
                {
                  findLbl.Text = "That item is not in the List";
                }
    }
}

It doesn't find anything except for the last item in the list, which in this case is Zebra, but it works fine when the else statement is not in there. Can anyone explain to me why this is?

//***Code***//
<%@ Page Language="c#" %>

<html>
<script runat="server">

SortedList mySortedList = new SortedList();

void Page_Load(object source, EventArgs e)
{
    mySortedList["armadillo"]="any of a family ... small bony plates";
    mySortedList["amaryllis"]="an autumn-flowering ... Sprekelia)";
    mySortedList["zebra"]="any of several fleet ... white or buff";
    mySortedList["artichoke"]="a tall composite herb ... cooked as a vegetable";
    mySortedList["aardvark"]="a large burrowing ... termites and ants";
}

void Find(object sender, EventArgs e)
{
    foreach(DictionaryEntry Item in mySortedList)
    {
        if(findBox.Text == Item.Key.ToString())
        {
            findLbl.Text = "You have found " + "<b>" + Item.Key.ToString() + ".</b><br/>";
            findLbl.Text +="<b> Definition: </b>" + Item.Value.ToString();
        }
    }
}

void Display(object sender, EventArgs e)
{
    findLbl.Text = "";
    foreach(DictionaryEntry Item in mySortedList)
    {
        findLbl.Text += Item.Key.ToString() + " " + Item.Value.ToString() + "<br/>";
    }
}

</script>

<h1>What are you looking for?</h1>
    <form runat="server" autopostback="true">
        <asp:TextBox id="findBox" runat="server"/>
        <br/><br/>
        <asp:button id="find" Text="Search" OnClick="Find" runat="server"/>
        <asp:button id="display" Text="Display List" OnClick="Display" runat="server"/>
        <br/><br/>
        <asp:label id="findLbl" runat="server"/>
    </form>
</html>
 


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
My solution to exercise 6, on ch.2 - help rjm BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 2 July 28th, 2010 06:50 PM
Ch 28 exercise using Eclipse keeotee BOOK: Beginning JavaServer Pages 1 April 2nd, 2006 01:09 PM
Chapter 11 Exercise - IE6 Margins discgolfer BOOK: Beginning CSS: Cascading Style Sheets for Web Design ISBN: 978-0-7645-7642-3 4 January 23rd, 2006 01:05 PM
Ch. 18 Exercise 1, is there a change? horseatingweeds BOOK: Beginning CSS: Cascading Style Sheets for Web Design ISBN: 978-0-7645-7642-3 4 November 29th, 2005 05:33 PM



All times are GMT -4. The time now is 11:44 AM.


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.