Use JavaScript to Handle Key Presses, Mouse Movements, and More

Problem

You want to add client-side JavaScript code to a Web form.

Solution

Use the Page.RegisterClientScriptBlock method to add JavaScript functions to a page, and add control attributes that call the functions.

Discussion

ASP.NET includes a rich programming model. Unfortunately, once a page is rendered to HTML, you can’t execute any more .NET code without first triggering a postback to the server. This limitation greatly reduces the effectiveness of validation code and other niceties that can lead to a more professional Web page.

Although .NET does not include any object interface for creating JavaScript, you can define a block of JavaScript code in a .NET string variable and instruct ...

Get Microsoft® Visual Basic® .NET Programmer's Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.