简体   繁体   English

如果我以编程方式加载它,如何访问用户控件属性?

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

i loaded my user control (.ascx) programmatically like: LoadControl("~/Controls/mycontrol.ascx") . 我以编程方式加载了我的用户控件(.ascx),例如: 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? 如何访问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 您可能还需要将page_load事件移到page_prerender中,以便在控件中的page_load事件发生之前明确设置属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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