内容载入中...
2、上传全部样式属性
在上一节中,说明了有关实现复合控件样式的内容,但是,那种实现方法只能实现子控件部分的样式,并且缺乏逻辑性和组织性。本小节介绍的实现复合控件样式属性的方法有效避免了以上问题。它实现了多重委托的属性,即对每个子控件分别定义Width、Height等样式,更进一步的讲,即实现每个子控件对应的Style类型的复杂样式属性,例如,TextBoxStyle、ButtonStyle。通过这种方式子控件的样式属性就上传为顶层属性,以便于设置子控件的外观。
显而易见,实现子控件的样式属性上传的关键是实现Style类型的复杂样式属性。为此,开发人员必须为复杂样式属性提供自定义视图状态管理。需要读者注意的是,复合控件的视图状态与普通控件视图状态有所不同。由于复合控件包含子控件,因此,相应的视图状态中既包括父控件的视图状态,也包括子控件对应的复杂样式属性的视图状态。例如,上文实例中控件的视图状态即包含3个部分:父控件自身的视图状态、ButtonStyle的视图状态和TextBoxStyle的视图状态。除此之外,具体的实现过程与实现普通的复杂属性基本一致。
不知读者是否还记得上一篇文章中的那个复合控件,即由一个文本框TextBox和一个按钮Button组成的复合控件CompositeEvent。在此,我们对该控件添加设置了控件的顶层样式属性ButtonStyle和TextBoxStyle。下面列举了控件类CompositeEvent的源代码。
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.ComponentModel.Design;
namespace WebControlLibrary{
public class CompositeEvent : CompositeControl {
//声明变量
private Button _button;
private TextBox _textBox;
private static readonly object EventSubmitKey = new object();
//声明样式变量
private Style _buttonStyle;
private Style _textBoxStyle;
//定义属性ButtonText,用于指定按钮上的文字
[Bindable(true), Category("Appearance"), DefaultValue(""), Description("获取或设置显示显示在按钮上的文字")]
public string ButtonText {
get { EnsureChildControls(); return _button.Text; }
set { EnsureChildControls(); _button.Text = value; }
}
//定义属性Text,表示文本框的输入
[Bindable(true), Category("Appearance"), DefaultValue(""), Description("获取或设置文本框输入文本")]
public string Text {
get {
EnsureChildControls();
return _textBox.Text;
}
set {
EnsureChildControls();
_textBox.Text = value;
}
}
// 定义ButtonStyle属性
[ Category("Style"), Description("Button的样式属性"), DesignerSerializationVisibility(DesignerSerializationVisibility.Content), NotifyParentProperty(true), PersistenceMode(PersistenceMode.InnerDefaultProperty) ]
public virtual Style ButtonStyle {
get {
if (_buttonStyle == null) {
_buttonStyle = new Style();
if (IsTrackingViewState) {
((IStateManager)_buttonStyle).TrackViewState();
}
}
return _buttonStyle;
}
}
//定义TextStyle属性
[ Category("Style"), Description("设置TextBox的样式属性"), DesignerSerializationVisibility(DesignerSerializationVisibility.Content), NotifyParentProperty(true), PersistenceMode(PersistenceMode.InnERProperty) ]
public virtual Style TextBoxStyle {
get {
if (_textBoxStyle == null) {
_textBoxStyle = new Style();
if (IsTrackingViewState) {
((IStateManager)_textBoxStyle).TrackViewState();
}
}
return _textBoxStyle;
}
}
// 实现事件属性结构
public event EventHandler Submit {
add { Events.AddHandler(EventSubmitKey, value); }
remove { Events.RemoveHandler(EventSubmitKey, value); }
}
// 实现OnSubmit
protected virtual void OnSubmit(EventArgs e) {
EventHandler SubmitHandler = (EventHandler)Events[EventSubmitKey];
if (SubmitHandler != null) {
SubmitHandler(this, e);
}
}
// 重写ICompositeControlDesignerAccessor接口的RecreateChildContrls方法
protected override void RecreateChildControls() { EnsureChildControls(); }
//重写CreateChildControls方法,将子控件添加到复合控件中
protected override void CreateChildControls() {
Controls.Clear();
_button = new Button();
_textBox = new TextBox();
_button.ID = "btn";
//_button.Click += new EventHandler(_button_Click);
_button.CommandName = "Submit";
this.Controls.Add(_button);
this.Controls.Add(_textBox);
}
// 重写OnBubbleEvent方法,执行事件冒泡
protected override bool OnBubbleEvent(object source, EventArgs e) {
bool handled = false;
if (e is CommandEventArgs) {
CommandEventArgs ce = (CommandEventArgs)e;
if (ce.CommandName == "Submit") {
OnSubmit(EventArgs.Empty);
handled = true;
}
}
return handled;
}
//重写Render方法,呈现控件中其他的HTML代码
protected override void Render(HTMLTextWriter output) {
AddAttributesToRender(output);
if (_textBoxStyle != null) {
_textBox.ApplyStyle(TextBoxStyle);
}
if (_buttonStyle != null) {
_button.ApplyStyle(ButtonStyle);
}
output.AddAttribute(HTMLTextWriterAttribute.Border, "0px");
output.AddAttribute(HTMLTextWriterAttribute.Cellpadding, "5px");
output.AddAttribute(HTMLTextWriterAttribute.Cellspacing, "0px");
output.RenderBeginTag(HTMLTextWriterTag.Table);
output.RenderBeginTag(HTMLTextWriterTag.Tr);
output.RenderBeginTag(HTMLTextWriterTag.Td);
_textBox.RenderControl(output);
output.RenderEndTag();
output.RenderBeginTag(HTMLTextWriterTag.Td);
_button.RenderControl(output);
output.RenderEndTag();
output.RenderEndTag();
output.RenderEndTag();
}
//复杂样式属性的状态管理,重写3个相关方法LoadViewState、 SaveViewState、TrackViewState
protected override void LoadViewState(object savedState) {
if (savedState == null) {
base.LoadViewState(null);
return;
}
if (savedState != null) {
object[] myState = (object[])savedState;
if (myState.Length != 3) { throw new ArgumentException("无效的ViewState"); }
base.LoadViewState(myState[0]); if (myState[1] != null) {
((IStateManager)TextBoxStyle).LoadViewState(myState[1]);
}
if (myState[2] != null) {
((IStateManager)ButtonStyle).LoadViewState(myState[2]);
}
}
}
protected override object SaveViewState() {
object[] myState = new object[3];
myState[0] = base.SaveViewState();
myState[1] = (_textBoxStyle != null) ? ((IStateManager)_textBoxStyle).SaveViewState() : null;
myState[2] = (_buttonStyle != null) ? ((IStateManager)_buttonStyle).SaveViewState() : null;
for (int i = 0; i < 3; i++) {
if (myState[i] != null) { return myState; }
}
return null;
}
protected override void TrackViewState() {
base.TrackViewState();
if (_buttonStyle != null) {
((IStateManager)_buttonStyle).TrackViewState();
}
if (_textBoxStyle != null) {
((IStateManager)_textBoxStyle).TrackViewState();
}
}
}
}
如果读者看过前面的文章,那么应该对以上代码不陌生。限于篇幅,本文不对先前说明过的内容进行讲解,而重点说明有关样式冒泡的内容。
与样式冒泡相关的内容可以分成三个部分:一是定义Style类型的复杂样式属性:ButtonStyle和TextBoxStyle;二是在Render方法中为子控件应用复杂样式属性;三是实现复杂样式属性的自定义视图状态管理部分。以上三个部分的实现,实际是实现子控件样式上传过程中最为关键的三个步骤。前两个部分的实现比较简单,在此就不多加说明。下面重点说明最后一个部分的实现。
第三部分主要实现复杂样式属性的自定义状态管理。在TrackViewState方法中,分别对基类、_textBoxStyle和_buttonStyle调用TrackViewState。在SaveViewState方法中,首先定义一个myState对象数组,然后按顺序将基类、TextBox和Button的视图状态数据保存到myState中并返回。在LoadViewState方法中,实现将所保存的状态数据(savedState)的第一部分加载入基类,第二部分加载给TextBoxStyle,第三部分加载给ButtonStyle,之所以按照如此顺序加载是与SaveViewState方法中的保存顺序对应的。
下面是CompositeEvent控件的应用代码片断。请读者注意的是:ButtonStyle和TextBoxStyle都是作为内部嵌套形式属性而标记,用户通过设置样式属性即可完成对子控件的外观设置。
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.ASPx.cs" Inherits="_Default" %>
<%@ ReGISter TagPrefix="Sample" Assembly="WebControlLibrary" Namespace="WebControlLibrary" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xHTML1/DTD/xHTML1-transitional.dtd">
<script runat="server">
void demo1_Submit(object sender, EventArgs e)
{
lbMessage.Text = "您刚才输入的是:" + demo1.Text;
}
</script>
<HTML XMLns="http://www.w3.org/1999/xHTML">
<head id="Head1" runat="server">
<title>为复合控件实现样式</title>
</head>
<body> <form id="form1" runat="server">
<div>
<Sample:CompositeEvent ID="demo1" runat="server" ButtonText="提交" OnSubmit="demo1_Submit">
<TextBoxStyle Width="198px" Height="20px" BorderWidth="1px" BackColor="orange">
</TextBoxStyle>
<ButtonStyle Width="84px" Height="24px" BorderWidth="1px" BorderStyle="dotted"></ButtonStyle>
</Sample:CompositeEvent>
<br />
<ASP:Label ID="lbMessage" runat="server">
</ASP:Label> </div>
</form>
</body>
</HTML>
下面列举了示例应用效果图。
。