Create an ASP.NET Web Custom Control - Rolute 3

xiaoxiao2021-03-06  39

"Routines in this series"

1

"And" routines

2

"Tell the use

Visual studio.net2003

Existing

Web

Custom control generates your own custom controls by inheriting or composite some simple controls. Such controls are simple, but its execution efficiency is relatively low, so what should this control do we do if we don't inherit existing controls?

Below the author tells you the programming method of this self-container through an example.

(Routine uses C #)

This routine implements a TextBox, which is checked for the input string, replacing the half-angle single quotes (half-angle single quotes cause database error).

The control first should inherit all controls: system.Web.ui.control, implement two interfaces: iStateManager, iPostBackDataHandler (Processing Retreat), then write system.web.ui.webControls.TextBox write Some common properties and methods. In this case, this example only implements the Text property.

Using system;

Using system.Web.ui;

Using system.Web.ui.webcontrols;

Using system.componentmodel;

Namespace Demo

{

///

/// WebCustomControl1 summary description.

///

Like the first two examples, processes the control design when controlled.

[DefaultProperty ("Text"),

Designer ("Demo.demodesigner),

ToolboxData ("<{0}: demotextbox runat = server> ")]

Public class deminextbox: system.Web.ui.Control, istateManager, iPostBackDataHandler

{

Private statne;

Private bool_marked;

Here is the properties we have to implement: Text

[Bindable (TRUE),

Category ("APPEARANCE"),

DEFAULTVALUE ("")]

Public String Text

{

get

{

String_text = (string) ViewState ["text"];

Return_Text == NULL? ": _ text;

}

set

{

String text = "";

TEXT = VALUE;

Text = text.Replace ("'", "'");

ViewState ["Text"] = text;

}

}

In order to implement view status, you must implement the ISTATEMANAGER interface.

Object istateManager.saveviewstate ()

{

Object _StateState = null;

IF (_STATE! = null)

_StateState = (iStateManager) _State) .saveViewState ();

IF (_StateState == null)

Return NULL;

Return_Statestate;

}

Void istateManager.trackviewState ()

{

_MARKED = True;

IF (_STATE! = null)

(ISTATEMANAGER) _STATE) .TRACKVIEWSTATE ();

}

Void istateManager.LoadViewState (Object State)

{

IF (State! = NULL)

{

Object _newstate = (object) state;

(IStateManager) .loadViewState (_newstate);

}

}

Bool istateManager.IstrackingViewState

{

get

{

Return_Marked;

}

}

INTERNAL New Statebag ViewState // Note that the viewState property of the base class here is covered

{

get

{

IF (_State == null)

{

_State = new statebag (true);

IF ((iStateManager) .IstrackingViewState)

(ISTATEMANAGER) _STATE) .TRACKVIEWSTATE ();

}

Return_State;

}

}

The following will output the control of the control to the page, in fact system.web.ui.webcontrols.textbox is also reorganizing Input.

Protected Override Void Render (HTMLTextWriter Output)

{

String stroutput = " ";

Output.write (stroutPut);

}

#Region iPostBackDataHandler member

Public void raisepostDataChangeDevent ()

{

// Todo: Add DemotextBox.raisePostDataChangeDevent implementation

}

The following method is important, save the return data.

Public Bool LoadPostData (String PostDataKey, System.Collections.Specialized.NameValueCollection PostCollection)

{

// Todo: Add DemotextBox.LoadPostData implementation

String PresentValue = this.Text;

String postedvalue = postcollection [postDataKey];

if (! PresentValue.equals (PostedValue)) // If the return data is not equal to the original data

{

THIS.TEXT = PostedValue;

Return True;

}

Return False;

}

#ndregion

}

}

Ok, a TEXTBOX control written is done. If the reader feels that you can realize the viewstate trouble, then you can change the inherited base class by system.web.ui.controls.WebControl, so you only need to implement iPostBackDataHandler, viewstate's question control yourself solved.

转载请注明原文地址:https://www.9cbs.com/read-83064.html

New Post(0)