Component

components used in the Web page or Web Service are released to .NET's garbage collection. The .NET Framework's garbage collector manages the allocation and release of memory for your application. Because this is how the Web server operates, you do not put state that needs to be held across pages in the standard components used by your Web application. There are mechanisms built into the .NET framework to enable you to manage state across pages in a site. The Session object is one common mechanism that is available to you. See page 210 for details on state management.

CREATE A STATEFUL COMPONENT (CONTINUED)

CREATE A STATEFUL COMPONENT (CONTINUED)

GenericTemplate.aspx template from the Code Templates directory.

£ Import the StatefulComponent namespace.

'-"E Add the Page_Load function.

Create a new variable of type StatefulComponent.

Set the Name property for the StatefulComponent.

• Create a new string variable and read the result of SayWelcomeStatement into that string.

GenericTemplate.aspx template from the Code Templates directory.

£ Import the StatefulComponent namespace.

'-"E Add the Page_Load function.

Create a new variable of type StatefulComponent.

Set the Name property for the StatefulComponent.

• Create a new string variable and read the result of SayWelcomeStatement into that string.

CREATE CUSTOM COMPONENTS

You can control your stateful components that persist data with fields and properties by initializing them with object constructors. You can also control the assignment of properties with validation code.

Example:

using System;

public class Goal {

private String m_strDescription;

m_strDescription = null;

public String Name {

return m_strDescription;

m_strDescription = value;

Untitled - Notepad

File Edit Format Help

<%@Import Namespace-'StatefulComponent" %>

«SCRIPT LANGUAGE="C#" RUNAT="Server"> public void Page_Load(Object sender, EventArgs E) {

StatefulComponent statefulcomponentMessage - new

StatefulComponentO;

statefulcomponentMessage.Name = "Danny"; string stringMessage -statefulcoinbonentMessaae.SavWelcomeStatementO:

labelMessage.Text - stringMessage;|

'FORM RUNAT="Server">

<H3acASP:LABEL ID="labelMessaqe" RUNAT="Seiver" JaeJH3a|

Set the label on the page equal to what was returned from

SayWelcomeStatement.

Add a server form.

Add a label to the server form.

E Save the file and request it from the Web server.

■ A welcome message appears that displays the value set for the Name property.

Was this article helpful?

0 0

Post a comment