Wednesday, April 18, 2007

Loading UserControl at Runtime and Calling Function in UserControl

If you want to load ASP.NET User Control at runtime and calling methods in the UserControl class (such as filling data in the control), you can use the following code. Use reflection to get method in the UserControl then invoke it.

UserControl uc = null;


uc = (UserControl)LoadControl("UserControl.ascx");

Type ucType = uc.GetType();

MethodInfo mi = ucType.GetMethod("FillData");

string returnValue=(string)mi.Invoke(uc, new object[] { arg1, arg2, arg3});

No comments: