23 Jul 2007

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

Share

 

在自定义控件中实现ICallbackEventHandler接口不经过回发而实现客户端回掉 
Asp.Net2.0中新增了ICallbackEventHandler接口,用于指示控件可以作器的回事件的目
MSDN中的描述:

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

ICallbackEventHandler成员有:

 

 

名称

 


 

 

 

GetCallbackResult

 

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

 

 

 

RaiseCallbackEvent

 

理以控件的回事件。

 

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



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

comments powered by Disqus