Create an ASP. NET Server Control Project. Visual Studio automatically generates the following code:
Namespace WebControlLibrary
{
[DefaultProperty ("Text")]
[ToolboxData ("<{0}: HelloWorld runat = server> </{0}: HelloWorld>")]
Public class HelloWorld: WebControl
{
[Bindable (true)]
[Category ("Appearance")]
[DefaultValue ("")]
[Localizable (true)]
Public string Text
{
Get
{
String s = (String) ViewState ["Text"];
Return (s = null )? "[" + This. ID + "]": s );
}
Set
{
ViewState ["Text"] = value;
}
}
Protected override void RenderContents (HtmlTextWriter output)
{
Output. Write (Text );
}
}
}
When I first started to Develop ASP. NET controls and components, my 1st question was: What are the meanings of DefaultProperty and ToolBoxData?
[DefaultProperty ("Text")]
[ToolboxData ("<{0}: HelloWorld runat = server> </{0}: HelloWorld>")]
DefaultProperty is the default property of the control.
ToolBoxData refers to the definition of the control added to the HTML code of the aspx file when you drag the control from the Toolbox into WebForm.
What does {0} mean?
The prefix www.2cto.com
When you drag this control to the page
It automatically adds <{0}: HelloWorld runat = server> </{0}: HelloWorld>, which is defined by you.
How exactly is {0} defined?
Modify the automatically generated code of Visual Studio and add the following line
[Assembly: TagPrefix ("WebControlLibrary", "aspNet")]
Namespace WebControlLibrary
{
// Other codes remain unchanged
}
Regenerate the control project.
Create a new website, drag the newly created control to the new Web form, and switch to the Source view status. The control definition is marked:
<AspNet: HelloWorld ID = "HelloWorld1" runat = "server"/>
The aspNet is defined by [assembly: TagPrefix ("WebControlLibrary", "aspNet.
At the same time, you can see the following mark in the upper part of the page:
<% @ Register assembly = "WebControlLibrary" namespace = "WebControlLibrary" tagprefix = "aspNet" %>
Check the basic usage of TagPrefixAttribute in msdn:
Public TagPrefixAttribute (
String namespaceName,
String tagPrefix
)
Of course, you can also modify the Web. config file of the website by customizing the TagPrefix of the ASP. NET Server Control:
<Controls>
<Add tagPrefix = "asp" namespace = "System. Web. UI" assembly = "System. Web. Extensions, Version = 3.5.0.0, Culture = neutral, PublicKeyToken = 31BF3856AD364E35"/>
<Add tagPrefix = "asp" namespace = "System. web. UI. webControls "assembly =" System. web. extensions, Version = 3.5.0.0, Culture = neutral, PublicKeyToken = 31BF3856AD364E35 "/>
<Add assembly = "WebControlLibrary" namespace = "WebControlLibrary" tagPrefix = "AspNet"/>
</Controls>
In this way, when you drag the control to the page, the tagPrefix of the control is AspNet:
<AspNet: HelloWorld ID = "HelloWorld1" runat = "server"/>
The cold Sand Island is lonely.
From Lonely sandcontinent