Server-Side Processing


One thing that's very important to understand is when sections of code actually run. It's easy to assume that code for buttons is all there is to it, but a little explanation will really help you to understand what's happening while users are interacting with your web page, and, more to the point, when.

Statelessness

The first thing to understand is that every time you load a page, it's distinct from the previous time it was loaded. So, loading a page for the first time is different from loading the page that is a result of hitting a button, and different again from hitting that button once more. ASP.NET has no knowledge of the previous times the page has been loaded, so any code that ASP.NET runs doesn't know about the previous times it's been run. This is known as stateless – nothing is maintained in code between page loads.

So why is this important? Well, the answer is because you can't rely on anything that's previously happened in code. This means, for example, that if you perform some action as the page loads, then you might have to perform that same action again – each load of the page is distinct. Say you have a site that allows users to log in, and has a 'login' link on the page. Once logged in, you display a welcome message on a page. There are two separate pages here – the one before the user logged in, and the one after. They may have an order to the user, but to ASP.NET they are separate pages. This means you can't assume the page has any intrinsic knowledge of what's gone before. You'd have to check each time the page loads to see whether the user is logged in. For example, some code to do so might look like this:

 Sub Page_Load()  If User.IsLoggedIn Then  lblWelcome.Text = "Welcome " & User.Name  Else  lblWelcome.Text = "Welcome stranger, please log in."  End If End Sub 

Server Controls and Statelessness

Statelessness only refers to standard HTML elements and to code; it doesn't refer to web controls. These have an in-built facility to retain their values each time a page is requested. You can easily see this in action by creating a new ASP.NET page. Drag an HTML TextBox from the HTML Elements tab of the Toolbox onto the page, followed by a TextBox and Button from the web controls. Run the page and enter some text in both textboxes. Press the button and notice how the text from the HTML element disappears, but that for the web control doesn't. This is because the HTML element is stateless, but the web control isn't.

So why is this good? Isn't it confusing to have code stateless, but web controls retain their state? Let's consider a page that is the checkout for a website allowing you to buy goods. The page has textboxes for the user name, address, credit card, and so on. The user has filled in all of their details and pressed the button. This sends the details back to the server, where the credit card is validated. However, the user mistyped their credit card number, the credit card validation fails, and the page is re-displayed. If you were using HTML elements, all of the entered text would disappear and you'd have to type it in again – very annoying indeed! With web controls though, the text is retained.

Postback

Let's take this example further by assuming that there is a button on this page, to allow the user to login. When you hit a button, the information in any control is sent back to the server, and ASP.NET runs any code for that button. This is known as postback – the information is posted back to the server. Think of it in terms of a letter being posted to ASP.NET – it writes a new one back to you, and so on.

The postback causes the page to be sent to ASP.NET, which runs through any processing it needs to, and sends the resulting HTML back to the user. So, the process is the same as when the page is first loaded, and follows the same rules – there's no knowledge of any previous times the page has been loaded.

When does my Code Run?

So, when does the code actually run? Let's take an example of a DropDownList control showing countries, which are stored in a database. If they are in the database, how do we get them into the list? The specifics of that are covered in detail in a later chapter, as part of the databases section of the book, but what's important for now is when we get them into the list. So far, you've seen code running when a button is pressed, but you can also have code running when the page loads – it's where we'd go off to the database, fetch the countries, and populate the list. However, this code would also be run when a button is pressed, because a button press means the page is reloaded. Would we want to fetch the countries list from the database again? No, because the DropDownList control will retain its values (remember, web controls retain their values – code doesn't). So filling the list would be a waste of time – we only want it filled the first time the page is loaded.

Server-Side Events

The term "events" is what we call the actions that take place when something, or someone, interacts with our web page. For example, when a button is clicked, an event is generated (or raised, as it is sometimes called) – in this case, it is the Click event, indicating that the button has been clicked. This is represented by the following code:

 Sub Button1_Click(sender As Object, e As EventArgs) 
Note

The two arguments identify which control raised the event, and any extra information that the control is sending us. For a button, we don't get any extra information, but for more complex controls, such as grids and lists, we can find out which row has been selected. For more details on this you should consult the .NET Framework documentation.

So far, we've just doubled-clicked on a button to get to this code, but you can also get to it through the Events section on the Properties window:

Clicking this icon will show the events for the selected control. In the following screenshot, we can see these for a Button control:

Double-clicking to the right of an event takes you to the code window for that event. If there is no code then it's created for you.

So, what about the page? We've said that code runs when the page is loaded. You'd think that clicking on a blank area of the page and then looking at the Events pane would show the events relating to the page itself, but it doesn't. This is because clicking on the page doesn't select the page itself. You have to click on the page, and then select Page from the list:

You can then click the Events button to see the events for a page:

What you notice here is that there is a Load event, so you can double click it, which will lead to this code:

 Sub Page_Load(sender As Object, e As EventArgs) End Sub 

It's here that you enter any code to be run each and every time the page loads.

An alternative to the above method using the Properties and Events windows is just to switch to Code view, and to type in the above code by hand.

Loading a page and Detecting Postback

So now you can see two distinct events; one for when a page loads, and one for when a button is pressed. If we only want code to run the first time a page is loaded, then we need a way of detecting that. This is where the IsPostBack property comes in. Let's give it a go.

Try It Out—Using the IsPostBack Property

  1. Create a new ASP.NET Page called Events.aspx.

  2. Add a Button, and underneath it a Label, and underneath that another Label.

  3. Double-click the button, and add the following to the code:

    Sub Button1_Click(sender As Object, e As EventArgs)  Label2.Text = "The button has been clicked" End Sub
  4. Now, at the bottom of the code window add the following code:

     Sub Page_Load(sender As Object, e As EventArgs)  Label1.Text = "The page has been loaded"  Label2.Text = "" End Sub 
  5. Save the file and run it:

    In the resulting page, you can see how the text in the Page_Load event has been run.

  6. Now press the button:

    Notice that code in the Page_Load event has run, as well as the code we typed in for the button.

  7. Close the browser, and switch back to Web Matrix.

  8. Change the Page_Load event so that it contains the following code:

    Sub Page_Load(sender As Object, e As EventArgs)  If Page.IsPostBack Then  Label1.Text = "The page has been loaded because of a button click"  Else  Label1.Text = "The page has been loaded for the first time"  End If  Label2.Text = "" End Sub 

  9. Save the file and run it:

    click to expand

  10. Now press the button:

    click to expand

    Notice how two different sets of text are now displayed.

How It Works

This works because of the IsPostBack property, which is True if the page is being re displayed, in other words as a result of a button click. Remember how we mentioned earlier that pressing buttons generates a postback – the page is posted back to the server. For the first time a page is displayed IsPostBack is False. This allows us to test whether the page is being loaded for the first time, and take the appropriate action. We just displayed some text, but this is where you could decide whether or not to fetch data from a database.




Beginning Dynamic Websites with ASP. NET Web Matrix
Beginning Dynamic Websites: with ASP.NET Web Matrix (Programmer to Programmer)
ISBN: 0764543741
EAN: 2147483647
Year: 2003
Pages: 141

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net