Create an ASP. net web custom control -- routine 1

Source: Internet
Author: User

WEB custom control programming is a difficult part of ASP. NET programming. In particular, complicated controls require some technical skills that are not commonly used.

Based on some of my practical experience, I will introduce this technology to readers.

Simple inheritance control: ConfirmButton

We are using ASP. when writing an application, you often need to pop up a [OK] [Cancel] confirmation box when submitting the button to prevent users from submitting the application by mistake during operations. The traditional method to implement this function is to add the button Attributes in the Page_Load event of the code page, but it is troublesome to add each button. Next we will create a button with such a function to solve this problem.

(The routine uses the C # language)

1. Create a project

First Open Visual Studio.net and create a new Web control library project named TestLib. In Solution Explorer, a source code file identified as WebCustomControl1.cs is displayed and renamed as ConfirmButton. cs.

2. Edit the code

Open ConfirmButton. cs source file, change the class name "WebCustomControl1" to "ConfirmButton", and inherit the class from "System. web. UI. webControls. change "WebControl" to "System. web. UI. webControls. button ";

Run the code "[DefaultProperty (" Text "),

ToolboxData ("<{0}: WebCustomControl1 runat = server ></ {0}: WebCustomControl1>")]"

Change to "[DefaultProperty (" Text "),

ToolboxData ("<{0}: ConfirmButton runat = server> </{0}: ConfirmButton>")] ", in this way, the xml Code ID of the control displayed on the aspx page is displayed as "<cc0: ConfirmButton…> ... </PC3: ConfirmButton> ".

The following code is further modified to delete the original code:

Private string text;

[Bindable (true ),

Category ("Appearance "),

DefaultValue ("")]

Public string Text

{

Get

{

Return text;

}

Set

{

Text = value;

}

}

Add 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 displayed in the toolbox

Select the menu [project]/[add new project], select create "bitmap file" in the pop-up dialog box, and change the file name to "ConfirmButton "(Important, The bitmap file name must be consistent with the class name ). Select the bitmap file in solution Resource Manager and set the value of "generate operation" to "embedded resource" in the Property setting box ".

 

Okay. compile it. Everything is okay. The rest is to find the compiled dll file and add it to the toolbox, which can be used in future WEB applications.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.