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.

776 x APPENDIX A EXERCISE ANSWERS<br />

Exercise 2 Solution<br />

If you don’t <strong>in</strong>spect the IsValid property, your system is vulnerable to <strong>in</strong>valid data. Users can disable<br />

JavaScript <strong>in</strong> their browsers <strong>and</strong> submit <strong>in</strong>valid data directly <strong>in</strong>to your page. By check<strong>in</strong>g the<br />

IsValid property you can tell whether or not it’s safe to cont<strong>in</strong>ue with the submitted data.<br />

Exercise 3 Solution<br />

The From property of the MailMessage class is of type MailAddress, mean<strong>in</strong>g that you can directly<br />

assign a s<strong>in</strong>gle <strong>in</strong>stance of this class to it. Because you can potentially have more than one recipient,<br />

the To property is a collection of MailAddress objects, <strong>and</strong> so you need to use its Add method to<br />

add <strong>in</strong>stances of MailAddress to it.<br />

Exercise 4 Solution<br />

To call a client-side validation function, you need to set the ClientValidationFunction property<br />

of the CustomValidator like this:<br />

*<br />

The client function that you need to add to the markup of the page must have the follow<strong>in</strong>g<br />

signature:<br />

function functionName(source, args)<br />

{ ... }<br />

The source argument conta<strong>in</strong>s a reference to the actual CustomValidator control <strong>in</strong> the client-side<br />

HTML code. The args argument provides context <strong>in</strong>formation about the data <strong>and</strong> enables you to<br />

<strong>in</strong>dicate whether or not the data is valid. The names of the arguments don’t have to be source <strong>and</strong><br />

args; however, when us<strong>in</strong>g these names, the client-side function looks as close to its server-side<br />

counterpart as possible. Another common nam<strong>in</strong>g scheme, used for almost all other event h<strong>and</strong>lers<br />

<strong>in</strong> <strong>ASP</strong>.<strong>NET</strong>, is to use sender <strong>and</strong> e, respectively.<br />

Exercise 5 Solution<br />

To tell the validation mechanism whether the data you checked is valid, you set the IsValid property<br />

of the args argument <strong>in</strong> your custom validation method. This applies to both client- <strong>and</strong> serverside<br />

code. The follow<strong>in</strong>g snippet shows how this is done <strong>in</strong> the client-side validation method for the<br />

ContactForm.ascx control:<br />

if (phoneHome.value != '' || phoneBus<strong>in</strong>ess.value != '')<br />

{<br />

args.IsValid = true;<br />

}<br />

else<br />

{<br />

args.IsValid = false;<br />

}

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

Saved successfully!

Ooh no, something went wrong!