3

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?

3 Answers 3

5

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!";
}
Sign up to request clarification or add additional context in comments.

Comments

0

You could use Page.Findcontrol(...) to get a control in the parent page.

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

Just make sure you set the ID in the code, so you can access it. Like:

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

Comments

0

cast in your control class type, you need to create public properties for controls (if you want to transfer data to controls)

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

userControl.yourpublicproperty ="some text";

Page.Controls.Add(userControl);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.