03.11.2016 Views

Beginning ASP.NET 4.5 in CSharp and VB Opsylum

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

312 x CHAPTER 9 VALIDATING USER INPUT<br />

To prevent your system from receiv<strong>in</strong>g <strong>in</strong>valid data, it’s important to validate this data before you<br />

allow your system to work with it. Fortunately, <strong>ASP</strong>.<strong>NET</strong> <strong>4.5</strong> comes with a bag of tools to make<br />

data validation a simple task.<br />

The first part of this chapter gives you a good look at the validation controls that <strong>ASP</strong>.<strong>NET</strong> supports.<br />

You see what controls are available, how to use <strong>and</strong> customize them, <strong>and</strong> <strong>in</strong> what scenarios<br />

they are applicable.<br />

The second half of this chapter shows you how to work with data <strong>in</strong> other ways. You see how to<br />

receive the <strong>in</strong>formation a user submits to your site <strong>and</strong> then send it by e-mail <strong>and</strong> how to customize<br />

the mail body us<strong>in</strong>g text-based templates.<br />

By the end of the chapter, you will have a good underst<strong>and</strong><strong>in</strong>g of the flow of <strong>in</strong>formation to an <strong>ASP</strong><br />

.<strong>NET</strong> website <strong>and</strong> the various techniques you have at your disposal to validate this data.<br />

GATHERING DATA FROM THE USER<br />

Literally every website on the Internet has to deal with <strong>in</strong>put from the user. Generally, this <strong>in</strong>put can<br />

be sent to the web server with a number of different techniques, of which GET <strong>and</strong> POST are the most<br />

common. In Chapter 4, you briefly saw the difference between these two methods <strong>and</strong> saw that GET<br />

data is appended to the actual address of the page be<strong>in</strong>g requested, whereas with the POST method,<br />

the data is sent <strong>in</strong> the body of the request for the page.<br />

With the GET method, you can retrieve the submitted data us<strong>in</strong>g the QueryStr<strong>in</strong>g property of the<br />

Request object, as discussed <strong>in</strong> Chapter 7. Imag<strong>in</strong>e you are request<strong>in</strong>g the follow<strong>in</strong>g page:<br />

http://www.PlanetWrox.com/Reviews/ViewDetails.aspx?ReviewId=34&CategoryId=3<br />

With this example, the query str<strong>in</strong>g is ReviewId=34&CategoryId=3. The question mark is used to<br />

separate the query str<strong>in</strong>g from the rest of the address, <strong>and</strong> the query str<strong>in</strong>g itself consists of name/<br />

value pairs separated by an ampers<strong>and</strong> (&). The names <strong>and</strong> values <strong>in</strong> turn are separated by the<br />

equals symbol (=). To access <strong>in</strong>dividual items <strong>in</strong> the query str<strong>in</strong>g, you can use the Get method of the<br />

QueryStr<strong>in</strong>g collection:<br />

<strong>VB</strong>.<strong>NET</strong><br />

' Assigns the value 34 to the reviewId variable<br />

Dim reviewId As Integer = Convert.ToInt32(Request.QueryStr<strong>in</strong>g.Get("ReviewId"))<br />

' Assigns the value 3 to the categoryId variable<br />

Dim categoryId As Integer = Convert.ToInt32(Request.QueryStr<strong>in</strong>g.Get("CategoryId"))<br />

C#<br />

// Assigns the value 34 to the reviewId variable<br />

<strong>in</strong>t reviewId = Convert.ToInt32(Request.QueryStr<strong>in</strong>g.Get("ReviewId"));<br />

// Assigns the value 3 to the categoryId variable<br />

<strong>in</strong>t categoryId = Convert.ToInt32(Request.QueryStr<strong>in</strong>g.Get("CategoryId"));<br />

The POST method, on the other h<strong>and</strong>, gets its data from a form with controls that have been submitted<br />

to the server. Imag<strong>in</strong>e you have a form with two controls: a TextBox called Age to hold the<br />

user’s age <strong>and</strong> a Button to submit that age to the server. In the Button control’s Click event, you<br />

could write the follow<strong>in</strong>g code to convert the user’s <strong>in</strong>put to an <strong>in</strong>teger:

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

Saved successfully!

Ooh no, something went wrong!