Use ASP. NET to create a custom text box

Source: Internet
Author: User

In. using self-created controls in applications of. NET can greatly enhance the functions of applications. You can add the desired attributes and behaviors on the basis of the original controls, and even create custom controls. In asp.net, we can add some client javascript Functions to some controls to reduce the number of times data is returned to the server each time when the page is submitted, thus improving the function and efficiency of the program. In this article, we will see how to use asp.net to create a custom text box control. when the focus is on the text box control and the control is left, the background color of the text box control changes accordingly. This control will include the following features:
1) when you enter data in the text box, the background color of the text box is displayed in a preset color. When the user's input focus leaves the text box, the background color of the original text box can be restored.
2) You can change the attributes of the custom control during vs.net design.
Next we will start to create the control step by step. First, create an empty vs.net solution and add an asp.net Project (named webapplication) and a web control library project (named ControLib ). Rename webform1.aspx in the asp.net project to container. aspx, and rename webmermercontrol1.cs in the web control library project to PimpedOutTextbox. cs.
Then, add the code to the pimpedouttextbox class. Because this is a WEB control library, VS. NET has introduced related class libraries. Because our application will use the color function, we introduce the plotting class.
Using System. Drawing;
Use the following code to replace the original predefined code.
Line 1: [assembly: TagPrefix ("ControlLib", "lib")]
Line 2: namespace ControlLib
Line 3 :{
Line 4: [DefaultProperty ("BackColorOn "),
Line 5: ToolboxData ("<{0}: PimpedOutTextbox runat = server> </{0}: PimpedOutTextbox>")]
Line 6: public class PimpedOutTextbox: System. Web. UI. WebControls. TextBox
Line 7 :{
The assembly attribute was added at the beginning to automatically add the Tagprefix control flag to vs.net when you drag the control in VS.net. Added several attributes in the pimedouttexbox class: defaultproperty and toolboxdata. the attribute backcoloron in defaultproperty indicates that when the control is removed from. when the toolbox of NET is dragged to the designer, the default properties of the control are selected. The Toolboxdata attribute is related to the [assembly: Tagprefix] attribute to show how the control is generated from the HTML view. These attributes are described in detail below.

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.