Go homepage(回首页) Upload pictures (上传图片) Write articles (发文字帖)
The author:(作者)delvpublished in(发表于) 2014/1/10 6:24:09 ASP.NET,2.0服务器控件之复合控件样式_[Asp.Net教程]
public class MyControl : CompositeControl{ // 相关代码 ...... // 定义初始值 private int _cellPadding = 0; private int _border = 1; ...... // 定义样式属性,它和Table控件的样式属性CellPadding和Border类似 public int CellPadding{ get { return _cellPadding; } set { _cellPadding = value; } // 实现属性Border public int Border{ get { return _border; } set { _border = value; } ...... // 重写CreateChildControls方法 protected override void CreateChildControls() { //相关代码 ...... Table t = new Table(); //将前面定义的属性添加到键/值对中 t.AddAttributes.Add("CellPadding",_cellPadding.ToString()); t.AddAttributes.Add("Border",_border.ToString()); ...... } }
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(); } } }}
<%@ 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>
图1 效果图
赞