WebPart - UserControl Loaded Multiple Times and GridView Event not Triggerd

Creating user control declaratively in your WebPart won't give your alot of problem. But when you try to load pro grammatically a user control (ascx) into your webparts using something like this:-

Your WebPartPage

Control uc = this.LoadControl(@"webparts\CompanyNews.ascx");
uc.ID = "webPartyMan";
GenericWebPart wp2 = wp.CreateWebPart(uc);
wp.AddWebPart(wp2, LeftZone, 1);

You found that you loaded it multiple times and somethings GridView events are not triggered.
What the trick?

You only load your ascx once (you won't load it ever again) and right after you dynamically load it call SetPersonalizationDirty(); This is done using your custom WebPartManager;

This is a very simplified version of your custom webPartManager (loadPersonalizationBlob and savePersonalizationBlob)

namespace CoreWebPart
{
public class myWebPartManager : WebPartManager
{

public void SetDirty()
{
SetPersonalizationDirty();
}
}

} ///// end namespace


Then your webpart page would look something like this:-

CoreWebPart .myWebPartManager wp = (CoreWebPart .wingTipManager) CoreWebPart .myWebPartManager .GetCurrentWebPartManager(this.Page);

//// create control code to load this only once /////

Control uc = this.LoadControl(@"webparts\CompanyNews.ascx");
uc.ID = "webPartyMan";
GenericWebPart wp2 = wp.CreateWebPart(uc);
wp.AddWebPart(wp2, LeftZone, 1);
wp.SetDirty();

//// create control code to load this only once /////

Please make sure you use myWebPartManager as oppose to the standard WebPartManager in your ASPX page.

Comments

Popular posts from this blog

The specified initialization vector (IV) does not match the block size for this algorithm