ASP.NET 2.0服务器控件与组件开发高级编程
2.5 为自定义控件添加设计时属性
当编写自定义控件时,您必须谨记页面开发人员将在Visual Studio环境中使用自定义控件,他们希望服务器控件具有某些标准的行为和外观。例如,页面开发人员希望当他们选中一个属性浏览器中的属性时,能够看到一行相关说明。
必须用设计时属性注释自定义控件,这样控件才能够与Visual Studio环境中的标准ASP.NET服务器控件行为类似。
2.5.1 属性(Property)层次属性(Attribute)
当页面开发人员在Visual Studio中选中CreditCardForm2控件时,他们希望在属性浏览器中显示有关PaymentMethodText、CreditCardNoText、CardholderNameText、ExpirationDate Text和SubmitButtonText属性的如下信息。
● 属性名称和值。
● 每个属性都包含一行描述属性作用的说明。
● 每个属性的默认值。
● 每个属性的特定类别,这样页面开发人员可以在属性浏览器中查看属性的分组。
可以对这5个属性(Property)应用BrowsableAttribute、DescriptionAttribute、DefaultValueAttribute和CategoryAttribute等设计时属性(Attribute),以便对这些属性定义上文所述的信息。例如,考虑如下代码:
- [BrowsableAttribute(true)] //设计时可见
- [DescriptionAttribute("Gets and sets the payment method")] //控件描述,在属性窗口最下端显示
- [DefaultValueAttribute("Payment Method")] //缺省值
- [CategoryAttribute("Appearance")] //属性归类
- public virtual string PaymentMethodText
- {
- get { return this.paymentMethodText; }
- set { this.paymentMethodText = value; }
- }
这些代码实现了以下动作。
(1)对PaymentMethodText属性(Property)应用BrowsableAttribute(true)属性(Attribute)可指示属性浏览器显示该属性的名称和值。默认情况下,每个公共属性都可考虑使用BrowsableAttribute。对于只读属性(Property)应用该属性(Attribute)将指示属性浏览器不要显示属性名称和值,因为如果页面开发人员不能修改属性值,则显示属性信息就变得没有意义了。
(2)对属性(Property)应用DescriptionAttribute("Gets and sets the payment method")可指示属性浏览器每当页面开发人员选中该属性(Property)时,显示文本“Gets and sets the pay- ment method”。
(3)对属性(Property)应用DefaultValueAttribute("Payment Method")可指示属性浏览器将文本“Payment Method”作为属性(Property)的默认值显示。
(4)对属性(Property)应用CategoryAttribute("Appearance")可指示属性浏览器在外观类别中显示该属性(Property)。
2.5.2 类层次属性(Attribute)
当页面开发人员在Visual Studio中选择一个服务器控件时,他们希望看到一个高亮的特殊控件属性。该属性称为服务器控件的默认属性。以下代码对自定义控件CreditCardForm2应用了DefaultPropertyAttribute ("CardholderNameText"),以便将CardholderNameText作为默认属性:
- [DefaultPropertyAttribute("CardholderNameText")]
- public class CreditCardForm2 : Control
当页面开发人员将CreditCardForm2控件从工具箱拖放到设计窗口时,设计器将自动将以下代码添加到相关ASP.NET Web页面中:
- <cc1:CreditCardForm2 ID="CreditCardForm2" runat="server"/>
以下代码针对CreditCardForm2自定义控件应用类层次属性(Attribute)ToolboxData- Attribute,这样可设置CreditCardForm2控件的多个属性的默认值:
- [ToolboxData("<{0}:CreditCardForm2 PaymentMethodText='Payment Options' CreditCardNoText='Credit Card Number' CardholderNameText='Cardholder Full Name' SubmitButtonText = 'Send' runat='server'></{0}:CreditCardForm2>")]
- public class CreditCardForm2 : Control
属性(Attribute)指示设计器当页面开发人员将CreditCardForm2控件从工具箱拖放到设计器窗口时,将以下代码添加到.aspx页面中:
- <ccl:CreditCardForm2 runat='server' CardholderNameText='Cardholder Full Name'
- CreditCardNoText='Credit Card Number'
- PaymentMethodText='Payment Options' SubmitButtonText='Send'>
- </ccl:CreditCardForm2>
2.5.3 程序集层次属性(Attribute)
正如所介绍的,当页面开发人员将CreditCardForm2控件从工具箱拖放到设计器窗口时,设计器会将以下代码添加到.aspx文件中:
- <%@ Register Assembly="CustomComponents" Namespace="CustomComponents"
- TagPrefix="cc1" %>
- ...
- <cc1:CreditCardForm2 ID="CreditCardForm2" runat="server"/>
注意,设计器使用cc1作为前缀。可以通过向AssemblyInfo.cs文件添加(以下)程序集层次属性(Attribute)来指示设计器使用“custom”作为标记前缀:
- using System.Web.UI;
- [assembly: TagPrefix("CustomComponents","custom")]
另外,设计器将使用自定义前缀替代ccl:
- <%@ Register Assembly="CustomComponents" Namespace="CustomComponents"
- TagPrefix="custom" %>
- ...
- <custom:CreditCardForm2 ID="CreditCardForm2" runat="server"/>
如果使用命令行生成程序集,则必须创建AssemblyInfo.cs文件,向该文件添加如下代码,并将该文件移动到CreditCardForm2.cs文件所在的目录:
- using System.Reflection;
- using System.Runtime.CompilerServices;
- using System.Web.UI;
- [assembly: TagPrefix("CustomComponents","custom")]
接着,必须使用如下命令编译AssemblyInfo.cs和CreditCardForm2.cs文件:
csc /t:library /out:CustomComponents.dll /r:System.dll /r:System.Web.dll AssemblyInfo.cs CreditCardForm2.cs