Portal Engine Questions on portal engine and web parts.
Version 6.x > Portal Engine > Is that possible to have one web part control another web part? View modes: 
User avatar
Member
Member
steven4733-gmail - 3/1/2012 12:09:31 PM
   
Is that possible to have one web part control another web part?
What I want to do is to use one web part to control the visible attribute of another one. So in web part 1, if I choose A, then A web part shows up, if I choose B, then B web part shows up etc. Is that possible? Since each web part is complex enough to be a separated one, I really don't want to combine them together as on web part. But if it is difficult to have such a relation among web parts in a same page, I'll just write all my code into one web part. Please advise.

User avatar
Member
Member
kentico_michal - 3/2/2012 4:40:43 AM
   
RE:Is that possible to have one web part control another web part?
Hello,

Well, you can take advantage of CMS.GlobalHelper.RequestStockHelper class which gives you ability to insert and get items into HttpContext.Current.Items collection using Add/GetItem methods. You can add a reference to a web part to the collection and retrieve it in the code of another web part. I will give you a simple example.

Let’s assume that I have two web parts (WebPartA and WebPartB) and I want to change the Visible property of the WebPartB fromt the code behind of the WebPartA.

WebPartA contains a button:

<asp:Button runat="server" ID="btn" OnClick="btn_Click" />

In the OnClick handler I want to change the Visible property of the WebPartB.

First, you need to add the WebPartB web part into the RequestStockHelper collection. You can do so in the OnInit method of the WebPartB:

protected override void OnInit(EventArgs e)
{
CMS.GlobalHelper.RequestStockHelper.Add("WebPartB", this);
base.OnInit(e);
}

Now you can access the WebPartB in the OnClick handler of the WebPartA and change its Visible (or any other) property:

protected void btn_Click(object sender, EventArgs e)
{
CMSAbstractWebPart WebPartB = CMS.GlobalHelper.RequestStockHelper.GetItem("WebPartB") as CMSAbstractWebPart;
if (something)
{
WebPartB.Visible = false;
}
}

I hope this makes sense to you.

If you need more information about RequestStockHelper class please take a look at API reference: Kentico CMS API

Best regards,
Michal Legen