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:
- How to load a web usercontrol from a physical path rather than a virtual path
- 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" });
精彩评论