In this series of articles, "Routine 1" and "routine 2" describe the use of Visual Studio. existing WEB custom controls in NET2003 generate custom controls by inheriting or combining some simple controls. This control is relatively simple to create, but its execution efficiency is relatively low. So if we do not inherit the existing control, how should we do this control?
The following describes the programming method of this self-writing control through examples.
(Routine uses C #)
This routine implements a TextBox, which tests the input string and replaces the single quotation marks with single quotation marks (single quotation marks lead to database errors ).
The control must first inherit the base class of all controls: System. web. UI. control: implements two interfaces: IStateManager (ViewState) and IPostBackDataHandler (processing the returned data. web. UI. webControls. textBox to write some common attributes and methods. Due to space limitations, this example only implements the Text attribute.
Using System;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. ComponentModel;
Namespace Demo
{
/// <Summary>
/// Summary of WebCustomControl1.
/// </Summary>
Like in the previous two examples, you can first process the properties of the control during design.
[DefaultProperty ("Text "),
ToolboxData ("<{0}: DemoTextBox runat = server> </{0}: DemoTextBox>")]
Public class DemoTextBox: System. Web. UI. Control, IStateManager, IPostBackDataHandler
{
Private StateBag _ state;
Private bool _ marked;
The following are the attributes we want 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;
}
}
IStateManager interface must be implemented to achieve view status.
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) ViewState). LoadViewState (_ newState );
}
}
Bool IStateManager. IsTrackingViewState
{
Get
{
Return _ marked;
}
}
Internal new StateBag ViewState // note that the ViewState attribute of the base class is overwritten here.
{
Get
{
If (_ state = null)
{
_ State = new StateBag (true );
If (IStateManager) this). IsTrackingViewState)
(IStateManager) _ state). TrackViewState ();
}
Return _ state;
}
}
The following shows the output of the control to the page. In fact, System. Web. UI. WebControls. TextBox repacks the Input.
Protected override void Render (HtmlTextWriter output)
{
String strOutput = "<Input name = \" "+ this. clientID + "\" type = \ "text \" value = \ "" + this. text + "\"> ";
Output. Write (strOutput );
}
# Region IPostBackDataHandler Member
Public void RaisePostDataChangedEvent ()
{
// TODO: Add DemoTextBox. RaisePostDataChangedEvent implementation
}
The following method is very important to save the returned 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 returned data is not equal to the original data
{
This. Text = postedValue;
Return true;
}
Return false;
}
# Endregion
}
}
Now, a self-written TextBox Control is complete. Now you can drag and drop the compiled dll file and add it to the toolbox to the page. The ViewState processing in this example is a demonstration to help you understand the method for implementing ViewState. The inherited Control class has implemented this processing. In reality, you only need to implement IPostBackDataHandler, the ViewState control solves the problem by itself.
Controls are generated. However, when you drag and drop them onto the page, they are not very friendly in design, but only a string of text. So how can I drag it like System. Web. UI. WebControls. TextBox and display it as an input box?
The following describes how to change the display.
First, add a class file named Designer. cs to the control project. Then modify it to the following code:
Using System;
Using System. Web. UI. Design;
Using System. ComponentModel;
Namespace Demo
{
/// <Summary>
/// Summary of Designer.
/// </Summary>
Public class DemoDesigner: ControlDesigner // inherits the ControlDesigner class
{
Private DemoTextBox demoTextBox; // declare a control class Object
Public override void Initialize (IComponent component)
{
This. demoTextBox = (DemoTextBox) component;
Base. Initialize (component );
}
// Overload function GetDesignTimeHtml ()
Public override string GetDesignTimeHtml ()
{
String _ html = "";
_ Html = "<Input type = \" text \ "value =" + this. demoTextBox. Text + "> ";
Return _ html;
}
}
}
The Designer is complete, but the widget cannot be displayed during design. You must also add the Designer ("Demo. DemoDesigner") to the Property Code of the control class to associate the widget with the Designer. The Property Code is as follows:
[DefaultProperty ("Text "),
Designer ("Demo. DemoDesigner "),
ToolboxData ("<{0}: DemoTextBox runat = server> </{0}: DemoTextBox>")]
Now this control is basically complete. After compilation, drag it to the page and you will find that the display is the same as that of TextBox.
With this code as the framework, readers can expand the properties of the control as needed.