给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>
<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
}
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>
<%@ 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);
}
}
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);
}
}
分类:
AJAX&ASP.NET
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 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的设计模式综述