Kuntady Nithesh
Kuntady Nithesh

Reputation: 11721

How to load a UserControl dynamically with LoadControl Method (Type, object[])

I am a bit new to user controls. my user control class is ucDefault . I dont have any constructors explicitly specified . I have to load my user control with the default constructor . How can i do it ?

Upvotes: 0

Views: 2956

Answers (1)

KV Prajapati
KV Prajapati

Reputation: 94625

Try,

Control control=LoadControl("~/UserControlFile.ascx");

My answers of threads posted by you:

  1. How to load a web usercontrol from a physical path rather than a virtual path
  2. Loading web user controls from blob storage in asp.net

Edit:

Here is a TestControl.cs located at App_code

public class TestControl : UserControl
{
    public TestControl() { }
    public TestControl(string message)
    {
        SayHello = message;
    }
    public string SayHello { get; set; }

    public override void RenderControl(HtmlTextWriter writer)
    {
        base.RenderControl(writer);
        writer.Write(SayHello);
    }
}

and code to loads/creates a control object:

TestControl tc = (TestControl)LoadControl(typeof(TestControl), new object[] { "Hello Buddy" });

Upvotes: 2

Related Questions