I have a custom event that is used to alert the main page that a button has been pressed on a usercontrol.
However it takes two clicks for the actions in the main page code behind to take effect.
Usercontrols are loaded inside an update panel
The user control includes the following
public static event EventHandler SignOut;
protected void profileSignOut_Click(object sender, EventArgs e)
{
if (SignOut != null)
SignOut(this, EventArgs.Empty);
}
the main page contains the following
protected void Page_Load(object sender, EventArgs e)
{
Views.Profile.SignOut += Profile_SignOut;
LoadUserControl();
}
private void Profile_SignOut(object sender, EventArgs e)
{
MenuButtons.Visible = true;
LastLoadedControl = null;
LoadUserControl();
}
and just for completeness, the load user control sections also in default.aspx
private string LastLoadedControl
{
get
{
return ViewState["LastLoaded"] as string;
}
set
{
ViewState["LastLoaded"] = value;
}
}
private void LoadUserControl()
{
string controlPath = LastLoadedControl;
ContentPlaceholder.Controls.Clear();
if (!string.IsNullOrEmpty(controlPath))
{
UserControl uc = (UserControl)LoadControl(controlPath);
ContentPlaceholder.Controls.Add(uc);
}
}
Double clicking for events is usually down to a PostBack issue, ie not catering for if (!IsPostBack)
in the Page_Load
. I not 100% sure what's going on with yours because I haven't yet been required to dynamically load a user control.
But I can offer a slight alternative; if you're wanting to fire a specific event on the parent page from your user control, you can use this:
this.Page.GetType().InvokeMember("MyMethodsName", System.Reflection.BindingFlags.InvokeMethod, null, this.Page, new object[] { });
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.