开发者

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

I am a bit new to user controls. my user 开发者_JAVA百科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 ?


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" });
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜