. Net provides you with a variety of controls, rich enough. But sometimes you want to develop some of your own controls to improve development efficiency or implement some special functions.
The following describes how to create a most basic web Composite Control.
First, create a C # class library project named MyControl. Delete the default class file class1 and create our own class file MyControl. cs.
Now we add a password to it.
First, modify the generated code to be like this.
We will use the following reference:
Using System;
Using System. IO;
Using System. Drawing;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. ComponentModel;
Ensure that our MyContro is inherited from WebControl.
Public class MyControl: WebControl
Next we will rewrite the following two functions, which are required.
Protected override void Render (HtmlTextWriter writer)
Protected override void CreateChildControls ()
Modify the first function as the following code:
Protected override void Render (HtmlTextWriter writer)
{
This. CreateChildControls ();
Base. Render (writer );
}
Modify the second function as the following code:
Protected override void CreateChildControls ()
{
// Clear the existing child Control and Its ViewState
This. Controls. Clear ();
This. ClearChildViewState ();
// Generate control tree
// Generate an environment table (one row, two cells)
Table myTable = new Table ();
// Build the table row to generate rows in the table
TableRow row = new TableRow ();
MyTable. Rows. Add (row );
// Generate Cells