在自定义控件中实现ICallbackEventHandler接口不经过回发而实现客户端回掉

 

在自定义控件中实现ICallbackEventHandler接口不经过回发而实现客户端回掉 

Asp.Net2.0中新增了ICallbackEventHandler接口,用于指示控件可以作器的回事件的目

MSDN中的描述:

实现 ICallbackEventHandler 接口的控件的示例包括 GridViewDetailsView TreeView 控件。当回事件以实现 ICallbackEventHandler 接口的控件标时,将把事件量作参数传递 RaiseCallbackEvent 方法以事件,并且GetCallbackResult 方法返回回

ICallbackEventHandler成员有:

 

名称

GetCallbackResult

返回以控件的回事件的果。

RaiseCallbackEvent

理以控件的回事件。

 

如下代码实现一个不经过回发而实现客户端回掉的CheckBox

//------------------------------------------------------------------------------ // <copyright company="Meibo Wu www.AspxBoy.com"> // Copyright (c) www.AspxBoy.com All rights reserved. // </copyright> //------------------------------------------------------------------------------ using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace HBZ { /// <summary> /// A Asynchronous AutoPostback Checkbox Control /// </summary> [DefaultEvent("CheckedChanged")] [ControlValueProperty("Checked")] [DefaultProperty("Text")] public class AsynchronousCheckBox : WebControl, INamingContainer, ICallbackEventHandler { #region Delegates /// <summary> /// The delegate for the checked changed event /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public delegate void CheckedChangedEventHander(object sender, CheckChangedEventArgs e); #endregion #region Events private static readonly object eventCheckedChanged; /// <summary> /// The checked changed event. /// </summary> public event CheckedChangedEventHander CheckedChanged { add { Events.AddHandler(eventCheckedChanged, value); } remove { Events.RemoveHandler(eventCheckedChanged, value); } } #endregion #region Constructors /// <summary> /// Static Constructor /// </summary> static AsynchronousCheckBox() { eventCheckedChanged = new object(); } /// <summary> /// Constructor /// </summary> public AsynchronousCheckBox() : base(HtmlTextWriterTag.Input) { } #endregion #region Properties /// <summary> /// Gets or sets a value indicating whether the Lable Text /// </summary> [Description("Gets or sets a value indicating whether the Lable Text")] public virtual string Text { get { return (string)ViewState["Text"]; } set { this.ViewState["Text"] = value; } } /// <summary> /// Gets or sets a value indicating whether the 'Client CallBack Script Name' /// </summary> [Description("Gets or sets a value indicating whether the 'Client CallBack Script function Name'")] public string ClientCallBackScript { get { object o = ViewState["ClientCallBackScript"]; return o == null ? "null" : o.ToString(); } set { ViewState["ClientCallBackScript"] = value; } } /// <summary> /// Gets or sets a value indicating whether the checkbox 's checked /// </summary> [Description("Gets or sets a value indicating whether the checkbox 's checked")] public bool Checked { get { object o = ViewState["Checked"]; return o == null ? false : (bool)o; } set { ViewState["Checked"] = value; } } /// <summary> /// Gets or sets a value indicating whether the Text 's cssClass /// </summary> [Description("Gets or sets a value indicating whether the Text 's cssClass")] public string TextCss { get { return (string)ViewState["TextCss"]; } set { ViewState["TextCss"] = value; } } /// <summary> /// Gets or sets a value indicating whether the Label 's position /// </summary> public virtual TextAlign TextAlign { get { object o = ViewState["TextAlign"]; if (o != null) { return (TextAlign)o; } return TextAlign.Right; } set { if ((value <textalign.left) || (value > TextAlign.Right)) { throw new ArgumentOutOfRangeException("value"); } ViewState["TextAlign"] = value; } } #endregion #region Render Meghods /// <summary> /// /// </summary> /// <param name="writer"></param> protected override void Render(HtmlTextWriter writer) { if (TextAlign == TextAlign.Left) { RenderLabel(writer); base.Render(writer); } else { base.Render(writer); RenderLabel(writer); } } /// <summary> /// Render Label /// </summary> /// <param name="writer"></param> private void RenderLabel(HtmlTextWriter writer) { if (string.IsNullOrEmpty(Text)) { return; } writer.Write("<label"); writer.WriteAttribute("for", ClientID); if (!string.IsNullOrEmpty(TextCss)) { writer.WriteAttribute("class", TextCss); } writer.Write(">"); writer.Write(Text); writer.WriteEndTag("label"); } /// <summary> /// Override the AddAttributesToRender method /// </summary> /// <param name="writer"></param> protected override void AddAttributesToRender(HtmlTextWriter writer) { if (base.Page == null) { base.Page.VerifyRenderingInServerForm(this); } string callbackReference = Page.ClientScript.GetCallbackEventReference(this, "this.checked", ClientCallBackScript, null); writer.AddAttribute(HtmlTextWriterAttribute.Onclick, callbackReference); writer.AddAttribute(HtmlTextWriterAttribute.Type, "checkbox"); if (Checked) { writer.AddAttribute(HtmlTextWriterAttribute.Checked, "checked"); } if (!Enabled) { writer.AddAttribute(HtmlTextWriterAttribute.Disabled, "disabled"); } if (!string.IsNullOrEmpty(ToolTip)) { writer.AddAttribute(HtmlTextWriterAttribute.Title, ToolTip); } base.AddAttributesToRender(writer); } #endregion #region On Checked Changed /// <summary> /// Invoke the check changed event. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected virtual void OnCheckedChanged(object sender, CheckChangedEventArgs e) { CheckedChangedEventHander hander = Events[eventCheckedChanged] as CheckedChangedEventHander; if (hander != null) { Checked = e.Checked; hander(this, e); } } #endregion #region ICallbackEventHandler Members /// <summary> /// Get the result of a client side callback. /// </summary> /// <returns>The callback result string.</returns> public string GetCallbackResult() { return Checked.ToString(); } /// <summary> /// Raise the client callback event /// </summary> /// <param name="eventArgument">The event arguments.</param> public void RaiseCallbackEvent(string eventArgument) { bool isChecked = Boolean.Parse(eventArgument); CheckChangedEventArgs args = new CheckChangedEventArgs(isChecked); OnCheckedChanged(this, args); } #endregion } } //------------------------------------------------------------------------------ // <copyright company="Meibo Wu www.AspxBoy.com"> // Copyright (c) www.AspxBoy.com All rights reserved. // </copyright> //------------------------------------------------------------------------------ using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace HBZ { /// <summary> /// /// </summary> public class CheckChangedEventArgs:EventArgs { /// <summary> /// /// </summary> /// <param name="_isChecked"></param> public CheckChangedEventArgs(bool _isChecked) { isChecked = _isChecked; } private bool isChecked = false; /// <summary> /// /// </summary> public bool Checked { get { return isChecked; } } } }
         http://www.aspxboy.com/private/5504/default.aspx

posted on 2007-07-23 14:49  活靶子.Net  阅读(2012)  评论(0编辑  收藏  举报

导航