ASP.NET 2.0服务器控件与组件开发高级编程:为自定义控件添加设计时属性

53 篇文章 0 订阅

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),以便对这些属性定义上文所述的信息。例如,考虑如下代码:

  1. [BrowsableAttribute(true)] //设计时可见
  2. [DescriptionAttribute("Gets and sets the payment method")] //控件描述,在属性窗口最下端显示
  3. [DefaultValueAttribute("Payment Method")] //缺省值
  4. [CategoryAttribute("Appearance")] //属性归类
  5. public virtual string PaymentMethodText
  6. {
  7.   get { return this.paymentMethodText; }
  8.   set { this.paymentMethodText = value; }
  9. }

这些代码实现了以下动作。

(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作为默认属性:

  1. [DefaultPropertyAttribute("CardholderNameText")]
  2. public class CreditCardForm2 : Control

当页面开发人员将CreditCardForm2控件从工具箱拖放到设计窗口时,设计器将自动将以下代码添加到相关ASP.NET Web页面中:

  1. <cc1:CreditCardForm2 ID="CreditCardForm2" runat="server"/>

以下代码针对CreditCardForm2自定义控件应用类层次属性(Attribute)ToolboxData- Attribute,这样可设置CreditCardForm2控件的多个属性的默认值:

  1. [ToolboxData("<{0}:CreditCardForm2 PaymentMethodText='Payment Options' CreditCardNoText='Credit Card Number' CardholderNameText='Cardholder Full Name' SubmitButtonText = 'Send' runat='server'></{0}:CreditCardForm2>")]
  2. public class CreditCardForm2 : Control

属性(Attribute)指示设计器当页面开发人员将CreditCardForm2控件从工具箱拖放到设计器窗口时,将以下代码添加到.aspx页面中:

  1. <ccl:CreditCardForm2 runat='server' CardholderNameText='Cardholder Full Name' 
  2. CreditCardNoText='Credit Card Number' 
  3. PaymentMethodText='Payment Options' SubmitButtonText='Send'>
  4. </ccl:CreditCardForm2>

2.5.3  程序集层次属性(Attribute)

正如所介绍的,当页面开发人员将CreditCardForm2控件从工具箱拖放到设计器窗口时,设计器会将以下代码添加到.aspx文件中:

  1. <%@ Register Assembly="CustomComponents" Namespace="CustomComponents" 
  2. TagPrefix="cc1" %>
  3. ...
  4. <cc1:CreditCardForm2 ID="CreditCardForm2" runat="server"/>

注意,设计器使用cc1作为前缀。可以通过向AssemblyInfo.cs文件添加(以下)程序集层次属性(Attribute)来指示设计器使用“custom”作为标记前缀:

  1. using System.Web.UI;
  2. [assembly: TagPrefix("CustomComponents","custom")]

另外,设计器将使用自定义前缀替代ccl:

  1. <%@ Register Assembly="CustomComponents" Namespace="CustomComponents" 
  2. TagPrefix="custom" %>
  3. ...
  4. <custom:CreditCardForm2 ID="CreditCardForm2" runat="server"/>

如果使用命令行生成程序集,则必须创建AssemblyInfo.cs文件,向该文件添加如下代码,并将该文件移动到CreditCardForm2.cs文件所在的目录:

  1. using System.Reflection;
  2. using System.Runtime.CompilerServices;
  3. using System.Web.UI;
  4. [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

 

http://book.csdn.net/bookfiles/337/10033713399.shtml

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值