Custom personalized Web composite control

xiaoxiao2021-03-06  73

Author: feic .net provides a variety of controls for everyone, enough colorful. 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.io; using system.drawing; using system.web.ui.webControls; using system.com

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 is 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 control and its viewState this.controls.clear (); this.clearchildViewState (); // Generate control tree // Generate a environment table (one line, two cells) Table myTable = new table (); // build the table = new tablerow row = new tableRow (); myTable.Rows.add (row); // Generate cell 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 = "Times New Roman"; myLinkButton.ToolTip = "Good mysterious ah!"; myLinkButton.Text = "please me"; Mycell.controls.add (myLinkButton); row.cells.add (mycell); controls.add (t);} Write your own event method

Private void mylinkbutton_click (object sender, system.eventargs e) {page.response.write ("If you want to do something, write something, it is 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.

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

New Post(0)