ASPX templates
Version 5.x > ASPX templates > Adding webpart dynamically View modes: 
User avatar
Member
Member
gregb - 12/5/2011 4:32:38 PM
   
Adding webpart dynamically
Hello,

I'm trying to add 1 to more webparts to a page based on the amount of folders I have under the page. For example: I have a page called January photos; In the CMS I will have folders under this page for whichever date.
January
-5
-15
-20

So on the page there should be 3 webparts 1 for each date.

On the front page:

<%@ Register Src="~/CMSWebParts/Viewers/Effects/lightbox.ascx" TagName="lightbox" TagPrefix="uc1" %>

<asp:Content ID="Content3" ContentPlaceHolderID="columnTwoContent" Runat="Server">

<div class="galleryLists">
<asp:PlaceHolder runat="server" ID="galleries" visible="false" />
</div>
</asp:Content>



In the code behind I'm attempting this:

string parentNodeAliasPath = CMSContext.CurrentPageInfo.NodeAliasPath.ToLower();
parentNodeAliasPath = parentNodeAliasPath.Trim() + "/%";

CMS.TreeEngine.TreeProvider tree = new CMS.TreeEngine.TreeProvider();
DataSet ds = tree.SelectNodes(CMSContext.CurrentSiteName, parentNodeAliasPath, CMSContext.CurrentDocument.DocumentCulture, false, "CMS.Folder");


foreach (DataRow dr in ds.Tables[0].Rows)
{
string folderName = dr["NodeName"].ToString();

galleries.Controls.Add(new LiteralControl("<p class='galleryTitle'>" + folderName + "</p>"));

CMSWebParts_Viewers_Effects_lightbox lightbox = new CMSWebParts_Viewers_Effects_lightbox();


lightbox.ID = folderName;
lightbox.ClassNames = "Client.Asset";
lightbox.Path = parentNodeAliasPath.Replace("/%", "/" + folderName);
lightbox.TransformationName = "Client.Asset.ImagesGalleryListView";
lightbox.SelectedItemTransformationName = "Client.Asset.ImagesGalleryDetailedView";
lightbox.LightBoxPermanentNavigation = true;
lightbox.LightBoxGroup = folderName;
galleries.Controls.Add(lightbox);
galleries.Visible = true;
}


However, it crashes with:
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

Line 93: {
Line 94: SetValue("ClassNames", value);
Line 95: repItems.ClassNames = value;
Line 96: }


I'm a bit lost at this point. Any help would be appreciated.

User avatar
Member
Member
gregb - 12/7/2011 8:24:06 AM
   
RE:Adding webpart dynamically
So I've made some head way with this. If anyone is interest in the code, I've managed to get this to work like so:

 string parentNodeAliasPath = CMSContext.CurrentPageInfo.NodeAliasPath.ToLower();
parentNodeAliasPath = parentNodeAliasPath.Trim() + "/%";

CMS.TreeEngine.TreeProvider tree = new CMS.TreeEngine.TreeProvider();
DataSet ds = tree.SelectNodes(CMSContext.CurrentSiteName, parentNodeAliasPath, CMSContext.CurrentDocument.DocumentCulture, false, "CMS.Folder");


foreach (DataRow dr in ds.Tables[0].Rows)
{
string folderName = dr["NodeName"].ToString();

galleries.Controls.Add(new LiteralControl("<p class='galleryTitle'>" + folderName + "</p>"));

CMSWebParts_Viewers_Effects_lightbox lightbox = (CMSWebParts_Viewers_Effects_lightbox)LoadControl("~/CMSWebParts/Viewers/Effects/lightbox.ascx");

lightbox.ClassNames = "Client.Asset";
lightbox.Path = parentNodeAliasPath.Replace("/%", "/" + folderName + "/%");
lightbox.TransformationName = "Client.Asset.ImagesGalleryListView";
lightbox.SelectedItemTransformationName = "Client.Asset.ImagesGalleryDetailedView";
lightbox.LightBoxPermanentNavigation = true;
lightbox.LightBoxGroup = folderName;
galleries.Controls.Add(lightbox);
galleries.Visible = true;
}


This method works, I've bolded the code that was wrong.

However I'm facing another issue that I don't understand.
The webparts load great on the webpage, the thumbnail appears unfortunately when you click on one of them the lightbox pop up appears and the loading gif displays but the image never shows.

This perplexes me since the transformation for the thumbnails seem to work fine but the transformation for the selected item isn't.

Anyone have any ideas?

User avatar
Member
Member
gregb - 12/7/2011 8:59:50 AM
   
RE:Adding webpart dynamically
Looks like I figured it out.

The above code was placed in: "if (!IsPostback)", which caused the issue. Taking it out of the If fixed it.

Everything is working now.

User avatar
Kentico Support
Kentico Support
kentico_zdenekc - 12/8/2011 3:12:48 AM
   
RE:Adding webpart dynamically
Hello Greg,

Thank you for sharing your solution with us.
Yes, the code has to run on postback too...

Regards,
Zdenek C.