Author: 武眉博<活靶子.Net>
在自定义控件中实现ICallbackEventHandler接口不经过回发而实现客户端回掉
Asp.Net2.0中新增了ICallbackEventHandler接口,用于指示控件可以作为服务器的回调事件的目标。
MSDN中的描述:
实现 ICallbackEventHandler 接口的控件的示例包括 GridView、DetailsView 和 TreeView 控件。当回调事件以实现了 ICallbackEventHandler 接口的控件为目标时,将把事件变量作为参数传递来调用 RaiseCallbackEvent 方法以处理该事件,并且GetCallbackResult 方法返回回调的结果。
ICallbackEventHandler成员有:
如下代码实现一个不经过回发而实现客户端回掉的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("