0

I am trying to solve the following issue:

I have a user control that needs to be dynamically loaded inside another control. This dynamically loaded control raises an event and as per my knowledge the events raised by dynamically loaded control will only be handled correctly if the control is created and loaded during the onload event. There is one more constraint that i have to consider when loading the control dynamically and that is a property in parent control. This property will determine if the control should be loaded or not.

Pseudo Code:

ControlA
  Property ShowControl
  ControlA has a CheckBox(chkShowControlIfSelected)
  OnLoadEvent()
    If chkShowControlIfSelected.checked checked and ShowControlProperty is set
    {
       reate ControlB Dynamically
       ControlB.Event += EventHandler()
       Add ControlB to ControlCollection
    }

The problem i am running into is that if I include the code to load the controlB in prerender event then the property ShowControl is set correctly but the EventHandler() is not called. If I put the code to load the controlB dynamically in pageLoad event then property ShowControl is not yet set but in that case the eventHandler Code is called correctly.

Am i missing something or handling the code in incorrect event handlers?

2
  • From where you setting ShowControl property? Have you implemented this property using ViewState? Commented May 13, 2014 at 18:26
  • That property is set from parent page. Yes, the property has been implemented using viewstate Commented May 13, 2014 at 18:42

1 Answer 1

1

Following is the working example:

ControlA:

public partial class ControlA : System.Web.UI.UserControl
{
    public bool ShowControl
    {
        get
        {
            if (this.ViewState["ShowControl"] == null)
                return false;
            else
                return (bool)this.ViewState["ShowControl"];
        }
        set
        {
            this.ViewState["ShowControl"] = value;
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (this.ShowControl)
        {
            var controlB = (ControlB)this.LoadControl("ControlB.ascx");
            controlB.FileUploadingComplete += controlB_FileUploadingComplete;
            this.pnl1.Controls.Add(controlB);
        }
    }

    void controlB_FileUploadingComplete(object sender, EventArgs e)
    {
        //throw new NotImplementedException();
        Trace.Write("file upload completed");
    }
}

ControlB:

public partial class ControlB : System.Web.UI.UserControl
{
    public event EventHandler FileUploadingComplete;
    protected void OnFileUploadingComplete()
    {
        if (this.FileUploadingComplete != null)
            this.FileUploadingComplete(this, EventArgs.Empty);
    }

    protected void btn1_Click(object sender, EventArgs e)
    {
        this.OnFileUploadingComplete();
    }
}

Page (has ControlA present):

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        this.ControlA1.ShowControl = true;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.