Several Methods for setting custom tagprefix for ASP. NET Server Control

Source: Internet
Author: User

These two days I read "not far from the road ----- in-depth analysis of ASP. net2.0 control development. After reading the second chapter, I want to summarize "set custom ASP. NET Server Control tagprefix several methods, for future reference, the following code written controls as an example, because the focus is not the control writing, so write a very simple control, it is called emailinput

1 using system; 2 using system. collections. generic; 3 using system. componentmodel; 4 using system. text; 5 using system. web; 6 using system. web. ui; 7 using system. web. UI. webcontrols; 8 9 namespace servercontrol10 {11 [toolboxdata ("<{0}: emailinput runat = Server> </{0}: emailinput>")] 12 public class emailinput: compositecontrol13 {14 protected comment _ regvalidator; 15 protected comment _ rqrvalidatator; 16 protected textbox _ input; 17 protected override htmltextwritertag tagkey18 {19 get20 {21 return htmltextwritertag. div; 22} 23} 24 protected override void createchildcontrols () 25 {26 controls. clear (); 27 _ INPUT = new Textbox (); 28 _ input. id = "txtemail"; 2930 _ rqrvalidatator = new requiredfieldvalidator (); 31 _ rqrvalidatator. id = "rqvemail"; 32 _ rqrvalidatator. errormessage = "enter email address"; 33 _ rqrvalidatator. TEXT = "*"; 34 _ rqrvalidatator. display = validatordisplay. dynamic; 35_rqrvalidatator. controltovalidate = _ input. ID; 3637 _ regvalidator = new regularexpressionvalidator (); 38 _ regvalidator. id = "revemail"; 39 _ regvalidator. display = validatordisplay. dynamic; 40_regvalidator. errormessage = "invalid email format"; 41 _ regvalidator. TEXT = "*"; 42 _ regvalidator. validationexpression = @ "\ W + ([-+. '] \ W +) * @ \ W + ([-.] \ W + )*\. \ W + ([-.] \ W +) * "; 43 _ regvalidator. controltovalidate = _ input. ID; 4445 this. controls. add (_ INPUT); 46 this. controls. add (_ rqrvalidatator); 47 This. controls. add (_ regvalidator); 4849 childcontrolscreated = true; 50} 51} 52}

Method 1: Configure in Web. config. Add the following code in the system. web section of the web. config file:

Code1<pages>2  <controls>3     <add assembly="ServerControl" namespace="ServerControl" tagPrefix="sc"/>4  </controls>5</pages>

Note: Assembly indicates the name of the Assembly where the control is located, namespace indicates the control namespace, and tagprefix indicates the items we want to set.

Method 2: add the <% register %> command on the page using the control, as shown below:

Code1<%@ Register assembly="ServerControl" namespace="ServerControl" tagprefix="sc" %>

Note: Assembly indicates the name of the Assembly where the control is located, namespace indicates the control namespace, and tagprefix indicates the items we want to set.

Method 3: Specify the tagprefix by decorating the attribute. For example, add the following attribute to the control class:

 
Code[assembly: TagPrefix("ServerControl", "scsc")]namespace ServerControl{    [ToolboxData("<{0}:EmailInput runat=server></{0}:EmailInput>")]    public class EmailInput : CompositeControl    {

Note: even if this is defined, the <% register %> command must be added to each page. This provides a uniform tagprefix, which is essentially the second method.

Method 4: Add the following code to the property file "assembly. cs" in the control project to customize the control Prefix:

Codeusing System.Reflection;using System.Runtime.CompilerServices;using System.Runtime.InteropServices;using System.Web.UI;[assembly: TagPrefix("ServerControl","scsc")]

Note: even if this is defined, the <% register %> command must be added to each page. This provides a uniform tagprefix, which is essentially the second method.

 

The above four methods are described as follows:

  • Custom in Web. config is an application-level definition. It can be valid for the entire application only once. The other three methods must add the <% register %> command on the page that references the control.
  • Asp.net defines the detection sequence of tagprefix as assemblyinfo. CS ----> attribute decoration in the control -----> Configuration in webconfig. That is to say, if you have set these methods, Asp.net will adopt the webconfig setting method! I believe this is also a simple principle of Asp.net coverage!
  • Asp.net's built-in control adopts the first method, that is, it is configured in Web. config to configure tagprefix to ASP. If you look uncomfortable, you can get rid of it.

 

 

The above is purely a reading experience. please correct me if you have any mistakes!

Related Article

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.