I have a requirement where I have to loop through all the web parts in my page layout whether they are in WebPartZone or not and render only the webpart instead of rendering webpart zone.
I am able to loop through for the WebPartZone, but unfortunately I am not able to loop through the webparts inside the page.
I have written this code to find the webpart in a page:
SPLimitedWebPartManager splwManager = web.GetLimitedWebPartManager(HttpContext.Current.Request.Url.ToString(), System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);
foreach (WebPart wprt in splwManager.WebParts.OfType<WebPart>())
{
wprt.RenderControl(writer);
}
but here I am getting the problem that my wprt.Page object is null. So it's giving me error that "Parameter value cannot be null. Parameter Required: Page"
Here is the code I have written for WebPartZone:
WebPartZone webPartZone = Control as WebPartZone;
if (webPartZone != null)
{
foreach (WebPart wp in webPartZone.WebParts)
{
wp.RenderControl(writer);
}
}
Looking for someone's help.