简体   繁体   中英

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 .

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

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM