Using JavaScript Along with asp.net

xiaoxiao2021-03-06  19

Using JavaScript Along with asp.net

Bill Evjen Reuters

February 2004

Applies TO: Microsoft® ASP.NET Microsoft Visual Basic® .NET Microsoft Visual C # ® .NET

Summary: Learn How to Apply JavaScript in Asp.Net Applications. Also Learn Ways of Doing Control Focus, Button Rollovers, Alerts and Planning Text In The Browser's Status Bar. (17 Printed Pages)

Contents

Introduction Adding JavaScript to a Server Control Performing a Simple Button-rollover Setting Control Focus Changing the Control Focus Dynamically Using Larger JavaScript Functions Keeping JavaScript in a Separate File (.js) Summary

Introduction

Web developers have been working around the limitations of the browser for a long time by using a combination of server-side processing and client-side processing. Working logic and application processes on the client-side allows browser based applications to seem more responsive and to have more "snappiness" to them. For client-side development in the past, most developers turned to JavaScript and intermingled this programming language into their classic Microsoft® Active Server Pages along with their server-side Microsoft Visual Basic® Scripting Edition or Microsoft JScript ® Code. NOW with Microsoft Asp.Net and The New Models That It Province, Web Developers offen wonder how to profrly work with their javascript functions in their asp.net pages.

This article will take a look at some of the ways past JavaScript functions can now be used. There is more than one way to accomplish this task and this article will take a look at many different possibilities. This article will also take a look at some Of The More Common Uses of JavaScript in ASP.NET PAGES with SORT Examples.

Adding JavaScript to a Server ControlIt is quite easy to add JavaScript to a specific server control that resides on an ASP.NET page. Let's take a look at the button server control as an example. If you drag and drop a Button HTML server control ( HTMLINPUTBUTTON CLASS) ONTO A Page Using Either Microsoft Visual Studio® .NET or THE ASP.NET Web Matrix and Run IT As a Server Control, IT Should Have The Following Construction:

This is a normal button that can be programmatically manipulated in the code-behind or server-side script of an ASP.NET page. For example, to assign the button text when the page is generated, simply use the value property of the button after This Element is Turned Into An HTML Server Control (Right-Click On The Control).

Visual Basic .NET

Private sub page_load (byval sender as system.object, _ byval e as system.eventargs) handles mybase.load button1.value = datetime.now.tostring () End Sub

Visual C # .net

Void Page_Load (Object Sender, Eventargs E) {Button1.Value = DateTime.now.toString ();

...............

Figure 1. Showing the date and time on a button

It is important to note that the ASP.NET page here gets the time from the server that generated the page. So if the Web server sits somewhere in the Central Time Zone of the United States (CST -6 GMT), then everyone who requests THIS Page Will Get The Same Time No Matter Where The Reside in The World.

What if you wanted the button to show the time of the person viewing the page? The easiest way of accomplishing this task would be to do this using JavaScript on the client-side.For an example of this, we will place the end user's ( The Viewer of the Web Page) Computer Time ON A Button Web Server Control. The Following Code Shows How To Accomplish this task:

Visual Basic .NET

<% @ Page language = "VB"%>