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.

376 x CHAPTER 10 <strong>ASP</strong>.<strong>NET</strong> AJAX<br />

page. The downside of register<strong>in</strong>g the web service <strong>in</strong> the master page is that its client JavaScript is referenced<br />

<strong>in</strong> each <strong>and</strong> every page <strong>in</strong> your site. For a service you only use once or twice, it’s much better<br />

to add a ScriptManagerProxy to the specific page(s) <strong>and</strong> register the service there. With<strong>in</strong> your page,<br />

the ScriptManagerProxy control looks <strong>and</strong> acts like a normal ScriptManager control. However, <strong>in</strong><br />

reality it’s just a proxy control that relays all its sett<strong>in</strong>gs to the true ScriptManager <strong>in</strong> the master page,<br />

comb<strong>in</strong><strong>in</strong>g the sett<strong>in</strong>gs for both controls. You used the ScriptManagerProxy control as follows to set<br />

up the element:<br />

<br />

<br />

<br />

<br />

<br />

All you need to do is refer to the service by sett<strong>in</strong>g the Path property. Just as with other server-side<br />

URLs you have seen <strong>in</strong> this book so far, you can use the tilde (~) syntax to refer to the application’s<br />

root.<br />

Once you have registered the service, it becomes available <strong>in</strong> your client-side code. Note that<br />

IntelliSense <strong>in</strong> VS is smart enough to discover the WCF services you have def<strong>in</strong>ed <strong>and</strong> registered. As<br />

soon as you typed NameService followed by a dot <strong>in</strong> a client-side script block, IntelliSense kicked <strong>in</strong><br />

aga<strong>in</strong> <strong>and</strong> showed the public methods it found. This makes it extremely easy to f<strong>in</strong>d the correct services<br />

you have def<strong>in</strong>ed <strong>in</strong> your site. This is a huge improvement over old versions of Visual Studio that<br />

had only a fixed number of JavaScript-related items <strong>in</strong> the IntelliSense list. Start<strong>in</strong>g with Visual Studio<br />

2008, IntelliSense is now actually able to look at your code <strong>and</strong> fill the IntelliSense list with the right<br />

variable names, methods, services, <strong>and</strong> so on that it f<strong>in</strong>ds <strong>in</strong> your code. In VS 2010 <strong>and</strong> VS 2012,<br />

Microsoft improved IntelliSense even further by improv<strong>in</strong>g the performance <strong>and</strong> the accuracy of the<br />

items shown <strong>in</strong> IntelliSense.<br />

To see how the actual page works, <strong>and</strong> how it accesses the web service, take a look at the code <strong>in</strong> the<br />

block.<br />

The first code you need to look at is the helloWorld method:<br />

function helloWorld()<br />

{<br />

var yourName = document.getElementById('YourName').value;<br />

NameService.HelloWorld(yourName, helloWorldCallback);<br />

}<br />

First, this code gets a reference to the text box you created earlier. You then access its value property<br />

to get the name the user entered.<br />

This name is then sent to the web service method HelloWorld with the follow<strong>in</strong>g code:<br />

NameService.HelloWorld(yourName, helloWorldCallback);<br />

The first argument of the call to HelloWorld is the argument that the web service method expects:<br />

a str<strong>in</strong>g hold<strong>in</strong>g your name. The second argument, helloWorldCallback, is a reference to another<br />

JavaScript method that is triggered when the service returns the result. By design, the call to the web

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

Saved successfully!

Ooh no, something went wrong!