本文介绍了如何在aspx文件中从usercontrol分配事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 假设我有一个名为usercontrol1的自定义Web用户控件,其中可能发生事件Event1。 我想在页面上运行一些函数,其中包含这个用户控制。 显而易见的方法是委托代表: Let's say I have custom Web User Control named usercontrol1, where an event Event1 can occure.I'd like to run some function on page, that contains this usercontrol.The obvious way is to asign a delegate:public partial class UserControl1 : System.Web.UI.UserControl{ public event EventHandler Event1; // ... code here ...} public partial class _Default : System.Web.UI.Page { protected void Page_Init(object sender, EventArgs e) { usercontrol1.Event1+=this.some_function; } protected void some_function(object sender, EventArgs e) { // what should be done after event raised... } // other code} 但是我想在基页的aspx文件上分配一个事件,如下所示: However I would like to assign an event on aspx file of base page, like this:<UserControl1 name="usercontrol1" OnEvent1="some_function" /> 如何做到这一点?我很感激任何帮助。 :)How can this be done? I'd appreciate any help. :)推荐答案 public partial class WebUserControl1 : System.Web.UI.UserControl{ public event EventHandler TextUpdated; public string Text { get { return ( TextBox1.Text ); } } protected void TextBox1_TextChanged ( object sender, EventArgs e ) { if ( TextUpdated != null ) { TextUpdated( this, EventArgs.Empty ); } }} 页面标记: Page markup:<%@ Register Src="~/WebUserControl1.ascx" TagPrefix="Control" TagName="WebUserControl1" %><!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"> <title></title></head><body> <form id="form1" runat="server"> <div> <Control:WebUserControl1 runat="server" OnTextUpdated="Unnamed_TextUpdated" /> </div> </form></body></html> 重要部分以粗体显示!The important parts are in bold! 这篇关于如何在aspx文件中从usercontrol分配事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-18 16:10