Asp.net custom control development for Alibaba Cloud (2)-TagPrefix label

Source: Internet
Author: User

  • Summary

We have previously made a vest of the simplest TextBox. In this article, we will talk about the labels of custom controls. You may see that the Code dragged in the previous article is

<C0: TextEdit ID = "TextEdit1" runat = "server"> </PC3: TextEdit>. This is definitely not the expected result. The label "PC3" is not well named, it also makes people feel "too watery". Now let's improve the code.

  • Modify code
 System.ComponentModel;,)]     

In fact, we only need to add one line of code. Let's take a look at the code at the UI Layer.

<% @ Page Language = AutoEventWireup = CodeBehind = Inherits = %> <! -- This line of code is automatically generated --> <% @ Register Assembly = Namespace = TagPrefix = %> <! DOCTYPE html PUBLIC> 

We found that the label is still, and we will see the above <% @ Register Assembly = Namespace = TagPrefix = %>
TagPrefix = "PC3", we only need to change TagPrefix = "PC3" to TagPrefix = "XYB". Now let's drag another control to see it.

<% @ Page Language = AutoEventWireup = CodeBehind = Inherits = %> <! -- This line of code is automatically generated --> <% @ Register Assembly = Namespace = TagPrefix = %> <! DOCTYPE html PUBLIC> 

Now, what has the label changed? <XYB: TextEdit ID = runat => </XYB: TextEdit>. Now we have changed, but let's take a closer look at our best practices. In the future, we will expand this class library, which encapsulates many classes. Do we add a sentence on every page?

[Assembly: TagPrefix ("XYB. controls "," XYB], <% @ Register Assembly = "XYB. controls "Namespace =" XYB. controls "TagPrefix =" XYB "%>? This is obviously unscientific. We should not mention the difficulties in maintenance, but will be despised in the future, and there will still be a lot of repetitive work.

  • Modify configuration file

Modify the web. config file and add the following code under the <system. web> node:

<Pages controlRenderingCompatibilityVersion = "4.0" clientIDMode = "AutoID">
<Controls>
<Add namespace = "XYB. Controls" assembly = "XYB. Controls" tagPrefix = "XYB"/>
</Controls>
</Pages>

The entire configuration file code is as follows:

<?xml version= encoding=?><!--  --><configuration>    <system.web>        <compilation debug= targetFramework= />        <pages controlRenderingCompatibilityVersion= clientIDMode=>            <controls>                <add = assembly= tagPrefix=/>            </controls>        </pages>    </system.web></configuration>

Now we can comment out the code dragged in before TextEditUI. aspx, or delete it. Delete the [assembly: TagPrefix ("XYB. Controls", "XYB")] code in the TextEdit class. Regenerate.

The TextEdit. cs code is as follows:

 System.ComponentModel; 


The HTML code is as follows:

<% @ Page Language = AutoEventWireup = CodeBehind = Inherits = %> <! -- This line of code is automatically generated --> <% -- <% @ Register Assembly = Namespace = TagPrefix = %> -- %> <! DOCTYPE html PUBLIC> 

Let's drag a control in, and the label is changed.

<% @ Page Language = AutoEventWireup = CodeBehind = Inherits = %> <! -- This line of code is automatically generated --> <% -- <% @ Register Assembly = Namespace = TagPrefix = %> -- %> <! DOCTYPE html PUBLIC> 

So far, it is a success.

  • Next episode notice:

Render renders controls and discusses the relationships between Render, RenderControl, and RenderChilden.

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.