Create an ASP.NET Web Custom Control - Rolute 3
Author: Qing Gu Gang prettywolf@vip.sina.com
In this series, "Routines 1" and "RRT 2" describe the Web custom controls that use the Visual Studio.net 2003 to generate their 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.
/// summary>
Like the first two examples, processes the control design when controlled.
[DefaultProperty ("Text"),
ToolboxData ("<{0}: demotextbox runat = server> {0}: demotextBox>")]
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 you wrote is complete, and now you can discover the compiled DLL file and add it to the toolbox to drag and drop it. This example is a demo to help you understand what you implement for ViewState. Inherited Control classes have implemented this process. You only need to implement iPostBackDataHandler. You can solve it yourself. The control is generated, but when dragging and dropping to the page, its design is not very friendly, just a string of text. So how do you drag up like System.Web.ui.WebControls.TextBox?
Below I introduce the reader to change the display.
First add a class file to the control project, named: Designer.cs. Then modify it as follows:
Using system;
Using system.Web.ui.design;
Using system.componentmodel;
Namespace Demo
{
///
/// Designer's summary description.
/// summary>
Public Class Demodesigner: ControlDesigner // Inherited ControlDesigner Class
{
Private demotextbox demotextBox; // Declare a control class object
Public Override Void Initialize (iComponent Component)
{
This.DemotextBox = (DEMOTEXTBOX) Component;
Base.initialize (Component);
}
// Reserved function getDesigntimehtml ()
Public override string getdesigntimehtml ()
{
String_html = "";
_html = "
Return_html;
}
}
}
The designer is complete, but it is not possible to display the control when it is designed, but also add Designer ("Demo.deModesigner" in the property code of the control class to enable the control and designer. The property code is as follows:
[DefaultProperty ("Text"),
Designer ("Demo.demodesigner),
ToolboxData ("<{0}: demotextbox runat = server> {0}: demotextBox>")]
Now this control is basically completed, and then drag it to the page before you find the displayed look like TextBox.
With this code as a frame, the reader can expand the properties of the control according to your own needs.