Penguen
Penguen

Reputation: 17298

How to access user control properties if i load it programatically?

i loaded my user control (.ascx) programmatically like: LoadControl("~/Controls/mycontrol.ascx"). Every thing was ok until today when i added two members to my control:

public StuffType stuffType { get; set; }

protected void Page_Load(object sender, EventArgs e)
{
    switch (stuffType)
    {
        case CardType.A:
            FillGvStuff();
            break;
        case CardType.B:
            FillGvExStuff();
            break;
        default:
            break;
    }       
}

how can i access StuffType?

i found a kind of solution.

Upvotes: 0

Views: 1088

Answers (1)

davidsleeps
davidsleeps

Reputation: 9513

I think you'd do something like this:

MyControl ctrl = (MyControl)LoadControl("~/Controls.mycontrol.ascx");
ctrl.stuffType = ...;
// put control somehwere

Basically, when you load it, assign it to a variable and cast it as its type and you should then have access to its methods and properties

You might also want to move your page_load event into page_prerender so that you are definetly setting the property before the page_load event occurs in the control

Upvotes: 5

Related Questions