Online Book Reader

Home Category

Programming Microsoft ASP.NET 4 - Dino Esposito [105]

By Root 5542 0
surrounding markup text for parsing and instantiation. It does not contain information useful to instantiate a control, and for this reason it can be omitted from the strings you pass directly to ParseControl.

Script-Related Methods


Table 5-13 enumerates all the methods in the Page class related to HTML and script code to be inserted in the client page.

Table 5-13. Script-Related Methods

Method

Description

GetCallbackEventReference

Obtains a reference to a client-side function that, when invoked, initiates a client callback to server-side events.

GetPostBackClientEvent

Calls into GetCallbackEventReference.

GetPostBackClientHyperlink

Appends javascript: to the beginning of the return string received from GetPostBackEventReference. For example:

javascript:__doPostBack(‘CtllD’,’’)

GetPostBackEventReference

Returns the prototype of the client-side script function that causes, when invoked, a postback. It takes a Control and an argument, and it returns a string like this:

__doPostBack(‘CtlID’,’’)

IsClientScriptBlockRegistered

Determines whether the specified client script is registered with the page. It’s marked as obsolete.

IsStartupScriptRegistered

Determines whether the specified client startup script is registered with the page. It’s marked as obsolete.

RegisterArrayDeclaration

Use this method to add an ECMAScript array to the client page. This method accepts the name of the array and a string that will be used verbatim as the body of the array. For example, if you call the method with arguments such as theArray and “’a’, ‘b’”, you get the following JavaScript code:

var theArray = new Array(‘a’, ‘b’);

It’s marked as obsolete.

RegisterClientScriptBlock

An ASP.NET page uses this method to emit client-side script blocks in the client page just after the opening tag of the HTML

element. It’s marked as obsolete.

RegisterHiddenField

Use this method to automatically register a hidden field on the page. It’s marked as obsolete.

RegisterOnSubmitStatement

Use this method to emit client script code that handles the client OnSubmit event. The script should be a JavaScript function call to client code registered elsewhere. It’s marked as obsolete.

RegisterStartupScript

An ASP.NET page uses this method to emit client-side script blocks in the client page just before closing the HTML element. It’s marked as obsolete.

SetFocus

Sets the browser focus to the specified control.

As you can see, some methods in Table 5-13, which are defined and usable in ASP.NET 1.x, are marked as obsolete. In ASP.NET 4 applications, you should avoid calling them and resort to methods with the same name exposed out of the ClientScript property.

// Avoid this in ASP.NET 4

Page.RegisterArrayDeclaration(...);

// Use this in ASP.NET 4

Page.ClientScript.RegisterArrayDeclaration(...);

The ClientScript property returns an instance of the ClientScriptManager class and represents the central console for registering script code to be programmatically emitted within the page.

Methods listed in Table 5-13 let you emit JavaScript code in the client page. When you use any of these methods, you actually tell the page to insert that script code when the page is rendered. So when any of these methods execute, the script-related information is simply cached in internal structures and used later when the page object generates its HTML text.

Events of the Page Class


The Page class fires a few events that are notified during the page life cycle. As Table 5-14 shows, some events are orthogonal to the typical life cycle of a page (initialization, postback, and rendering phases) and are fired as extra-page situations evolve. Let’s briefly review the events and then attack the topic with an in-depth discussion of the page life cycle.

Table 5-14. Events a Page Can Fire

Event

Description

AbortTransaction

Occurs for ASP.NET pages marked to participate in an automatic transaction when a transaction aborts

CommitTransaction

Occurs for ASP.NET pages marked to participate in an

Return Main Page Previous Page Next Page

®Online Book Reader