Control Development Learning notes (1) -- ToolboxData meaning

Source: Internet
Author: User

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

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.