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! || Forums || RAM 
Search Site:



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

The ASP.NET Code

We've seen that the ASP.NET server control passes form data to the ASP.NET code. Now it's up to the ASP.NET code to access it. Let's take a look at how it does this now:

 

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

  void Page_Load()

    {

      if (Page.IsPostBack){  

      Message.Text = "You have selected " + list1.SelectedItem.Value;

      }

    }

</script>

 

There are three lines of code here inside Page_Load(). We won't explain how the code works fully, as there are some features in it that won't be introduced until Chapter 6.

 

The purpose of this code is to check if a selection has been made in the listbox, and then display a message confirming the choice the user made. If they haven't made a choice, then it won't display anything in the <asp:label> control.


The first line of code:

 

if (Page.IsPostBack){

 

checks to see whether the page has been returned by the user before. This check involves using the Page object that we mentioned in the last chapter. The Page object keeps a record of whether or not a form has been submitted by the user in IsPostBack. If the form has been submitted, IsPostBack returns true, otherwise it returns false.

 

The code inside the If statement and curly braces will only be run if the form has been posted back by the user. So, if this is the first time the user has seen the form, then ASP.NET will jump the code until it encounters the next brace:

 

}

 

But, if the user has submitted the page, then the following line will be run:

 

Message.Text = "You have selected " + list1.SelectedItem.Value;

 

This line displays a customized message in the <asp:label> control about which destination the user has selected. The list1.SelectedItem.Value is the item of particular interest. The list1 identifier name is set when the server control is created. So when the <asp:label> control is created:

 

<asp:dropdownlist id="list1" runat="server">

 

then ASP.NET will enable us to use the identifier as a handle to the contents of this specific drop-down listbox control as follows:

 

list1.SelectedItem.Value

 

As list1 is unique on the page, it refers directly to our drop-down listbox. If you look up the drop-down listbox control in the class browser, you can see that the control has a SelectedItem property (properties correspond directly with attributes). We could reference this attribute of our drop-down listbox control as follows in our ASP.NET code:

 

list1.SelectedItem.Value

 

The SelectedItem keeps a record of which item the user has selected. The Value item returns the corresponding contents of the <asp:listitem> tag.

 

So given that this will return the contents of the  <asp:listitem>  tag for the corresponding list choice made by the user, our message is constructed as follows:

 

"You have selected " + "Lisbon"


So where does Message.Text come into all of this? Message, as you might have already noticed, is the unique identifier of our <asp:label> control. We use Message.Text to display the message, as this refers to the text that this particular <asp:label> control will display. You can find the <asp:label> control just underneath the <body> tag on our ASPX page:

 

<body>

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

  <br />

 

In this way, we can ensure that the first time a user logs on, there is no message displayed in the <asp:label> control, as it isn't set until the page has been submitted at least once, but every time the user subsequently returns to the page, there is a message displayed. We will be looking further at how the particular if statements work in Chapter 6.

Previous Page  Page 1 Page 2 Page 3 Page 4 Page 5 Page 6 Page 7 Page 9 Page 10 Page 11 Page 12 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.











Recent Forum Threads
•  Grep or fgrep ??
•  Grep or fgrep ??
•  Grep or fgrep ??
•  How do you create....
•  Re: Import Excel File to sql server database...
•  automation of Word Mail Merge
•  Re: Filepointer in Perl
•  Re: dynamic crystal report generation
•  Data transfer from DB2 to Sybase using Perl?


Recent Articles
What is a pointer in C?
Multiple submit buttons with form validation
Understanding Hibernate ORM for Java/J2EE
HTTP screen-scraping and caching
a javascript calculator
A simple way to JTable
Java Native Interface (JNI)
Parsing Dynamic Layouts
MagicGrid
Caching With ASP.Net


© Copyright codetoad.com 2001-2006