How to share information between web parts or controls

This article describes the how to access fields from another custom web part or control within your custom web part or control.
If you are using a custom web part and want to modify its behavior based on the value of another web part, you can use session variables to share information between the web parts. As an example, let’s suppose you’re using a customized CMS Repeater web part and would like to change the behavior of a custom static text web part, based on the type of documents displayed in the repeater.
You can modify the SetupControl method of the repeater web part. At the end of the else statement, add the following:

CMS.GlobalHelper.SessionHelper.SetValue("CustomRepeaterClassNames", repItems.ClassNames);

You can now retrieve this value from another web part using the following code:

string customRepeaterClassNames = CMS.GlobalHelper.SessionHelper.GetValue("CustomRepeaterClassNames");

To make sure the value is populated, you should retrieve this later in the page life cycle than the value is set, such as within the OnPreRender method of the static text web part.
Using this method, the values are stored for the lifetime of the session. If you would like to only retain the data for a single request, you can use a similar approach, but make use of this class instead:

CMS.GlobalHelper.RequestStockHelper.Add("CustomRepeaterClassNames", repItems.ClassNames);


Applies to: Kentico CMS 6.x, 5.x 
Share this article on   LinkedIn