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

IBM Technology Conference & Expo 2012
May 23, 2012

Convention Centre B2 Room at 22nd Floor, Centara Grand @ Central World, 999/99 Rama I Road, Pathumwan, Bangkok 10330

Echelon 2012
June 11 and 12, 2012

University Cultural Centre, National University of Singapore

Startup Asia Jakarta 2012
June 7 and 8, 2012

12th Floor, Annex Building, Wisma Nusantara Complex, Jl. M.H. Thamrin No. 59 Jakarta 10350, Indonesia

MMA Forum Singapore
April 23-25, 2012

Grand Hyatt 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

Big data acquisitions pave way to fast, effective innovation - ZDNet Asia News http://t.co/vDZpl0lu

"Big data acquisitions pave way to fast, effective innovation" including @Vivisimo_Inc (client) in @ZDnetAsia http://t.co/yNSdPqbb

Homegrown smartphone OSes gaining favor in China: 59 Jakarta 10350, Indonesia Locally-made mobile operating syst... http://t.co/BruP98Es

RT @MDMGeek: Big data acquisitions pave way to fast, effective innovation - ZDNet Asia http://t.co/ky8YgPAn #Bigdata #analytics via @ciropuglisi

Integration, focused investments to propel Windows Phone http://t.co/6JkDa9sB

RT @AsianFashionLaw: Malaysia offers some manufacturing benefits over China http://t.co/bMquIFiX

Acquisitions in the Big Data market increasingly important to enterprises… http://t.co/Br4BkXyZ

Experience trumps content in apps monetization http://t.co/iaCY5ebX

Malaysia offers some manufacturing benefits over China http://t.co/bMquIFiX

RT @MDMGeek: Big data acquisitions pave way to fast, effective innovation - ZDNet Asia http://t.co/ky8YgPAn #Bigdata #analytics via @ciropuglisi

Thats it.Im digging up an old bus plan i wrote around acquisition of #bigdata talent. http://t.co/gpkha5A1 Any investors want2 read/discuss?

Integration, focused investments to propel Windows Phone: By Kevin Kwang , ZDNet Asia on May 23, 2012 (2 mins ag... http://t.co/aaa0Cb73

Homegrown smartphone OSes gaining favor in China http://t.co/lOBVp1T6

Homegrown smartphone OSes gaining favor in China: 59 Jakarta 10350, Indonesia Locally-made mobile operating syst... http://t.co/gHypbdIY

Integration, focused investments to propel Windows Phone - ZDNet Asia http://t.co/7sZi6Dhb

So much as we know , MTK6575 extremely integrated frequency1GHz ARM Cortex-A9 processor, the superiority of 3G / HSPA Modem, and help the...

1 day ago by y15822137359 on 5 SaaS adoption speed bumps to avoid

I reckon your view: "CRM is strategy, not software", if a company replicating the approach uses in ERP implementation into CRM, what they...

2 days ago by wykoong on Gartner: Mobile CRM gives better ROI than social

This video will teach you about the Excel fill handle but also provide you with a workook to download... http://www.youtube.com/watch?v=...

3 days ago by TradeBrother on A quick fill handle trick for Microsoft Excel

waiting...

5 days ago by eapete on What should count in a company's market value?

Boy, you've opened a can of worms now.

Wait for the rants & raves.

5 days ago by eapete on What should count in a company's market value?

I was puzzling before this whether to replicate the success formula we executed for a financial institute, and come out with a standard s...

5 days ago by wykoong on Drop the egos, copy ideas, then innovate