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

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

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

MSDN中的描述:

实现 ICallbackEventHandler 接口的控件的示例包括 GridView、DetailsView 和 TreeView 控件。当回调事件以实现了 ICallbackEventHandler 接口的控件为目标时,将把事件变量作为参数传递来调用 RaiseCallbackEvent 方法以处理该事件,并且GetCallbackResult 方法返回回调的结果。

ICallbackEventHandler成员有:

 

名称

GetCallbackResult

返回以控件为目标的回调事件的结果。

RaiseCallbackEvent

处理以控件为目标的回调事件。

 

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

//------------------------------------------------------------------------------ // // Copyright (c) www.AspxBoy.com All rights reserved. // //------------------------------------------------------------------------------ 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 { /// /// A Asynchronous AutoPostback Checkbox Control /// [DefaultEvent("CheckedChanged")] [ControlValueProperty("Checked")] [DefaultProperty("Text")] public class AsynchronousCheckBox : WebControl, INamingContainer, ICallbackEventHandler { #region Delegates /// /// The delegate for the checked changed event /// /// /// public delegate void CheckedChangedEventHander(object sender, CheckChangedEventArgs e); #endregion #region Events private static readonly object eventCheckedChanged; /// /// The checked changed event. /// public event CheckedChangedEventHander CheckedChanged { add { Events.AddHandler(eventCheckedChanged, value); } remove { Events.RemoveHandler(eventCheckedChanged, value); } } #endregion #region Constructors /// /// Static Constructor /// static AsynchronousCheckBox() { eventCheckedChanged = new object(); } /// /// Constructor /// public AsynchronousCheckBox() : base(HtmlTextWriterTag.Input) { } #endregion #region Properties /// /// Gets or sets a value indicating whether the Lable Text /// [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; } } /// /// Gets or sets a value indicating whether the 'Client CallBack Script Name' /// [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; } } /// /// Gets or sets a value indicating whether the checkbox 's checked /// [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; } } /// /// Gets or sets a value indicating whether the Text 's cssClass /// [Description("Gets or sets a value indicating whether the Text 's cssClass")] public string TextCss { get { return (string)ViewState["TextCss"]; } set { ViewState["TextCss"] = value; } } /// /// Gets or sets a value indicating whether the Label 's position /// public virtual TextAlign TextAlign { get { object o = ViewState["TextAlign"]; if (o != null) { return (TextAlign)o; } return TextAlign.Right; } set { if ((value TextAlign.Right)) { throw new ArgumentOutOfRangeException("value"); } ViewState["TextAlign"] = value; } } #endregion #region Render Meghods /// /// /// /// protected override void Render(HtmlTextWriter writer) { if (TextAlign == TextAlign.Left) { RenderLabel(writer); base.Render(writer); } else { base.Render(writer); RenderLabel(writer); } } /// /// Render Label /// /// private void RenderLabel(HtmlTextWriter writer) { if (string.IsNullOrEmpty(Text)) { return; } writer.Write(""); writer.Write(Text); writer.WriteEndTag("label"); } /// /// Override the AddAttributesToRender method /// /// 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 /// /// Invoke the check changed event. /// /// /// 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 /// /// Get the result of a client side callback. /// /// The callback result string. public string GetCallbackResult() { return Checked.ToString(); } /// /// Raise the client callback event /// /// public void RaiseCallbackEvent(string eventArgument) { bool isChecked = Boolean.Parse(eventArgument); CheckChangedEventArgs args = new CheckChangedEventArgs(isChecked); OnCheckedChanged(this, args); } #endregion } } //------------------------------------------------------------------------------ // // Copyright (c) www.AspxBoy.com All rights reserved. // //------------------------------------------------------------------------------ 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 { /// /// /// public class CheckChangedEventArgs:EventArgs { /// /// /// /// public CheckChangedEventArgs(bool _isChecked) { isChecked = _isChecked; } private bool isChecked = false; /// /// /// public bool Checked { get { return isChecked; } } } }

转载于:https://www.cnblogs.com/Luoke365/archive/2008/01/08/1030963.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值