内容载入中...
重写样式属性
样式属性的重载与其他属性的重载没有裁辞稹H欢谑迪止讨斜匦胱⒁獾氖牵允粜灾邓鞯男薷谋匦肷洗丶腃ontrolStyle属性。下面列举了一个示例应用程序,其重写了Table控件的样式属性CellSpacing和Caption。服务器控件源代码如下所示。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebControlLibrary{
[DefaultProperty("Text")]
[ToolboxData("<{0}:WebCustomControl runat=server></{0}:WebCustomControl>")]
public class WebCustomControl : Table {
//创建构造函数
public WebCustomControl() {
base.Caption = "工作安排列表";
base.CellSpacing = 0;
}
// 重写CellSpacing属性
[ Bindable(false), Browsable(false), DefaultValue(0) ]
public override int CellSpacing {
get {
return base.CellSpacing;
}
set {
throw new NotSupportedException("不能设置CellSpacing属性.");
}
}
//重写Caption属性
[DefaultValue("工作安排列表")]
public override string Caption {
get { return base.Caption; }
set { base.Caption = value; }
}
}
}
以上代码主要用于说明重写样式属性的实现方法。具体分析如下所示。
(1)控件类WebCustomControl继承自Table。这样,自定义控件则自动继承了Table控件所具有的所有样式属性。这为重写样式属性奠定了基础。
(2)在控件类的构造函数中设置了Caption和CellSpacing的属性值。
(3)重写CellSpacing属性。通过元数据属性标记设置了该属性不可被数据绑定(Bindable),告诉设计器该属性不可被浏览(Browsable),最后设置了默认值为0(DefaultValue)。另外,在CellSpacing属性的设置操作中定义了一个异常。当开发人员设置该属性时将显示该异常。
(4)重写Caption属性,为该属性设置默认值。
可能有些读者认为构造函数的设置内容没有什么意义。实际上,实现本例的核心就在于此。只有在构造函数中设置新的属性值,才能够将新值传递给ControStyle属性。因为ControlStyle属性主要完成的工作是负责样式状态管理以及样式属性的生成。如果没有把改变传到ControlStyle,那么重写的样式属性就不会按照预期的那样显示。
下面列举了为测试自定义控件WebCustomControl而创建的Default.ASPx页面源代码。
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.ASPx.cs" Inherits="_Default" %>
<%@ ReGISter TagPrefix="wcc" Namespace="WebControlLibrary" Assembly="WebControlLibrary" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xHTML1/DTD/xHTML1-transitional.dtd">
<HTML XMLns="http://www.w3.org/1999/xHTML">
<head runat="server">
<title>重写样式属性</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<wcc:WebCustomControl ID="demo1" runat="server" Font-Size="small" BorderWidth="1px" CellPadding="4" BorderColor="black" GridLines="both">
<ASP:TableRow>
<ASP:TableCell font-bold="True" runat="server">工作项目</ASP:TableCell>
<ASP:TableCell font-bold="True" runat="server">截至日期</ASP:TableCell>
<ASP:TableCell font-bold="True" runat="server">备注</ASP:TableCell>
</ASP:TableRow>
<ASP:TableRow runat="server">
<ASP:TableCell runat="server">工作1</ASP:TableCell>
<ASP:TableCell runat="server">7月17日</ASP:TableCell>
<ASP:TableCell runat="server">备注内容</ASP:TableCell>
</ASP:TableRow>
<ASP:TableRow runat="server">
<ASP:TableCell runat="server">工作2</ASP:TableCell>
<ASP:TableCell runat="server">7月27日</ASP:TableCell>
<ASP:TableCell runat="server">备注内容</ASP:TableCell>
</ASP:TableRow>
<ASP:TableRow runat="server">
<ASP:TableCell runat="server">工作3</ASP:TableCell>
<ASP:TableCell runat="server">7月29日</ASP:TableCell>
<ASP:TableCell runat="server">备注内容</ASP:TableCell>
</ASP:TableRow>
</wcc:WebCustomControl>
</div>
</form>
</body>
</HTML>
下面显示了示例应用效果图。
根据Default.ASPx源代码以及应用效果图可知,上图中的表格标题(Caption),以及相邻表格间距(CellSpacing)均由自定义控件内置设置,而不是通过控件的显式标记来完成。这就是重写控件样式属性的结果。
小结
本文首先对服务器控件样式的基本知识进行了简要介绍,然后,通过一个典型示例说明了重写控件样式属性的方法。希望读者通过这些内容,能够对服务器控件样式属性建立一个更为深入的理解。在随后的文章中,我们将讲解实现样式属性的具体方法。
。