Portal Engine Questions on portal engine and web parts.
Version 5.x > Portal Engine > How can I access a webpart inside usercontrol? View modes: 
User avatar
Member
Member
leandroncbrito-gmail - 8/19/2011 11:12:54 AM
   
How can I access a webpart inside usercontrol?
Hello guys,

I have a webpart in ~/CMSWebParts/MyWebParts/webpart.ascx.
I have a usercontrol in ~/MySite/UserControls/myusercontrol.ascx.

My webpart has the usercontrol inside it using <% Reference %>.

My usercontrol has a button and the event need call a method of parent webpart, by example:

webpart method = SearchResults();

usercontrol event button_Click()
{
//here I need acess the SearchResults() method but I can't instance the webpart for do a cast on this.NamingContainer or this.Parent.Parent and use this new instance for call SearchResults().
}

How can I cast the webpart(usercontrol) inside my custom usercontrol code behind?

Thanks, and sorry about my English.
Leandro

User avatar
Kentico Developer
Kentico Developer
kentico_ivanat - 8/22/2011 4:03:46 AM
   
RE:How can I access a webpart inside usercontrol?
Hi,

you have two possibilities to achieve your goal. Both options are described below:

A: using event handler

In your user control you need to create an event handler:

///<summary>Occurs before data is loaded from the database in the form.</summary>
public delegate void OnButtonClickHandler();

///<summary>OnButtonClick event.</summary>
public event OnButtonClickHandler OnButtonClick;


Then in a button click method yo need to call this event:

protected void Button1_Click(object sender, EventArgs e)
{
if (OnButtonClick != null)
{
OnButtonClick();
}
}


In your web part you need to register above event:


// on markup page is registerd your control
//<%@ Register Src="~/CMSWebParts/Text/nested.ascx" TagName="mycontrol" //TagPrefix="cms" %>
//<cms:mycontrol ID="controlID" runat="server" />

protected void SetupControl()
{
if (this.StopProcessing)
{
// Do not process
}
else
{
this.contolID.OnButtonClick += new CMSWebParts_Text_nested.OnButtonClickHandler(controlID_OnButtonClick);
}
}


And finally you need to implement the event:

void list_OnButtonClick()
{
Response.Write("implement me");
}


B: using one of standard method implemented in each web part. In this example is used ReloadData method.

In your control:

CMSAbstractWebPart abs = this.Parent as CMSAbstractWebPart;
if (abs != null)
{
/// here you can call some general method
abs.ReloadData();
}

In yourweb part:



/// <summary>
/// Reloads the control data
/// </summary>
public override void ReloadData()
{
base.ReloadData();
SetupControl();
Response.Write("add your custmo code");
}


Best regards,
Ivana Tomanickova

User avatar
Member
Member
Aon_Vlado - 2/3/2012 9:16:42 AM
   
RE:How can I access a webpart inside usercontrol?
This is helpful - thank you. Could you please post a snippet how to call a method form a different web part on the same page? Obviously I cannot do:

CMSAbstractWebPart abs = this.Parent as CMSAbstractWebPart;

How do I find the ID of the web part I need?

Thanks.

User avatar
Kentico Developer
Kentico Developer
kentico_ivanat - 2/7/2012 5:56:11 AM
   
RE:How can I access a webpart inside usercontrol?
Hi,

you can use page info to get information about some template (and web parts which are used on this template):


PageInfo pi = PageInfoProvider.GetPageInfo(CMS.CMSHelper.CMSContext.CurrentSiteName, CMS.CMSHelper.CMSContext.CurrentDocumentParent.NodeAliasPath, "en-gb", "", false, ConnectionHelper.ContextConnection);

PageTemplateInfo iPageTemp = pi.PageTemplateInfo;

// Get zone
CMS.PortalEngine.WebPartZoneInstance wpz = iPageTemp.GetZone("Variables");

// Get web part
CMS.PortalEngine.WebPartInstance iWebPart = wpz.GetWebPart("Variables_1");

string iProperty = (string)iWebPart.GetValue("Root");


Best regards,
Ivana Tomanickova