给WEB用户控件添加事件

在创建某个用户控件时,我使用了TextBox控件,我想用户控件也有TextChanged这样的事件,怎么实现呢?下面提供了一种方法。

阅读本部分内容需要具备【委托(delegate)和事件(event)】的相关知识 。实际上,代码非常简单。

1、创建用户控件

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="UserTextBox.ascx.cs" Inherits="UserTextBox" %>
<asp:TextBox ID="txtUserTextBox" runat="server" 
    ontextchanged
="txtUserTextBox_TextChanged"></asp:TextBox>

复制代码
using System;

public partial class UserTextBox : System.Web.UI.UserControl
{
    
#region private fields
    
private string _Text;
    
#endregion

    
public delegate void TextChangedHandler(object sender, TextChangedEventArgs e);
    
public event TextChangedHandler TextChanged;

    
public void OnTextChaged(TextChangedEventArgs e)
    {
        
if (TextChanged != null)
        {
            TextChanged(
this, e);
        }
    }
    
//在这里触发事件
    protected void txtUserTextBox_TextChanged(object sender, EventArgs e)
    {
        OnTextChaged(
new TextChangedEventArgs(txtUserTextBox.Text));
    }
    
/// <summary>
    
/// 事件数据类
    
/// </summary>
    public class TextChangedEventArgs : EventArgs
    {
        
public readonly string Text;
        
public TextChangedEventArgs(string Text)
        {
            
this.Text = Text;
        }
    }

    
#region Properties
    
public bool AutoPostBack
    {
        
get
        {
            
return txtUserTextBox.AutoPostBack;
        }
        
set
        {
            txtUserTextBox.AutoPostBack 
= value;
        }
    }
    
public string Text
    {
        
get { return _Text; }
        
set { _Text = value; }
    }
    
#endregion
}
复制代码

2、使用ASP.NET窗体测试

注意:事件不能在属性窗口点击“闪电”符号那里添加,而是在窗体类中写代码添加的,这里是重写了基类Page中的OnInit方法。

复制代码
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<%@ Register src="UserTextBox.ascx" tagname="UserTextBox" tagprefix="uc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    
<title>无标题页</title>
</head>
<body>
    
<form id="form1" runat="server">
    
<div>
    
        
<uc1:UserTextBox ID="UserTextBox1" runat="server" 
            AutoPostBack
="True"/>
    
    
</div>
    
</form>
</body>
</html>
复制代码

 

复制代码
using System;

public partial class _Default : System.Web.UI.Page 
{

    
void UserTextBox1_TextChanged(object sender, UserTextBox.TextChangedEventArgs e)
    {
        Response.Write(e.Text);
    }

    
protected override void OnInit(EventArgs e)
    {
        
this.UserTextBox1.TextChanged += new UserTextBox.TextChangedHandler(UserTextBox1_TextChanged);
        
base.OnInit(e);
    }
}
复制代码

posted on   Ferry  阅读(1045)  评论(0编辑  收藏  举报

编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述
< 2009年7月 >
28 29 30 1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31 1
2 3 4 5 6 7 8

导航

统计

点击右上角即可分享
微信分享提示