web控件开发系列(四) 自定义控件属性(下)
控件在WEB開發時經常要用到,雖然有部分已經存在工具箱里,但有時總需要根據自己的要求,開發一些合適自己的控件。接上一節,已經說過了控件的屬性, 例如,我們需要一組屬性的集合時,這時我們需要用到的就是復雜屬性了,簡單的屬性滿足不了我們的要求,例如:大家熟悉的字體信息設置那欄。下面為大家介紹一下實現的幾種代碼與注意細節
一、連字符形式的復雜屬性標記
<asp:Button ID="Button1" runat="server" Font-Bold="True" Font-Italic="True" Font-Names="Arial" Font-Overline="True" Font-Size="20pt" Text="Button" />
例如上面就是一個Font的復雜屬性,通過這個屬性可以設置一系列相關的值。
在ServerControl中添加一個類ComplexAttribute,然后輸入下面代碼:
using?System;
using?System.Collections.Generic;
using?System.Linq;
using?System.Text;
using?System.Web;
using?System.Web.UI;
using?System.Web.UI.WebControls;
using?System.ComponentModel;
namespace?ServerControl
{
????[DefaultProperty("Text")]
????[ToolboxData("<{0}:ComplexAttribute?runat=server></{0}:ComplexAttribute>")]
????public?class?ComplexAttribute:?WebControl?
????{
????????private?Student?_student;
????????public?ComplexAttribute()
????????{
????????????_student?=?new?Student();
????????}????????
????????[Description("Student屬性")]
????????[Category("Student屬性")]
????????[PersistenceMode(PersistenceMode.Attribute)]???//指定如何將服務器控件屬性或事件保持到ASP.NET頁面的元數據屬性
????????[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]??//指定屬性是否以及如何在代碼中序列化
????????[TypeConverter(typeof(ExpandableObjectConverter))]??//指定用作此特性所綁定到的對象的轉換器的類型
????????[NotifyParentProperty(true)]
????????public?Student?Student
????????{
????????????get?
????????????{
????????????????if?(_student?==?null)
????????????????{
????????????????????_student?=?new?Student();
????????????????}
????????????????return?_student;
????????????}
????????}
????????///?<summary>
????????///?輸出控件
????????///?</summary>
????????///?<param?name="writer"></param>
????????protected?override?void?Render(HtmlTextWriter?writer)
????????{
????????????writer.Write("姓名:");
????????????writer.Write(_student.Name);
????????????writer.WriteBreak();
????????????writer.Write("年齡:");
????????????writer.Write(_student.Age.ToString());
????????????writer.WriteBreak();
????????????writer.Write("分數:");
????????????writer.Write(_student.Cost.ToString());
????????????writer.WriteBreak();
????????}
????}
????///?<summary>
????///?密封類,封裝文本的字體屬性。無法繼承此類。
????///?</summary>
????public?sealed?class?Student
????{
????????private?string?_Name?=?"Name";
????????private?int?_Age?=?0;
????????private?int?_Cost?=?0;
????????[NotifyParentProperty(true)]???//這個是為了實現在屬性窗口中更新屬性值時將通知其父屬性,不然修改了屬性窗口的值,但不通知你屬性,等于沒修改
????????public?string?Name
????????{
????????????get?{?return?_Name;?}
????????????set?{?_Name?=?value;?}
????????}
????????[NotifyParentProperty(true)]
????????public?int?Age?
????????{
????????????get?{?return?_Age;?}
????????????set?{?_Age?=?value;?}
????????}
????????[NotifyParentProperty(true)]
????????public?int?Cost?
????????{
????????????get?{?return?_Cost;?}
????????????set?{?_Cost?=?value;?}
????????}
????}
}
注意細節,不然你會感覺到很多地方不明白:
1、Student類,在設計時最好封裝起來,添加密封限制,讓這個類無法繼承。
2、Student類的屬性添加特性[NotifyParentProperty(true)]
3、構造函數ComplexAttribute時初始化Student類,不然設計器中你的控件會出現未引用對象的錯誤提示。
4、ComplexAttribute類的屬性添加特性[PersistenceMode(PersistenceMode.Attribute)]?? //指定如何將服務器控件屬性或事件保持到ASP.NET頁面的元數據屬性
5、ComplexAttribute類的屬性添加特性[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]? //指定屬性是否以及如何在代碼中序列化
6、ComplexAttribute類的屬性添加特性[TypeConverter(typeof(ExpandableObjectConverter))]? //指定用作此特性所綁定到的對象的轉換器的類型
7、ComplexAttribute類的屬性添加特性[NotifyParentProperty(true)]
PersistenceMode詳解:
指定如何將服務器控件屬性或事件保持到ASP.NET頁面的元數據屬性,共存在4種枚舉設置方式:
1、PersistenceMode(PersistenceMode.Attribute)指定屬性或事件保持為屬性;
2、PersistenceMode(PersistenceMode.EncodedInnerDefaultProperty)指定屬性作為服務器控件的唯一內部文本,如果屬性值是HTML編碼的,只能對字符串作這種指定;
3、PersistenceMode(PersistenceMode.InnerDefaultProperty)指定屬性在服務器控件中保持為內部文本,還指示將該屬性定義為元素的默認屬性,只能指定一個屬性為默認屬性;
4、PersistenceMode(PersistenceMode.InnerProperty)指定屬性在服務器控件中保持為嵌套標記,通常用于復雜對象,它們具有自己的持久性屬性。
DesignerSerializationVisibility詳解:
指定屬性是否以及如何在代碼中序列化,其值為DesignerSerializationVisibility的枚舉值,存在3種設置方式:
1、DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)指定序列化程序不應該序列化屬性值;
2、DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)指定應該允許序列化程序序列化屬性的值;
3、DesignerSerializationVisibility(DesignerSerializationVisibility.Content)指定序列化程序應該序列化屬性的內容,而不是屬性本身。此字段為只讀。Visible為其默認值。
編譯,在頁面上拖進控件,再看一下屬性窗口,你自己定義的屬性就在里面了,如下圖:
設置Student屬性,到頁面代碼視圖,就會出現下面的代碼:
?<cc1:ComplexAttribute ID="ComplexAttribute1" runat="server" Student-Age="10" Student-Cost="95" Student-Name="ASP.NET" />
二、內部嵌套復雜屬性標記
<asp:GridView ID="GridView1" runat="server">
??? <RowStyle BackColor="#EFF3FB" />
</asp:GridView>
例如上面的代碼,BackColor屬性RowStyle是內部嵌套。
在ServerControl中添加一個類InRowAttribute然后輸入下面代碼:
using?System;
using?System.Collections.Generic;
using?System.Drawing;
using?System.Linq;
using?System.Text;
using?System.Web;
using?System.Web.UI;
using?System.Web.UI.WebControls;
using?System.ComponentModel;
namespace?ServerControl
{
????[DefaultProperty("Text")]
????[ToolboxData("<{0}:InRowAttribute?runat=server></{0}:InRowAttribute>")]
????[ParseChildren(true),?PersistChildren(false)]
????public?class?InRowAttribute?:?CompositeControl
????{
????????private?Style?_Style;
????????public?InRowAttribute()
????????{
????????????_Style?=?new?Style();
????????}
????????[PersistenceMode(PersistenceMode.InnerProperty)]
????????[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
????????[NotifyParentProperty(true)]
????????[Category("復雜屬性")]
????????[Description("復雜屬性——內部嵌套形式")]
????????public?Style?BackColorStyle
????????{
????????????get
????????????{
????????????????if?(_Style?==?null)
????????????????{
????????????????????_Style?=?new?Style();
????????????????}
????????????????return?_Style;
????????????}
????????}
????????///?<summary>
????????///?輸出控件
????????///?</summary>
????????///?<param?name="writer"></param>
????????protected?override?void?Render(HtmlTextWriter?writer)
????????{
????????????writer.Write(_Style.BackColor.ToString());
????????}
????}
????[TypeConverter(typeof(ExpandableObjectConverter))]??
????public?class?Style
????{
????????private?Color?_BackColor;
????????[NotifyParentProperty(true)]
????????public?Color?BackColor
????????{
????????????get?{?return?_BackColor;?}
????????????set?{?_BackColor?=?value;?}
????????}
????}
}
編譯后在設計器中拖進這個控件,在屬性窗口設置值,在代碼視圖中可以看到下面的代碼
<cc1:InRowAttribute ID="InRowAttribute1" runat="server">
??? <BackColorStyle BackColor="ActiveBorder"></BackColorStyle>
</cc1:InRowAttribute>
屬性定定義方面還有好多類型,例如我們常用的ListItem組合, CheckBox組合, 或自己需要時定義的邊框線條,邊框大小,顏色等等,這些都是大家在開發中慢慢摸索(舉一反百)吧,如果大家有好的控件,不防也與我分享一下。
轉載于:https://www.cnblogs.com/whtydn/archive/2009/09/15/1566818.html
總結
以上是生活随笔為你收集整理的web控件开发系列(四) 自定义控件属性(下)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android MTK PDAF流程
- 下一篇: Kinect for Windows S