
154
|
Chapter 6: User Controls and Custom Server Controls
This is the Title of the Book, eMatter Edition
Copyright © 2008 O’Reilly & Associates, Inc. All rights reserved.
Adding Client Script
Sometimes you may want to use client-side script in your ASP.NET pages, either
with controls or independent of them. In classic ASP, it was possible to write
client script to the browser using Response.Write. However, this could get very
messy—particularly if you needed to write the same set of code for use with more
than one form element.
The ASP.NET
Page class provides several methods for sending client script to the
browser that make this process simpler and more reliable.
These methods include:
RegisterClientScriptBlock
Renders a string containing the specified client script to the browser.
RegisterHiddenField
Adds an
<input> element whose type is set to hidden.
IsClientScriptBlockRegistered
Allows you to test whether a given named script block has been already regis-
tered by another control to avoid redundancy.
You might use these methods to pop up a message box on the client with the
number of Blogs that currently exist in the XML file. To accomplish this, add the
following snippet to the DisplayBlogs method of the Blog control:
Page.RegisterClientScriptBlock("Blog", "<script>alert('There are now " +
BlogRows.Count + " Blogs!');</script>");
Then, if any other controls need to use the same script, call IsClientScript- ...