Scenario: A layout has controller rendering and it is in
main placeholder. When the page loads and hits the action, we are trying to insert an existing WFFM form to the same placeholder as given in this link.
This way, we have the option to conditionally load different forms to the same placeholder.
In above mentioned article, it states that, it is possible to insert an existing form onto a page using the code-behind class. By getting hold of placeholder and adding a control to it.
But when the page loads and hits the controller action, this code segment's 3rd line gives
null reference error
FormRender fr = new FormRender(); fr.FormID = "5D9E85F3-5E03-49A7-A136-93269DEA22A7";//form item id Sitecore.Context.Page.GetPlaceholder("main").Controls.Add(fr);
The reason is,
Sitecore.Context.Page returns 0 placeholder, hence null reference exception as '
GetPlaceholder("main") gives null.
But this code segment, returns placeholder, taken from here
var placeholderName = "my-placeholder"; var renderingReferences = Sitecore.Context.Item.Visualization.GetRenderings(Sitecore.Context.Device, true); var renderingsInPlaceholder = renderingReferences.Where(r => r.Placeholder.StartsWith(placeholderName, StringComparison.OrdinalIgnoreCase)); var numberOfRenderingsInPlaceholder = renderingsInPlaceholder.Count();
Yes, we can get hold of the placeholder but then how can we add a control/form to this placeholder?