.NET provides you with a variety of controls, and it is also rich enough. But sometimes you want to develop some of your own controls to increase development efficiency, or achieve some special features.
Below I tell, build a process of the most basic Web composite control.
First create a C # class library project, get your name MYControl. Delete the default class file Class1 and build our own class file mycontrol.cs.
Now we add a password to it.
First modify it to this case on the code that has just been generated.
We will use the following quote:
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
Below we will rewrite the following two functions, this is a must
Protected Override Void Render (HTMLTextWriter Writer)
Protected Override Void CreateChildControls ()
Modify the first function to the following code
Protected Override Void Render (HTMLTextWriter Writer)
{
THIS.CREATECHILDCONTROLS ();
Base.render (Writer);
}
Modify the second function to the following code
Protected Override Void CreateChildControls ()
{
/ / Clear existing child controls and their viewState
THIS.CONTROLS.CLEAR ();
THIS.CLEARCHILDVIEWSTATE ();
/ / Generate control tree
/ / Generate a environment table (one line, two cells)
Table myTable = new table ();
// build the table row generates lines in the table
TableRow Row = New TableRow ();
MyTable.Rows.Add (Row);
/ / Generate cells
Tablecell mycell = new TableCell ();
// Used to generate the code of the link button navigation bar. Each button displays a webdings character, which can be disabled as needed and bound to the internal Click event handler.
LinkButton MyLinkButton = New LinkButton ();
MyLinkButton.id = "MyLinkButton";
MyLinkButton.Click = new eventhandler (MyLinkButton_Click);
MyLinkButton.font.name = "Song";
MYLINKBUTTON.TOOLTIP = "Good Xuan!";
MyLinkButton.Text = "Please click me";
Mycell.controls.add (MyLinkButton);
Row.cells.Add (mycell);
Controls.add (t);
}
Let's write your own event method.
Private vid mylinkbutton_click (Object Sender, System.Eventargs E)
{
Page.Response.write ("If you want to do something, write something, it's so simple!");
}
Let's compile it, then click Tool - Add / Remove Toolbox Number
Then browse to your compile generation DLL, now what have you seen in the toolbox? Drag MyControl to your own Webform look at the effect.
OK is as simple as it is.
Ok, I will write here if I don't have much time.
We will continue to discuss this expansion of this custom control. But smart, you should also extend a lot of functions.