How to: Create Scripts and Edit Event Handlers

As you edit an HTML document in Visual Studio 2005, you can create client-script functions that cause elements on the page to respond to user actions. For example, if your page contains an HTML button (an <input type="button"> element, not an ASP.NET Button server control), you can select a button and then add a client-script event handler for its click event. The DefaultClientScript Property of an HTML document or ASP.NET Web page sets the default scripting language that is used for new client-side script elements. You can set the DefaultClientScript Property in the Properties window. For more information, see How to: Set the DefaultClientScript Property of a Web Page.

Note

You can add client script to ASP.NET server controls so that the controls respond to user actions in both client script and server code. For more information, see Client Script in ASP.NET Web Pages.

In Visual Studio 2005, you can create event handlers for HTML controls (input elements) and for the document and window objects. In addition, you can create handlers and scripts for all elements by coding them manually.

Note

The shortcut key combination CTRL + F2 places the mouse pointer focus in the Object drop-down list where you can specify which object you want to insert an event for. What the Object list displays depends on whether you are using a code-behind page model or single-file page model for your Web page. For the code-behind page model, you can insert events for Client Objects & Events and Client Script. For the single-file page model, you can insert events for Server Objects & Events, Server Code, Client Objects & Events, and Client Script.

Security noteSecurity Note:

Always encode and validate user input in your Web application before using it. This helps protect against script exploit attacks. For more information, see Basic Security Practices for Web Applications and Validating User Input in ASP.NET Web Pages.

To create a client-script event handler

  1. In Source view, open the HTML or ASP.NET Web page that you want to work on.

  2. In the Client Objects & Events box, click the control that you want to write a handler for.

  3. In the upper-right box, click the event.

    The editor creates a skeleton event handler, and if necessary, a script block to contain it. The script block uses the client-script language that is set as the DefaultClientScript Property for the document.

  4. Fill in the script.

    The editor provides Microsoft IntelliSense technology to assist you.

See Also

Concepts

Client Script in ASP.NET Web Pages

ASP.NET Web Page Code Model

Reference

HTML Designer Shortcut Keys, General Development Settings