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 Dell

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

Software Developer - ASP.Net (C#, VB.Net) SQL Server in Taunton

This is a hands on role that requires someone to work on internal business systems in ASP.Net and C# or VB.Net. If you have experience in VB.Net they ...

VB.Net ASP.net. Leeds Centre. Learn Marketable skills. 32,000 VB.Net

Global FTSE 250 company in the heart of Leeds is currently looking for a VB.Net developer with experience of web development. The initial project ...

Junior ASP.Net Developer - ASP.Net, VB.Net, SQL Server - Bristol

They ideally want people that have worked with ASP.Net and VB.Net. Your role will be focused on delivering applications and databases using ASP.Net, ...

Featured Talkback

Why do so many (virtually all) software packages think that they are so important that they have to be started automatically every time the computer boots? What is the largest number of "speed access", "update check", "camera download" and whatever other background programs you have ever seen running? Of those, how many did you really need?

By: J.A. Watson

Read full story:
Annoying software: a rogues' gallery

Discussions

319762 319762

Eve of Distraction

Saturday 26 July 2008, 4:37 AM

1 comment

Vista Upgrade Blog

Microsoft's pre-modern message puts a...

Over at ZDNet.com, Ed Bott reports a first sighting of Microsoft's eagerly awaited $300 million ad campaign. Already the cause of much speculation, the consensus is that this will be... More

8 comments

A $40 CONSUMER-class router has create...

Believe it or not I don't work in IT, haven't for 7 years. Yes I work with Microsoft's Windows XP Embedded and as a result I have to know a lot about the OS, the kernal, Win API calls... More

Post a comment

Sick Puppy Redo

I generally follow a dispassionate investigative process when trying to discern what happened when a project goes bad. Although its a low priority item, it gets done simply because... More

Post a comment