0
votes

I am experiencing the almost same issue specified in this question. Can anybody please post the answer for this? The question is not answered in a clear way.

Issue with dynamically loading a user control on button click

In this aforementioned question, he is loading the control to a placeholder present in the first user control. My scenario is slightly different. My scenario is i have a single aspx page, UserControl1 and UserControl2. At the very beginning, I will load UserControl1 to Page. Then I need to unload userControl1 and load UserControl2 to Page when user clicks a button from UserControl1.

2
What exactly you don't understand? What have you tried?walther
@walther - In the marked answer, the '~/UserCtrl2.ascx' is adding to PlaceHolder2 from the Button1_Click() event. But PlaceHolder2 is present in Default.aspx and Button1 is in UserCtrl1.ascx. And SecondControlLoaded viewstate is associated with Default.aspx.cs i think. But there, it is linked in UserCtrl1.ascx.cs And please see Kumar's comment in the marked answer. "Can you please post the complete code? I am guessing that SecondControlLoaded is a bool property in UserCtrl1 which saves the value to viewstate..." It would be really helpful if you can post the complete code.mlg

2 Answers

0
votes

Events must be registered on page load not later. Your control is created during event handling and its event is not registered. Take a look: http://msdn.microsoft.com/en-us/library/ms178472.aspx

0
votes

You need to create a custom event handler for UserControl1 and bubble the event up to the page when the button is clicked.

Create a custom event handler for UserControl1:

public event EventHandler UpdateButtonClick;

public void OnUpdateButtonClick(EventArgs e)
{
    if (UpdateButtonClick!= null)
        UpdateButtonClick(this, e);
}

Assign the event handler for the UserControl1:

<uc:UserControl1 ID="UserControl1" runat="server"
    OnUpdateButtonClick="UserControl1_UpdateButtonClick" ... />

Handle the event in the code-behind:

protected void UserControl1_UpdateButtonClick(object sender, EventArgs e)
{
    UserControl1.Visible = false;
    UserControl2.Visible = true;
}