Using JavaScript for faster ASP.NET

 

Summary

Using ASP.NET's server-based controls may impair performance if server links are slow. JavaScript provides an alternative by negating the need, in some cases, to call the server.

Events

Asian Financial Services Congress 2012
23 - 24 Feb 2012

Marina Bay Sands, Singapore

ASP.NET provides a new paradigm for developing Web-based applications. This includes a set of server-based controls that are analogous to HTML form elements such as text boxes, buttons, and so forth. The problem with these controls is the need to call the server.

That's where JavaScript comes in--by providing an alternative for many tasks that negates the need to go to the server, hence improving performance.

Making calls to the server requires bandwidth and server processing time. This may not be a problem with an intranet application, which has the advantage of a high-speed LAN, but an Internet application is a different story. An Internet user's connection speed varies depending on whether the user has a dial-up modem, DSL, or a cable modem. Utilising client-side JavaScript negates the need for a server round-trip.

The traditional approach
A standard Web form includes numerous standard areas such as the head, body, and form. JavaScript functions are traditionally placed in the head portion of the Web form. This allows the functions to be loaded and be available to the rest of the page. Once loaded, the functions may be called from HTML elements.

Let's take a look at a simple JavaScript example:

function valSubmit() {
var doc = document.forms[0];
var msg = "";
if (doc.firstName.value == "") {

msg += "- Please enter a first name.n";
}

if (doc.lastName.value == "") {
msg += "- Please enter a last name.n";
}

if (msg == "") {
doc.submit();
} else {
alert("The following errors were
encountered.nn" + msg);
} }

This function verifies that values are entered into two HTML fields on the form. If either field is empty, an error message displays and execution stops. If both fields are populated, the form is submitted. You may call this function from an HTML button with the following code:

name="btnSubmit" onClick="valSubmit();">

With this relationship, the form isn't submitted to the server until the fields are populated. The code may be simple, but it doesn't have any adverse effect on performance since no extra server calls are necessary; and the JavaScript code is short and sweet, which means the form doesn't require additional load time. This solution utilises normal HTML form elements, but using it with an ASP.NET Web form isn't as straightforward.

Combining JavaScript with ASP.NET
ASP.NET Web forms allow the use of standard HTML, so you can easily use the previous example--but it negates the power ASP.NET offers. ASP.NET User Controls allow you to call server code easily to process the Web form. Fortunately, it's possible to combine the power of the User Controls and JavaScript. I'll demonstrate this with an ASP.NET Button control.

The Button control's Attributes property provides a way to tie JavaScript to the control. First, the JavaScript function is placed on the ASP.NET Web form, but it's altered in a particular way: A return value is added. The function returns true if the validation is successful; this signals that the form may be submitted to the server. The submission to the server signals that the server function associated with the button is called. A return value of false signals the function failed, so the form isn't submitted.

<%@ Page language="c#" %>


WebForm1





type="text" name="firstName" id="firstName">

type="text" name="lastName" id="lastName">



This is the key line in the code:

btnSubmit.Attributes.Add("onClick", "return valSubmit();");

The elements are placed on the HTML form using CSS via their style attribute. The form ties the JavaScript function to the ASP.NET Button (btnSubmit) and to its HTML onClick event. The OnClick attribute of the ASP.NET Button tells the system what function is called when the form is submitted to the server.

If you're a VB.NET developer, the only change to the previous code is the C# block of code. The VB.NET version follows:

<script language="vb" runat="server">
Private btnSearch_Click (sender As Object, e As System.EventArgs)
Response.Write("Search")
End Sub
Private Page_Load(sender As Object, e As System.EventArgs)
btnSubmit.Attributes.Add("onClick", "return valSubmit();")
End Sub
</script>
A powerful combination
JavaScript is the de facto standard for client-side Web development. Combining it with ASP.NET Web forms provides the developer with a powerful set of tools for building robust applications that consider performance a key ingredient.

Tony Patton began his professional career as an application developer earning Java, VB, Lotus, and XML certifications to bolster his knowledge.

Talkback

Add your opinion

In order to post a comment, you need to be registered. (Sign In or register below)

Post your comment

ZDNet Asia Live

Microbloggers in Beijing must authenticate real names before 16th March or will have their access limited to browsing. http://t.co/tB30exzy

DailySync 2.2 http://t.co/CSogJW6P

RT @BeyondTrust: Breached security vendors' response should focus on customers http://t.co/PxDjTjEJ #infosec #databreach #security

Judge: Chinese engineer didn't spy on US: By Ellyne Phneah , ZDNet Asia on February 9, 2012 (11 hours ago) US ju... http://t.co/7Cw5G6Be

New standards make using carrier Wi-Fi super easy: ... ago) A set of new standards being developed will soon mak... http://t.co/Xvnumaxx

First 'biological computer' created, can read DNA. http://t.co/7dny07vS

Amazon breaks into India online retail market http://t.co/MUR5i5NU

Singapour : 95% des foyers et societes seront equipés en fibre optique mi-2012 http://t.co/zPLb1erU

RT @HostingInAsia: Singapour : 95% des foyers et societes seront equipés en fibre optique mi-2012 http://t.co/zPLb1erU

Gr8 article via @ZDNet on user's role in securing their videoconferencing estates: http://t.co/0KuypBEN

RT @ArkadinAPAC: Home telepresence demand to grow despite hiccups http://t.co/1yz8tSdK

New Wi-Fi standard won't replace Ethernet http://t.co/NgALh39J via @zdnetasia

APP NEWS--RIM says app interest actually really high
http://t.co/iNIMbdYW

EU News: New standards make using carrier Wi-Fi super easy: Joining a carrier Wi-Fi hot spot on your... http://t.co/trMYbugj #smartphone

Home telepresence demand to grow despite hiccups http://t.co/1yz8tSdK

Nice research and pardon If there are some major issues which make cloud security trembling & risky.. 1. Abuse and Nefarious Use of Clo...

12 hours ago by evabrian on Cloud, mobility to drive security market

Nice research and pardon If there are some major issues which make cloud security trembling & risky.. 1. Abuse and Nefarious Use of Clo...

12 hours ago by evabrian on Cloud to drive industry, security concerns remain