惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Cyberwarzone
Cyberwarzone
V
Vulnerabilities – Threatpost
T
Tenable Blog
Forbes - Security
Forbes - Security
Simon Willison's Weblog
Simon Willison's Weblog
AWS News Blog
AWS News Blog
G
GRAHAM CLULEY
Know Your Adversary
Know Your Adversary
S
Securelist
C
Cybersecurity and Infrastructure Security Agency CISA
Project Zero
Project Zero
C
CXSECURITY Database RSS Feed - CXSecurity.com
V
Visual Studio Blog
WordPress大学
WordPress大学
Latest news
Latest news
K
Kaspersky official blog
T
Tailwind CSS Blog
T
Threat Research - Cisco Blogs
B
Blog RSS Feed
C
Cisco Blogs
博客园 - 聂微东
Martin Fowler
Martin Fowler
T
The Blog of Author Tim Ferriss
小众软件
小众软件
L
LangChain Blog
阮一峰的网络日志
阮一峰的网络日志
L
LINUX DO - 热门话题
Stack Overflow Blog
Stack Overflow Blog
罗磊的独立博客
P
Proofpoint News Feed
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
P
Privacy International News Feed
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
C
CERT Recently Published Vulnerability Notes
Cisco Talos Blog
Cisco Talos Blog
S
SegmentFault 最新的问题
Security Latest
Security Latest
Y
Y Combinator Blog
爱范儿
爱范儿
aimingoo的专栏
aimingoo的专栏
P
Privacy & Cybersecurity Law Blog
L
LINUX DO - 最新话题
月光博客
月光博客
The GitHub Blog
The GitHub Blog
博客园 - 三生石上(FineUI控件)
S
Security Affairs
P
Proofpoint News Feed
D
DataBreaches.Net
有赞技术团队
有赞技术团队
云风的 BLOG
云风的 BLOG

博客园 - rex.ying

ASP.NET+Win2003虚拟主机安全设置 Windows2003服务器安全配置详细篇 - rex.ying - 博客园 mssql 日期格式化函数 devexpress报表控件的使用 关闭wscript.shell VS2005网站和SQL一起打包部署安装心得【转载】 怎样制作VS2008.NET应用程序的安装包 Developer Express XtraGrid使用技巧 UltraGrid文章收集 QQ2009聊天记录破解思路 用独立的DLL来存储图片(资源文件) InstallShield集成.net Framework的安装包制作 导出EXCEL后去除单元格前置的单引号 从SQL Server中导入/导出 Excel 的基本方法 通过远程桌面操作程序出现hook cannot be created(SendKeys语句错误)的解决 C# 获取任意窗体选中文字 屏幕取词 在 ASP.NET 中执行 URL 重写 仿Google的输入下拉提示框 xtrareport的使用心得(转)
ASP.NET自定义控件事件响应
rex.ying · 2009-08-24 · via 博客园 - rex.ying

1.重载protected override bool OnBubbleEvent(object source, EventArgs args)
OnBubbleEvent是控件内包含的控件向外层控件作事件冒泡
-------------------------------------------------------------------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.ComponentModel;

    public class uc_DanXuan : CompositeControl
    {
        private Button btnSave = null;                          
        public EventHandler SaveAnswer;      //定义这个是为了在控件外进行事件响应处理
     
        protected override void CreateChildControls()
        {
            Controls.Clear();
            btnSave = new Button();
            btnSave.Text = "Click Me";
            btnSave.CommandName = "事件的名字随便取";   //这是事件发生时,事件的名字,区分事件用
            btnSave.CommandArgument = "事件的参数,看需要"; //这是事件发生后,用来传参数的,可以没有
            Controls.Add(btnSave);
        }

        protected override bool OnBubbleEvent(object source, EventArgs args)
        {
            if (args is CommandEventArgs)
            {
                CommandEventArgs cmdArgs = args as CommandEventArgs;
                                                         //可以在这里加些代码,在控件内先处理一下
                if(SaveAnswer != null)                   //如果要在外面处理,就到事件处理程序中去
                    SaveAnswer(source, args);           
                return true;
            }
            else
            {
                return base.OnBubbleEvent(source, args);
            }

        }
    }
--------------------------------------------------------------------------------------------------------------------------------------

测试它:
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        uc_DanXuan ucdx = new uc_DanXuan();
        ucdx.SaveAnswer += new EventHandler(SaveAnswer);             //要在外面处理事件
        form1.Controls.Add(ucdx);

    }
    public void SaveAnswer(object sender, EventArgs args)               //这里具体处理
    {

    }
}
---------------------------------------------------------------------------------------------------------------------------------------------

2.直接响应内部控件的事件
using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.ComponentModel;

public class uc_DanXuan : CompositeControl
{
   private Button btnSave;
   public event EventHandler SaveAnswer;               //留给控件外部处理这个事件

   protected virtual void OnSave(Object source, EventArgs e)   //控件内部在这里处理这个事件
   {
       if (SaveAnswer != null)
       {
           SaveAnswer(this, e);                       //内部处理完了,再给外部处理
       }
   }
   
    protected override void CreateChildControls()
    {
        Controls.Clear();
        btnSave = new Button();
        btnSave.ID = "btn";
        btnSave.Text = "Save";
        btnSave.Click += new EventHandler(OnSave);    //响应按钮的click
        this.Controls.Add(btnSave);
    }

    protected override void Render(HtmlTextWriter writer)
    {
        btnSave.RenderControl(writer);    
    }
}
测试代码同上

----------------------------------------------------------------------------------------------------------------------------------------
3.利用客户端javascript来回发?
using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.ComponentModel;

public class uc_DanXuan : CompositeControl, IPostBackEventHandler
{
    private Button btnSave;
    public event EventHandler SaveAnswer;               //留给控件外部处理这个事件

    protected virtual void OnSave(EventArgs e)   //控件内部在这里处理这个事件
    {
        if (SaveAnswer != null)
        {
            SaveAnswer(this, e);                      //内部处理完了,再给外部处理
        }
    }

    protected override void CreateChildControls()
    {
        btnSave = new Button();
        btnSave.ID = "btn";
        btnSave.Text = "Save";

        //实用客户端javascript来完成事件回发
        btnSave.OnClientClick = Page.ClientScript.GetPostBackEventReference(this, "save");

    }
    public override void RenderControl(HtmlTextWriter writer)
    {
        btnSave.RenderControl(writer);
        base.RenderControl(writer);
    }

    void IPostBackEventHandler.RaisePostBackEvent(string eventArgument)
    {
        if (eventArgument == "save")
            OnSave(new EventArgs());
    }  
}
测试代码同上

4.让TextBox响应onKeyDown
----------------------------------------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.ComponentModel;

public class uc_DanXuan : CompositeControl, IPostBackEventHandler
{
    private TextBox tbx;

    public event EventHandler OnKeyDown;
  
    protected override void CreateChildControls()
    {
        Controls.Clear();
        tbx = new TextBox();
        tbx.ID = "tbx";

tbx.Attributes.Add("onkeydown", Page.ClientScript.GetPostBackEventReference(this, "onKeyDown"));
        Controls.Add(tbx);
    }

    public void RaisePostBackEvent(string eventArgument)
    {
        switch (eventArgument)
        {
            case "onKeyDown":
                if (OnKeyDown != null)
                    OnKeyDown(this, EventArgs.Empty);
                break;
        }
    }
}
测试代码:
protected void Page_Load(object sender, EventArgs e)
    {
        uc_DanXuan ucdx = new uc_DanXuan();
        ucdx.OnKeyDown += new EventHandler(ucdx_OnKeyDown);
        form1.Controls.Add(ucdx);

    }

    void ucdx_OnKeyDown(object sender, EventArgs e)
    {
       
    }