Due to the life cycle of an ASP.NET web page, I get Nulls when I set public properties in a user control from the main page. These variables go beyond the scope after the page is sent to the client. However, this is a simple solution that will allow you to be notified when an event occurs in your user control.
NOTE. There are some problems with Page.Context when they return to the main page, so changing the actions that affect the presentation may not work.
...
public delegate void CallbackAction(object sender, CustomEventArgs e);
public CallbackAction OnCallbackAction
{
get { return Session["CallbackAction"] as CallbackAction; }
set { Session["CallbackAction"] = value; }
}
- UserControl ,
protected void UserControlButton_Click(object sender, EventArgs e)
{
if (IsPostback)
{
if (this.OnCallbackAction != null)
{
this.OnCallbackAction.Invoke(this, new CustomEventArgs("ET phone home") );
}
}
}
, usercontrol. usercontrol
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostback)
{
this.UserControlFoo.OnCallbackAction += CallbackFromUserControl_Click;
}
}
protected void CallbackFromUserControl_Click(object sender, CustomEventArgs e)
{
}