18.01.2013 Views

Mastering Visual Basic .NET

Mastering Visual Basic .NET

Mastering Visual Basic .NET

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

ASP was limited to the HTML controls. ASP.<strong>NET</strong> uses a new family of controls, the Web controls.<br />

The Web controls exist on the server, and you can program against them, just as you program<br />

Windows controls. Your code resides on the server and manipulates Web controls. When it’s time<br />

to send a response to the client, ASP.<strong>NET</strong> translates the Web controls into HTML controls and<br />

HTML code, and sends to the client a page that can be rendered by the browser. As you will realize<br />

after reading this and the following chapter, ASP.<strong>NET</strong> abstracts much of the mundane tasks of<br />

client/server interaction and makes the process look like programming a Windows application.<br />

I should clarify this point for the benefit of readers who are already familiar with ASP. If you’re new<br />

to Web programming, please bear with me. Before you reach the end of this chapter, everything will<br />

make perfect sense. With ASP, the browser submits the contents of the controls on the current page to<br />

the server when a special button is clicked, the Submit button. This button may be named anything: Go,<br />

Show Results, Place Order, whatever. The HTML code of the page uses a Submit button to send the<br />

data on the current page to the server (or post the page back to the server, as this process is known).<br />

The ASP script on the server knows the names of the controls on the page and must extract their<br />

values from the QueryString property of the Request object. If the page being submitted contains<br />

two Text controls, named ProdID and Quantity, it must use the following statements to extract the<br />

values on these two controls:<br />

ProductID = Request.QueryString(“ProdID”)<br />

Quantity = Request.QueryString(“Quantity”)<br />

Then, it must process them (retrieve the price of the specified product from a database, multiply<br />

it by the quantity ordered, and apply some discount) and create a page to send to the client. The<br />

page is created by sending HTML code to the client through the Write method of the Response<br />

object:<br />

Response.Write “Thank you for ordering”<br />

Response.Write “Your total is “ & price * Quantity<br />

where price is a variable that holds the product’s price (I’m not showing the code that retrieves this<br />

value from the database).<br />

With ASP.<strong>NET</strong>, you can use the TextBox Web control, which is very similar to the<br />

Windows TextBox control. Let’s say that the names of the two TextBoxes are ProdID and Quantity,<br />

and that the form also contains a Label control. To program the application, you double-click the<br />

Submit button and insert the following statements in its Click event handler:<br />

Label1.Text = “Thank you for ordering” & vbCrLf<br />

Label1.Text = Label1.Text & “Your total is “ & price * Quantity.Text<br />

AN HTML PRIMER<br />

This is VB.<strong>NET</strong> code, which will be compiled and executed on the server. You don’t have to use the<br />

Request object to retrieve the values of the TextBox controls, and you don’t have to use the Response<br />

object to create a new page. The results of the processing are placed on a Label control on the current<br />

form, which is then sent to the client. Obviously, ASP.<strong>NET</strong> parses the data submitted by the client<br />

through the QueryString object and makes them available to your code as control properties.<br />

In short, ASP.<strong>NET</strong> is a vastly improved version of ASP, abstracting many of the tasks in<br />

client/server interaction. It makes the whole process look like a Windows application to the developer,<br />

but behind the scenes it generates the HTML that will produce the desired page on the client.<br />

This page contains straight HTML code.<br />

1003

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!