Portal Engine Questions on portal engine and web parts.
Version 5.x > Portal Engine > Instance of webpart View modes: 
User avatar
Member
Member
Leandro Brito - 9/19/2011 8:46:56 AM
   
Instance of webpart
How to create an instance of a webpart and call a public method?

The document:
CMSWebParts>MyWebParts>wucHelloWorld.ascx

I'm trying do something like that, but I can't access the webpart.
e.g.

CMSWebParts_MyWebParts_wucHelloWorld hw = new CMSWebParts_MyWebParts_wucHelloWorld();

hw.WriteHelloWorld();

Exists an easy for to do this?

Thx!

User avatar
Member
Member
kentico_michal - 9/20/2011 3:40:09 AM
   
RE:Instance of webpart
Hello,

You can use the following code:

Control usercontrolobject = Page.LoadControl("<virtual path>");

System.Type typeUserControl = null;
System.Reflection.MethodInfo methodUserControl = null;

typeUserControl = usercontrolobject.GetType();

methodUserControl = typeUserControl.GetMethod("WriteHelloWorld");

string returnValue = (methodUserControl.Invoke(usercontrolobject, null)).ToString();


I hope this will help you.

Best regards,
Michal Legen