转让价值来动态加载web用户控件 [英] Transfer value to dynamically loaded web user control

查看:136
本文介绍了转让价值来动态加载web用户控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我ASPX页面,在preINIT我检查whitch用户控件加载。

I have ASPX page where on preinit i check whitch user control to load.

 control = "~/templates/" + which + "/master.ascx";

然后在页面加载,我加载控制

Then on pageload, i load that control

 Control userControl = Page.LoadControl(control);
 Page.Controls.Add(userControl);

我怎么能转移动态加载的用户控件,从ASPX到ASCX?

How i can transfer dynamically loaded user control, from aspx to ascx?

推荐答案

您可以创建一个由所有的自定义的控件实现的接口。这样,您就可以转换到该接口并使用它,通过它来传递您的数据。考虑这个例子:

You can create an interface that is implemented by all your custom controls. This way, you can cast to that interface and use it to pass your data through it. Consider this example:

public interface ICustomControl
{
    string SomeProperty { get; set; }
}

...和你的控制:

... and your controls:

public class Control1 : Control, ICustomControl
{
    public string SomeProperty
    {
        get { return someControl.Text; }
        set { someControl.Text = value; }
    }

    // ...
}

现在,你可以做这样的事情:

Now, you can do something like this:

Control userControl = Page.LoadControl(control);
Page.Controls.Add(userControl);

if (userControl is ICustomControl)
{
    ICustomControl customControl = userControl as ICustomControl;
    customControl.SomeProperty = "Hello, world!";
}

这篇关于转让价值来动态加载web用户控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆