In the codebehind of a page.
How do i get hold of a webpart that exist on the page, then add properties to that webpart using c#.
The webpart exists withing a webpartzone.
Do i need to do anything with SPWebPartManager?
In the codebehind of a page.
How do i get hold of a webpart that exist on the page, then add properties to that webpart using c#.
The webpart exists withing a webpartzone.
Do i need to do anything with SPWebPartManager?
Use SPWeb.GetLimitedWebPartManager. The following example demonstrates updating a property in a list view web part:
using (SPLimitedWebPartManager webPartManager =
SPContext.Current.Web.GetLimitedWebPartManager("default.aspx",
PersonalizationScope.Shared))
{
try
{
foreach (WebPart webPart in webPartManager.WebParts)
{
if (webPart.Title == "Web Part To Update")
{
ListViewWebPart listViewWebPart = (ListViewWebPart)webPart;
// TODO: Set property on web part
webPartManager.SaveChanges(listViewWebPart);
break;
}
}
}
finally
{
webPartManager.Web.Dispose();
}
}
Instead of default.aspx
you need to use the name of the current page relative to the SPWeb.