Good2Know: Calling .NET Web Service via Javascript (Client side)

The .NET framework makes it very simple to accomplish a task that is not in the least trivial.  What client-side calls to a web service provides is the ability to retrieve data and make it available to the user without a page refresh.  So let me get right to the point and show you how it’s done.

Place a ScriptManager within the Form tag

<asp:ScriptManager ID=”ScriptManager1″ runat=”server”>

<Services>

<asp:ServiceReference Path=”http://localhost/YourService/databroker.asmx&#8221; />

</Services>

</asp:ScriptManager>

The web service will be called by one JavaScript method and its result returned to a return method.  The GetWebServiceData method will call the web service, pass the parameters the service receives and also pass the method that the web service will call to return its results.

function GetWebServiceData() {

var myName;

myName = “Danny”

MyWebServiceNamespace.MyWebService.HelloWorldMethod(myName, WebServiceReturn);

}
function WebServiceReturn(result) {

document.getElementById(“resultReturn”).innerHTML = result;

}

The applications are endless and chances are you have already seen this in action countless of times.  This opens the door to much richer and interactive data driven applications.

Good2Read: ASP.NET 2.0 Website Programming: Problem – Design – Solution

If you have been programming for a while you have come to understand that the only way to keep up with the ever growing amount of technology Redmond throws at us is to read…constantly…without stopping.  The fact that everybody and their mother seems to be writing nowadays doesn’t help the situation either.  Over the years I’ve come to identify authors and publishers that seem to produce quality books on a variety of subjects.  Authors like like Carli Watson, Rockford Lhotka, Steve McConnell (to name a few) put out quality on everything they write.

A new face in the good authors line up is Marco Bellinaso.  Marco wrote ASP.NET 2.0 Web Site Programming which turned out to be a great book I always recommend to someone that knows some ASP.NET and wants to take their development of enterprise level applications to the next level.  Marco has done an excellent job at outlining what I think should be the path every developer takes in identifying the problem at hand, designing the application and providing a solution to the client.  Reading Marco’s book is a delight for the one seeking how serious development is done.  It is the kind of book that you wear out from frequent reading because it could easily become part of one’s library of reference books.

If you are looking for a sound approach to the world of web development look no further, you have arrived.  Reading this book you’ll learn concepts like how UML design fits in the overall planning stage, how designing a  Data Access Layer (DAL) and a Business Logic Layer (BLL) prior to any UI implementation (what a concept right?) is the key to an application that can withstand the test of time and much more.  Marco’s conversational writing approach makes the learning of these important concepts simple to understand and implement.

Good2Read: ASP.NET WebForms vs MVC

Dino Esposito, and if you don’t know who he is you need to read more, has an excellent article on the July issue of the MSDN Magazine where he compares WebForms and ASP.NET MVC.

If you haven’t taken the plunge into the MVC pool, below are some pros and cons of both WebForms and ASP.NET MVC.

WebForms

Pros:

  • Third party controls
  • As close as stateful forms as an app can get with viewstate, postbased and the event-driven paradigm.
  • No need to know HTML, CSS, etc.  Just know .NET and you are ready to go.

Cons:

  • Hard to  test
  • HTML abstraction hinders browser compatibility
  • Hard to integrate with javascript frameworks such as jQuery, PrototypeJS, etc.
  • Lack of parameters in links when using viewstate tends to yield pages that rank lower on search engines.

ASP.NET MVC

Pros:

  • Separation of concerns (SoC) architecture
  • Ideal for Test Driven Development (TDD)
  • Complete script and CSS freedom due to simpler markup

Cons:

  • Full control of HTML, Javascript and CSS might be seen as a weakness for some.
  • Requires familiarity of the MVC pattern.  One needs to know how Models, Views and Controllers work together.

So when it is all said and done, it comes down to using the right tool to address a particular issue.  Don’t get stuck with only a hammer in hand because then everything you’ll see will look like a nail.