asp.net:将对象的事件从UserControl发送到其父级 [英] asp.net: send an object's event from a UserControl to its Parent
问题描述
我有一个用户类,其中有 Login()和 Logout()和 GetData() 。有一个 UserChanged 事件,它会在调用这些函数之一时触发。
I have a User class with functions to Login() and Logout() and GetData(). There's a UserChanged event, it fires when either of these functions are called.
我的默认值为UserControl我们有一个名为Login.ascx的UserControl,它们被动态添加。
I have UserControls on my Default.aspx (they're added dynamically).
它提供了使用User类的功能:您可以在此处登录/注销。我有一个**** EventHandler * for ** UserChanged *****(如果我没有一个,它会失败,一个对象没有找到异常)。
I have a UserControl named Login.ascx. It provides the functionality to use the User class: you can Login / Logout here. I have an ****EventHandler* for **UserChanged***** (if I don't have one, it will fail with a object not found exception).
我需要提醒其他UserControls关于用户状态的更改。
我以某种方式需要将UserChanged事件从Login.ascx传递给其父Default.aspx。
I need to alert the other UserControls about the changes to the user's state. I somehow need to pass the UserChanged event from Login.ascx to its Parent, Default.aspx.
我该怎么做?
推荐答案
Login.ascx.cs
public delegate void OnUserChangedHandler(object sender);
public OnUserChangedHandler OnUserChanged;
private void RaiseUserChangedEvent() //Call this method when the user changes
{
//UserChanged Event
if (OnUserChanged != null)
{
OnUserChanged(this);
}
}
Default.aspx.cs
protected override void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
loginControl.OnUserChanged += OnUserChanged;
}
private void OnUserChanged(object sender)
{
//Code here to do something with other controls on this page
}
这篇关于asp.net:将对象的事件从UserControl发送到其父级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!