{infiniteZest}
// Articles. Tutorials. Utilities.
Home  |   Search  |   Login  
Categories Skip Navigation Links
New / All
AJAX
Apple
ASP.NET
.NET
Git
Google / Android
Python / IronPython
Miscellaneous
SQL Server
How to change a custom control’s Tag Prefix from cc1?
Summary
This article looks at where the TagPrefix cc1 comes from in the @Register directive. Also looks the ToolboxData attribute which serves as the template for the custom control declaration on a page.
 
Table of Contents

Default behavior

Figure 1. Tag prefixes inside the page and the custom control

Changing Prefix Manually

Set a meaningful prefix with the help of assembly level attribute

 

Let’s say you have created a custom control and added it to the toolbox and ready to use it on a page (Or, let’s say, you created a custom control and a page developer has added it to the toolbox and is getting ready to use it on a page).

Default behavior

If the page developer drags the custom control and drops it on a page, it would add two lines like the following:

At the top of the page, the custom control will be registered with the @Register directive. In the @Register directive, you will see the TagPrefix set to cc1. This is the default value that stands for custom control 1.

<%@ Register Assembly="AJAXWrite" Namespace="AJAXWrite" TagPrefix="cc1" %>

In the body of the page, you will see the actual declarative placement of the AJAXWrite custom control. Here you will see the prefix cc1 again.

<cc1:AJAXWrite ID="AJAXWrite1" runat="server" />

The above declaration comes from the class level attribute you set on the custom control class.

[ToolboxData("<{0}:AJAXWrite runat=server></{0}:AJAXWrite>")]
public class AJAXWrite : WebControl, IPostBackDataHandler

The attribute ToolboxData is the template for the declaration you see in the source window of the page. In this case, the name of the custom control is AJAXWrite and the attribute ToolboxData is right above it.

Now, you want to change cc1 to something more meaningful. Since this control is called AJAXWrite, the prefix could be something like ajw. There are two ways of changing this:

- Change the prefix manually on the page

- Change it with an assembly level attribute

Figure 1. Tag prefixes inside the page and the custom control

Figure 1. Tag prefixes inside the page and the custom control

Changing Prefix Manually

Go into the source window of the page (e.g. default.aspx) and manually change the TagPrefix from cc1 to ajw.

<%@ Register Assembly="AJAXWrite" Namespace="AJAXWrite" TagPrefix="ajw" %>

<ajw:AJAXWrite ID="AJAXWrite1" runat="server" />

Don’t forget to change prefix at both the places: in the @Register directive and in the actual declaration. Otherwise, you will get the following error: Unknown server tag.

The advantage of this approach is that the page developers can change this prefix to whatever the string that makes sense to them.

Set a meaningful prefix with the help of assembly level attribute

Another approach is to have meaningful TagPrefix set in the custom control code by the control developer. This prefix can still be changed by the page developer, but it would at least be a bit more meaningful (it won’t be cc1).

This can be achieved by using an assembly-level attribute: TagPrefix.

[assembly: TagPrefix("AJAXWrite", "ajw")]

namespace AJAXWrite
{

In the above example, the AJAXWrite namespace contains the custom control AJAXWrite. So, the TagPrefix attribute added above that takes the namespace and tag prefix as the parameters.

With the above attribute in the custom control, if you drag-drop the AJAXWrite custom control from the toolbox, the default tag prefix used on the page will be ajw, as shown below:

<%@ Register Assembly="AJAXWrite" Namespace="AJAXWrite" TagPrefix="ajw" %>
<ajw:AJAXWrite ID="AJAXWrite1" runat="server" />

Bookmark and Share This

More Articles With Similar Tags
icon-ajaxservercontrol-new-project.jpg
This article explains the various pieces of the code generated for beginning an AJAX Server Control. The class ScriptControl and the over-ridden methods GetScriptDescriptors() and GetScriptReferences() are explained, among others.
icon-ajaxservercontrol-toolbox.jpg
This article talks about using an AJAX Server Control from a web site. Registering the control and adding a ScriptManager are also discussed.
This test has questions about using an AJAX Server Control from a web site. Registering the control and adding a ScriptManager are also included.
This is a test on the various pieces of the code generated for an AJAX Server Control. This includes the class ScriptControl and the over-ridden methods GetScriptDescriptors() and GetScriptReferences().
icon-ajaxservercontrol-client-files.jpg
With an AJAX Server control, some additional JavaScript is generated behind the scenes (in addition to the JS you write). This article looks at this generated JavaScript when an AJAX Server Control is used.
About  Contact  Privacy Policy  Site Map