自定义控件(12)回传事件IPostBackEventHandler
来源:互联网 发布:世爵娱乐软件 编辑:程序博客网 时间:2024/06/05 15:45
如果一个服务器控件需要支持回传事件,那么它必须要实现IPostBackEventHandler接口
需要实现RaisePostBackEvent(string eventArgument)方法
控件代码
using System;using System.ComponentModel;using System.Web.UI;using System.Web.UI.WebControls;namespace kjlx.BLL.IPostBack{ [ParseChildren(true), //解析为属性 DefaultEvent("Click")] public class IPostBackTest2 : WebControl, IPostBackEventHandler { //外层DIV protected override HtmlTextWriterTag TagKey { get { return HtmlTextWriterTag.Div; } } /// <summary> /// 给Div添加属性和样式 /// </summary> /// <param name="writer"></param> protected override void AddAttributesToRender(HtmlTextWriter writer) { base.AddAttributesToRender(writer); //由于AddAttributeToRender()方法实现了对控件基本属性和样式的支持,所以就算需要重写这个方法,也应当调用base.AddAttributeToRender(),以使自定义控件获得基本的功能。 writer.AddStyleAttribute(HtmlTextWriterStyle.Width, "120px"); writer.AddStyleAttribute(HtmlTextWriterStyle.Height, "35px"); writer.AddStyleAttribute(HtmlTextWriterStyle.BackgroundColor, "#0000aa"); writer.AddStyleAttribute(HtmlTextWriterStyle.Cursor, "pointer");
//如果是链接,回传事件放在href中,使用GetPostBackClientHyperlink string upRef = Page.ClientScript.GetPostBackEventReference(this, null); //创建onclick事件
writer.AddAttribute(HtmlTextWriterAttribute.Onclick, upRef); //给onclick事件赋值 } //Click事件属性 static object ClickHandler=new object(); [Category("Action")] public event EventHandler Click { add { Events.AddHandler(ClickHandler,value); } remove { Events.RemoveHandler(ClickHandler,value); } } /// <summary> /// 处理网页回传事件 /// 触发控件相应的事件 /// </summary> /// <param name="eventArgument"></param> public void RaisePostBackEvent(string eventArgument) { EventArgs args=new EventArgs(); if (Events[ClickHandler] != null) { (Events[ClickHandler] as EventHandler)(null, args); } } }}
html代码
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> <cc1:IPostBackTest2 ID="IPostBackTest1" runat="server" onclick="IPostBackTest1_Click" />
后台代码
protected void IPostBackTest1_Click(object sender, EventArgs e) { Label1.Text = "111"; }
显示结果
阅读全文
0 0
- 自定义控件(12)回传事件IPostBackEventHandler
- asp.net2.0服务器控件捕获回传事件IPostBackEventHandler
- IPostBackEventHandler 触发Web自定义控件事件的接口
- 使用IPostBackEventHandler让JavaScript“调用”回传事件
- 给自定义控件(Web Control)添加事件的几种方法。前两种方法可以不实现IPostBackEventHandler
- 给自定义控件(Web Control)添加事件的几种方法。前两种方法可以不实现IPostBackEventHandler
- IPostBackEventHandler
- 自定义控件之事件回传
- 自定义控件(11)回传事件原理
- 用户控件自定义事件
- 控件的自定义事件
- 自定义控件的事件
- 自定义控件添加事件
- C#-控件-自定义事件
- 自定义控件加事件
- 自定义控件添加事件
- 自定义控件添加事件
- 自定义控件添加事件
- E
- js 的删除方法和 filter的过滤删除方法对比
- 红黑树理解-插入删除
- 类中内容在内存中到底是如何分配的呢?
- 类模板
- 自定义控件(12)回传事件IPostBackEventHandler
- HDU 6069 Count Divisor 数学+思维
- [编程题] 序列交换
- Eigen: C++开源矩阵计算工具——Eigen的简单用法
- table-layout
- 事务的实现方案
- 搭建virtual private server
- 找出无序数组的最长连续子数组
- 关系型数据的分布式处理系统MyCAT(1)—概述和基本使用教程