Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transfer value to dynamically loaded web user control

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);

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

like image 716
Novkovski Stevo Bato Avatar asked Dec 03 '25 19:12

Novkovski Stevo Bato


1 Answers

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!";
}
like image 164
npclaudiu Avatar answered Dec 05 '25 09:12

npclaudiu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!