您的位置:首页 > 其它

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

2007-07-23 14:49 597 查看
在自定义控件中实现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
///
///
The event arguments.
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;
}
}
}
}

http://www.aspxboy.com/private/5504/default.aspx
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐