扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
作者:金属边缘 来源:天极开发 2007年11月6日
关键字: Windows
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 效果图 |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者