Create an ASP.NET Web Custom Control - RRR 1
Author: Qing Gu Gang prettywolf@vip.sina.com
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
When we write an application with ASP.NET, we often need to pop up a [OK] [Cancel] confirmation box when you submit, 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.
(Routine uses C # language)
1. New Project
First open Visual Studio.net, 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. Edit code
Open 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 "
The following is further modified, delete the original code:
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 icons that are displayed in the toolbox
Select Menu [Project] / [Add New Item] Select Create "Bitmap File" in the pop-up dialog box and change the file name to "confirmbutton" (very important, bitmap file name must be consistent). 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.