开发者

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

开发者 https://www.devze.com 2023-02-09 23:46 出处:网络
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:

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)
    {
        cas开发者_开发知识库e 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

0

精彩评论

暂无评论...
验证码 换一张
取 消