ZDNet UK


Skip to Main Content

ZDNet.co.uk - Winner of Best Business Website 2007
  1. Home
  2. News
  3. Blogs
  4. Reviews
  5. Prices
  6. Resources
  7. Community
  8. My ZDNet

 

ZDNet UK RSS Feeds


IT Jobs

Office applications Toolkit

Tie JavaScript events to ASP.NET objects

Tony Patton Builder.com

Published: 07 May 2003 10:40 BST

  • Email
  • Trackback
  • Clip Link
  • Print friendly
  • Post Comment

The ASP.NET Framework gives you enormous power and flexibility. You can create server-side code to handle any type of requirement, but nothing beats client-side JavaScript for many tasks. Adding JavaScript to an ASP.NET Web form is like adding it to any other type of Web page -- but binding code to ASP.NET form elements is not as simple.

Why JavaScript?
The merits of client-side JavaScript have been exhausted in other articles. The main advantage is performance. The same functionality is easily developed using C# or VB.NET, but it requires a roundtrip to the server and thus entails a performance hit. The JavaScript counterpart is downloaded with the page, and execution is confined to the browser with no server call. Common scenarios for this approach include field validation, confirmation dialog boxes, and opening other windows.

Let's review how you include JavaScript in a regular Web page. Then, we'll take a look at the extra steps needed to get JavaScript to play nice with ASP.NET Web forms.

Traditional Web forms
A regular Web page includes various standard areas, such as the head and the body. Normally, JavaScript functions are placed in the head portion of the form so that they are loaded before the body, making them immediately available. The functions are then called from HTML elements within the page body. As an example, let's look at the following JavaScript function:

function confirmSubmit() {
var doc = document.forms[0];
var msg = "Are you sure you want to submit this data?";
if (confirm(msg)) {
doc.submit();
} else {
// do nothing
} }

This function is simple: It asks the user to verify whether he or she wants to submit the document. The document is submitted if the user clicks Yes in the confirmation box; otherwise, nothing happens.

The function is easily connected to an HTML submit button:
<input type="button" value="submit" name="butSubmit" onClick="confirmSubmit();">

Once the user clicks the button, the message in Figure A appears.

Figure A

    Example JavaScript confirmation

Next

Previous

1 2


  • Email
  • Trackback
  • Clip Link
  • Print friendly Print with HP

Did you find this article useful?
63 out of 127 people found this useful


Full Talkback thread

0 comments

Company/Topic Alerts

Create a new alert from the list below:









Related Jobs

VB.Net, SQL Server 2005 Win Forms Developer Needed in Bristol

Huxley Associates are working exclusively on a role for a Microsoft solutions provider based near central Bristol. This company is a Microsoft Gold ...

VB.net web developer, yorkshire, sqlserver, c#, vb.net

My health care client based in yorkshire have a software developer position available for a long contract. You will be involved in the full project ...

C#/VB.NET,ASP.NET Web Developer - Manchester

Real Resourcing require a Web Developer to be based on site in Manchester with experience in VB.NET or C# with ASP.NET 1.1/2.0 and SQL Server ...

Vista Upgrade Blog

Windows XP SP3 Installed

I have downloaded and installed Service Pack 3 for Windows XP Professional on my Fujitsu Lifebook S6510. Everything went smoothly, and it seems to work just fine. I don't see anything... More

Post a comment

Vista vs. XP: The Final Retreat

I suppose that most people are getting tired of reading about Vista vs. XP. I know that I am getting tired of writing about. I'm getting even more tired of fighting with it. So this... More

Post a comment

Vista Memory Leak?

I'm wondering if anyone else has seen anything that looks like a memory leak in Vista? I've been running Vista Business on my Lifebook S6510 for several weeks now, and overall I'm... More

Post a comment

Featured Talkback

"We don't recommend specific technologies — we promote the use of technology per se." What sort of nonsense is this?? Every Becta endorsed IT supplier to schools is a Microsoft shop. Every single one.

By: 1000193068

Read full story:
Becta takes Microsoft to the OFT