Build Your Own ASP.NET 3.5 Website Using C# & VB (108 page)

Read Build Your Own ASP.NET 3.5 Website Using C# & VB Online

Authors: Cristian Darie,Zak Ruvalcaba,Wyatt Barnett

Tags: #C♯ (Computer program language), #Active server pages, #Programming Languages, #C#, #Web Page Design, #Computers, #Web site development, #internet programming, #General, #C? (Computer program language), #Internet, #Visual BASIC, #Microsoft Visual BASIC, #Application Development, #Microsoft .NET Framework

BOOK: Build Your Own ASP.NET 3.5 Website Using C# & VB
8.33Mb size Format: txt, pdf, ePub

"/Images/newsletter_header.gif"" />" & _

"

" & _

HttpUtility.HtmlEncode(introTextBox.Text) & "

" & _

"

Employee of the month: " & _

HttpUtility.HtmlEncode(employeeTextBox.Text) & "

" & _

"

This month's featured event: " & _

HttpUtility.HtmlEncode(eventTextBox.Text) & "

" & _

""

C#

Dorknozzle\CS\03_AdminNewsletter.aspx.cs
(excerpt)

message.Body =

"" +<br/></p><p>HttpUtility.HtmlEncode(subjectTextBox.Text) +<br/></p><p>"" +

"

"/Images/newsletter_header.gif\" />" +

"

" +

HttpUtility.HtmlEncode(introTextBox.Text) + "

" +

"

Employee of the month: " +

HttpUtility.HtmlEncode(employeeTextBox.Text) + "

" +

"

This month's featured event: " +

HttpUtility.HtmlEncode(eventTextBox.Text) + "

" +

"";

Licensed to [email protected]

Working with Files and Email

631

As we’re building an HTML document, we need to take care to convert special

characters (including <, >, and &) into their character entity equivalents (<, >,

&, and so on). The HtmlEncode method of the HttpUtility class does this for

us.

Also note that the image we’ll use in the email has to be hosted on a site somewhere.

In the code above, I’ve used an example URL. To get this example to work properly,

you’ll need to host the image on your web site, and use the appropriate URL in your

code.

We set the Host property of the smtpClient object to localhost, indicating that the

computer that’s acting as our ASP.NET server should also act as our outgoing mail

server—you’ll need to change this if you’re using another SMTP server. Finally, we

call the Send method, pass in the message object, and display a confirmation message

to the user within the resultLabel control:

Dorknozzle\VB\03_AdminNewsletter.aspx.vb
(excerpt)

smtpClient.Host = "
localhost
"

smtpClient.Send(message)

resultLabel.Text = "Email sent!
"

Dorknozzle\CS\03_AdminNewsletter.aspx.cs
(excerpt)

smtpClient.Host = "
localhost
";

smtpClient.Send(message);

resultLabel.Text = "Email sent!
";

Save your work and run the page in your browser. Enter all the necessary information

into the Newsletters page and click the
Send Newsletter
button, as shown in
Fig-

ure 14.16.

Licensed to [email protected]

632

Build Your Own ASP.NET 3.5 Web Site Using C# & VB

Figure 14.16. Sending the newsletter

Licensed to [email protected]

Working with Files and Email

633

Figure 14.17. Receiving the newsletter

You should receive the “Email sent!” message once the email is sent. Check your

email account for the new email message. The message should look like the one

shown in Figure 14.17.

Summary

This chapter introduced you to some useful topics relating to files and email; it gave

you the ability to read and write to files, access directory information, upload files

from the client to the server, and send email messages in ASP.NET.

The topics we covered in this chapter will prove invaluable as you develop applications with ASP.NET. Although, strictly speaking, they’re not core features of ASP.NET, these tools enable you to implement many of the common requirements

of ASP.NET applications. Stick ’em in your mental tool belt for now—I guarantee

they won’t gather much dust!

Licensed to [email protected]

Licensed to [email protected]

Chapter15

ASP.NET AJAX

Throughout this book, we’ve been building ASP.NET Web Forms that, after any

user interaction, send all of the page’s data back to the server. You may not really

notice any problems with this approach if you’re developing locally on a fast computer, but once your application is deployed to the Internet, it may appear to be quite slow and unresponsive to users. However, by harnessing ASP.NET AJAX, you

can take a slow and clunky web application and make it sing with surprisingly little

effort.

In this chapter we’ll learn about:

■ what Ajax is and how it works

■ ASP.NET AJAX

■ the UpdatePanel and UpdateProgress controls

■ using triggers

■ the ASP.NET AJAX Control Toolkit

■ the ValidatorCalloutExtender control extender

■ the UpdatePanelAnimationExtender control extender

Licensed to [email protected]

636

Build Your Own ASP.NET 3.5 Web Site Using C# & VB

What is Ajax?

What is
Ajax
, you ask? The acronym itself, coined by Jesse James Garret in 2005,

stands for Asynchronous JavaScript and XML.1 In a nutshell, the term represents

the methodologies and technologies that allow developers to make web applications

feel far more interactive and responsive. Notable examples of applications that

utilize Ajax include Gmail, Google Maps, Yahoo! Mail, Kayak, Flickr, and Facebook.

Moreover, just about every significant web site you interact with today probably

takes advantage of some Ajax concepts to improve the user experience in some way.

On a technical level, Ajax uses a few key technologies:

■ The most critical piece is the JavaScript XmlHttpRequest object. Originally invented by Microsoft for Internet Explorer 4, it’s now a standard feature of all browsers. This very powerful object allows developers to use JavaScript to make

HTTP GET and POST requests without submitting the whole page, and to then

access the content of the server response. Note that the Xml part of the name is

a bit misleading. In practice, you’re free to choose the format of the response

data—XML, JSON,2 an HTML snippet, or just about any format that can be sent

over HTTP.

■ The second most important technology is browser DOM scripting: Document

Object Model manipulation using JavaScript. JavaScript is required to create the

XmlHttpRequest object and make the request, but also to take the raw data returned from the request, parse it, and integrate it back into the page for the user.

■ The final leg on which Ajax applications stand is standards–based HTML and

CSS. HTML provides the structure and the content, while CSS is used to control

the presentation of the interface.

Like any technology, Ajax is not without its challenges. First and foremost, to do

anything with Ajax, you need to have JavaScript enabled in the browser. From an

accessibility viewpoint, heavily interactive web sites can be very difficult for people

using assistive technology like screen readers to use. The application of Ajax within

1 “Ajax: A New Approach to Web Applications,”
http://www.adaptivepath.com/ideas/es-

says/archives/000385.php

2 JavaScript Object Notation, a lightweight data transfer format for use with JavaScript. See
ht-

tp://www.json.org/
for further information.

Licensed to [email protected]

ASP.NET AJAX

637

a web site can subtly alter the behavior of the browser in ways that users can find

confusing—for example, they may be unable to use the
Back
button, or accurately

bookmark pages. Finally, if your web site content can’t be accessed by any means

other than JavaScript, search engines won’t be able to index your content.

Ajax may sound intimidating, and for the original trail blazers there were a number

of complex problems to solve. But, as Ajax has matured and become a standard

component of modern web development, a number of tools have been created to

make “Ajaxifying” your application very easy. You may have heard of—or even

used—
some of these tools, including jQuery,
3
Prototype,4
and MooTools.5
However, as a budding ASP.NET developer, you really need to familiarize yourself with one

tool in particular: Microsoft’s ASP.NET AJAX.

ASP.NET AJAX

ASP.NET AJAX, originally called ASP.NET Atlas, was originally a separate add-on

to ASP.NET
2.0, but has been included as a core part of ASP.NET 3.5.6
The library itself has three components. The first is a set of client-side JavaScript libraries,

known as the Microsoft AJAX Library, which provide the framework of cross-browser

functionality that’s used by the ASP.NET AJAX server-side components. Second,

on the server-side, Microsoft developed a set of HTTP handlers and modules to

wrap the standard XML-based web services and allow them to respond to Ajax requests using JSON. Finally, there’s the component we’ll focus on in this chapter—the server-side controls that make Ajaxifying your application incredibly easy.

Included in the core ASP.NET AJAX library are just a few key server controls:

1. The ScriptManager control is rather boring and unsung, but it’
s absolutely crit-

ical.
7 As its name indicates, it manages the registration of client scripts on your web page, handling mundane tasks like making certain that your Ajax controls

have the scripts they need and, more importantly, that these scripts are included

only once.

3 http://jquery.com/

4 http://www.prototypejs.org/

5 http://mootools.net/

6 http://www.asp.net/ajax/

7 http://www.asp.net/AJAX/Documentation/Live/overview/ScriptManagerOverview.aspx

Licensed to [email protected]

638

Build Your Own ASP.NET 3.5 Web Site Using C# & VB

2. The Timer control
provides the plumbing to perform postback operations at

defined intervals.
8 Again, it’s a straightforward, often-overlooked control, but it’s very handy for those operations.

3.
The final components are the interrelated UpdatePanel and UpdateProgress

controls.9
The UpdatePanel, when used with a ScriptManager, allows a developer to use
partial rendering
—a procedure by which different sections of a page are

updated independently, without requiring the browser to reload the entire page.

The UpdateProgress control informs users that a partial rendering operation is

in progress.

While it probably doesn’t seem like there’s much variety in the way of server controls, these provide the core functionality to let us very quickly and easily add Ajax functionality to just about any web site that uses ASP.NET 2.0 or later versions.

Using the UpdatePanel Control

The most exciting control included in the core of ASP.NET AJAX is the UpdatePanel.

It’s been called the “gateway drug” to Ajax, and rightly so—it can be added to nearly

Other books

Terror at the Zoo by Peg Kehret
The Time Stone by Jeffrey Estrella
Flame of Sevenwaters by Juliet Marillier
Lieutenant by Grenville, Kate
Pushing the Limits by Katie McGarry
The Selkie’s Daughter by Deborah Macgillivray