Transfer value to dynamically loaded web user control

I have an ASPX page where on preinit I check whitch user management for download.

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

Then in pageload load this control

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

How can I pass dynamically loaded user control, from aspx to ascx?

+3
source share
3 answers

You can create an interface that will be implemented by all custom controls. Thus, you can use this interface and use it to transfer 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!";
}
+5
source

Page.Findcontrol(...), .

. http://msdn.microsoft.com/en-us/library/31hxzsdw.aspx

, , . :

Control userControl = Page.LoadControl(control);
userControl.ID = "ctlName";
Page.Controls.Add(userControl);
0

in your type of control class you need to create public properties for the controls (if you want to pass data to the controls)

var userControl = (master)Page.LoadControl(control);

userControl.yourpublicproperty ="some text";

Page.Controls.Add(userControl);
0
source

All Articles