Applying Design-Time Attributes


Although the PrimeGenerator control works as expected on an .aspx page, you will find that if you use it in a visual designer such as Visual Studio. NET (which we'll demonstrate in the next section), it lacks some standard features that are expected at design time. For example, you might expect the Number property to be automatically highlighted when a page developer clicks the control on the design surface. To get this and other design-time features, you have to provide metadata that the designer uses to improve the design-time experience. The .NET Framework enables metadata to be supplied for a class and its members through a feature known as attributes . (We described metadata attributes in Chapter 3.) We have marked PrimeGenerator with design-time attributes (in square brackets) in the following code:

 usingSystem;  usingSystem.ComponentModel;  usingSystem.Web.UI; namespaceMSPress.ServerControls{  [ DefaultProperty("Number"), ToolboxData("<{0}:PrimeGeneratorNumber=\"15\" runat=\"server\">" +  "</{0}:PrimeGenerator>") ]  publicclassPrimeGenerator:Control{  publicintNumber{  } protectedoverridevoidRender(HtmlTextWriterwriter){  } } } 

DefaultPropertyAttribute specifies the default property for the control, and ToolboxDataAttribute specifies the tag that is written to the page when the control is dragged from the toolbox onto the design surface. ToolboxDataAttribute is optional; if this attribute is not applied, the designer generates a standard tag to the page.

We will discuss design-time attributes in more detail in Chapter 15, "Design-Time Functionality." Appendix A, "Metadata Attributes," provides information about the syntax and usage of attributes commonly applied to controls. In this chapter, we'll describe only one attribute ” TagPrefixAttribute ” in more detail because it is especially important at design time.

Applying TagPrefixAttribute

TagPrefixAttribute is an assembly-level attribute that provides a tag prefix that a designer uses for a control when it is dragged from the toolbox onto the design surface. If this attribute is not applied, the designer generates a default tag prefix such as "cc1" on the page. Because TagPrefixAttribute is an assembly-level attribute, it is not applied directly to any particular control; instead, it is declared in a separate file that is compiled into the same assembly as the control.

The TagPrefixAttribute attribute is already applied to the sample controls in the book's sample files. If you are re-creating the controls in this chapter to walk through the steps for compilation and deployment, you can apply this attribute as described next.

To apply TagPrefixAttribute in Visual Studio .NET, edit the AssemblyInfo.cs file in your project to include the following lines of code and rebuild your control project:

 usingSystem.Web.UI; [assembly:TagPrefix("MSPress.ServerControls", "msp")] 

The tag prefix just shown associates the msp tag prefix with the namespace MSPress.ServerControls so that the tag prefix is generated when a user drags and drops your controls from the toolbox onto a page.

If you are building from the command line, create a file named AssemblyInfo.cs and place it in the directory that contains the source files for your controls (for example, C:\MyBookCodeCs\MyServerControls). This file does not necessarily have to be named AssemblyInfo.cs, but for consistency with Visual Studio .NET projects, we recommend that you give it this name .

Add the following code to AssemblyInfo.cs, and save the file:

 //AssemblyInfo.cs usingSystem.Reflection; usingSystem.Runtime.CompilerServices;  usingSystem.Web.UI;  // //Generalinformationaboutanassemblyiscontrolled //throughassembly-levelattributes. //TheTagPrefixAttributeassociatesatagprefixwitha //namespace.Ifyouhavemultiplecontrolnamespacesinasingle //assembly,youwillneedaTagPrefixAttributecorresponding //toeachnamespace.  [assembly:TagPrefix("MSPress.ServerControls", "msp")]  

Recompile the code for your controls and for AssemblyInfo.cs into an assembly using the same command that you used earlier:

 csc/t:library/out:MSPress.ServerControls.dll/r:System.dll /r:System.Web.dllSimpleControl.csPrimeGenerator.cs StyledPrimeGenerator.csAssemblyInfo.cs 

See the instructions outlined earlier in the chapter in the section "Compiling and Deploying a Server Control" to make sure that you do not overwrite the assembly generated by the batch file you downloaded with the book's sample files.

The Register directive with the tag prefix specified in the TagPrefixAttribute is generated only when the control is dragged and dropped onto a page in the designer. If you author pages in a text editor outside the designer, you must manually add the Register directive. The next section shows how the TagPrefixAttribute is used by a visual designer such as Visual Studio .NET.



Developing Microsoft ASP. NET Server Controls and Components
Developing Microsoft ASP.NET Server Controls and Components (Pro-Developer)
ISBN: 0735615829
EAN: 2147483647
Year: 2005
Pages: 183

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net