Create A Stateful Component

For most cases, your applications only use components that do not hold any state between calls to the component (stateless components). If you have an application that makes multiple calls to a component and these calls rely on common state, then you will want to have attributes on your classes to hold this state. The state can be provided by setting properties on the class programmatically on your Web page or state can be set in the initialization of the class.

Like a simple component, the process for creating a stateful component starts with creating a C# source file. You first declare the namespace and then add any aliases that you need. For example, if you want to build strings, you can add an alias for the

System.Text namespace to get access to the

StringBuilder class.

To create a property for the class, you first create a private variable. Next, you will want to add some code for initializing the variable when the class is created. Then, you will need to create a public variable that will be used to read and write to the private variable. After you finish creating the code to create the property, you need to compile the program using the csc command at the command prompt. Finally, you need to create the Web page that uses this component.

CREATE A STATEFUL COMPONENT

CREATE A STATEFUL COMPONENT

□ Open a new document in your text editor.

L0 Create a new namespace.

< Add an alias to the System and System.Text namespace.

Create a private string variable for holding state.

Q Add a public string variable that has a method to read the value of the private string variable and a method to write to the private string variable.

□ Open a new document in your text editor.

L0 Create a new namespace.

< Add an alias to the System and System.Text namespace.

_Q Create a public class.

Create a private string variable for holding state.

^0 Initialize the private string variable when an instance of the class is created.

Q Add a public string variable that has a method to read the value of the private string variable and a method to write to the private string variable.

—O Add a function that returns a string.

CREATE CUSTOM COMPONENTS

You can create a Web page containing a form used for updating the Name property of the stateful component.

TYPE THIS:

<%@ Import Namespace="StatefulComponent" %> <HTML>

<HEADxSCRIPT LANGUAGE="C#" RUNAT="Server"> public void Page_Load(Object sender, EventArgs E){ if (!Page.IsPostBack){

StatefulComponent statefulcomponentMessage = new StatefulComponent(); string stringMessage = statefulcomponentMessage.SayWelcomeStatement(); labelMessage.Text = stringMessage;

else{

StatefulComponent statefulcomponentMessage = new StatefulComponent(); statefulcomponentMessage.Nama = textboxName.Text;

string stringMessage = statefulcomponentMessage.SayWelcomeStatement(); labelMessage.Text = stringMessage;

<FONT FACE ="Verdana"><FORM RUNAT="Server"> <H3><ASP:LABEL ID="labelMessage" RUNAT="Server" /></H3> <P/><ASP:TEXTBOX ID="textboxNane" TEXT="Type your name here." RUNAT="Server" WIDTH=n300px"/><P/>

<ASP:BUTTON ID="buttonPersonalize" RUNAT="Server" TEXT="Personalize"/> </FORM></FONTx/BODY></HTML>

RESULT:

A Web page allows you to put your name in a text box that is used to give you a personal greeting when the form is posted back to the server.

C: ViVlNNT\Si>stem32\cmd

C : \ Inetpub\iiAiAuroot>

C: ViVlNNT\Si>stem32\cmd

Microsoft Windows 2DDD [Version 5.BD.2195] (C) Copyright 1985 2DDD Microsoft Corp.

C:\>cd C:\InetPubViuwwroüt

C:\InetpubVuAAuroot>csc /tilibrarji /out:bin\StatefulComponent.dll StatefulC

onponent.es_

Microsott IHJ Visual Utf Compiler Version /.UU.9Ttb LCLK version V1.U.261M

Copyright (Cj Microsoft Corp 2000. ALI rights reserved.

C : \ Inetpub\iiAiAuroot>

| Create a StringBuilder variable and initialize with a message.

Use an if statement to customize the message to the user if the Name property is set.

Use an else statement to handle the case where the Name property has not been set.

Return the StringBuilder variable.

m Go to the command prompt.

™ Compile the component using the csc command.

CONTINUED

Was this article helpful?

0 0

Post a comment