Create an ASP.NET Web Custom Control - RRR 1
Author: ovo http://damao.0538.org
Web Custom Control Programming is a relatively difficult part of ASP.NET programming, especially complex controls, requires some technical techniques that are commonly used. Because of some of my own experience, I will introduce the technique to the reader. Simple inheritance control: CONFIRMBUTTON We often need to pop up a [OK] [Cancel] confirmation box when you submit it, to prevent the user from being mistaken when the user is operated. Implementing this feature The traditional way is to add a button at the Page_Load event in the code page, but each button will add more troublesome. Below we are making a button with such a function to solve this problem. (Routines use C # language) 1. New projects first open Visual Studio.net to create a new web control library project, name TestLib. In the Solution Explorer, there will be a source code file identified as WebcustomControl1.cs, which is renamed as confirmButton.cs. 2. Editing code Opens the CONFIRMBUTTON.CS source file, change the class name "WebcustomControl1" to "confirmButton"; inherit the class from "System.Web.ui.WebControls.webControl" to "System.Web.ui.WebControls.Button"; Will code "[DefaultProperty (" text "),
ToolboxData ("<{0}: WebcustomControl1 runat = server> {0}: WebcustomControl1>")] "Change to" [DefaultProperty ("text"),
ToolBoxData ("<{0}: confirmbutton runt = server> {0}: confirmButton>") "", this control XML code identifier display "
PRIVATE STRING TEXT;
[Bindable (TRUE),
Category ("APPEARANCE"),
DEFAULTVALUE ("")]
Public String Text
{
get
{
Return TEXT;
}
set
{
TEXT = VALUE;
}
}
Add a new code (used to set the information displayed in the pop-up confirmation box):
Private string _confirmmessage = "is ok?";
[Bindable (TRUE),
Category ("APPEARANCE"),
DEFAULTVALUE ("is ok?")]]
Public String ConfirmMessage
{
get
{
Return_ConfirmMessage;
}
set
{
_confirmmessage = value;
}
}
Finally, PROTECTED OVERRIDE VOID RENDER (HTMLTEXTWRITER OUTPUT)
{
Output.write (text);
}
Change to Protected Override Void Render (HTMLTextWriter Output)
{
Base.attributes.add ("Onclick", "Return Confirm ('" this._confirmmessage ");"); Base.Render (Output);
}
3. Add the icon selection menu [Item] / [Add New Item] displayed in the toolbox, select Create "bitmap file" in the pop-up dialog box and change the file name to "confirmbutton" (very important, bitmap file name Requirements and class names). The bitmap file is then selected in the Solution Explorer, and set the value of the "Generation Action" to "Embedded Resources" in the Solution Explorer.
Ok, compile it, everything is OK. The rest is to find and add the compiled DLL file and add it to the toolbox, which can be used in the later web application.