Create Custom PageControl - Object reference not set to an I

Posted by Community Admin on 04-Aug-2018 21:42

Create Custom PageControl - Object reference not set to an Instance of an object

All Replies

Posted by Community Admin on 07-Mar-2014 00:00

I am writing a custom web site migration routine to extract the contents of the div tags from one site and then create sitefinity pages dynamically using the PageManager API.

 I've had success automating the ContentBlock PageControl. However, we have a few custom controls that style the content blocks in a special way.  I am using the same technique as the ContentBlock control, however the page designer is rendering the control as "Object reference not set to an Instance of an object" in design mode.  When I click on Edit to view the properties of this control, it looks fine and contains the properties that I set.  When I try to navigate to the page that was created, it just bombs out with a site maintenance message.

 Here is the snippet of code that I am using.  Note, the code doesn't generate any errors, the page that was created programmatically does display an error:

pageNode.Page.Controls.Add(cphPrimaryContent);
 
var cphRelatedLinks = manager.CreateControl<PageControl>("~/CustomControls/RelatedLinks/RelatedLinks.ascx", "cphRelatedLinks");
cphRelatedLinks.ObjectType = typeof(RelatedLinks).FullName;
cphRelatedLinks.Caption = "Related links";
cphRelatedLinks.Description = "Related links";
 
var pcContent = manager.CreateProperty();
pcContent.Name = "Content";
pcContent.Value = "<ul><li><a href=\"http://www.google.com\">Related Link</a></li></ul>";
cphRelatedLinks.Properties.Add(pcContent);
 
pageNode.Page.Controls.Add(cphRelatedLinks);
 

This code is modeled after this code which works fine for ContentBlock:

 

var cphPrimaryContent = manager.CreateControl<PageControl>();
cphPrimaryContent.ObjectType = typeof(ContentBlock).FullName;
cphPrimaryContent.PlaceHolder = "cphPrimaryContent";
cphPrimaryContent.Caption = "Content block";
cphPrimaryContent.Description = "Content block";
 
var pcHhtml = manager.CreateProperty();
pcHhtml.Name = "Html";
pcHhtml.Value = "<p>awesome</p>";
cphPrimaryContent.Properties.Add(pcHhtml);
 
pageNode.Page.Controls.Add(cphPrimaryContent);

 

I am developing this for a Fortune 500 company with a very limited time/budget for this particular project. A prompt reply is appreciated.

Peace,
Brian

Posted by Community Admin on 11-Mar-2014 00:00

Hello , Brian!

Wild guess number one would be you are missing the placeholder for your control.

Posted by Community Admin on 11-Mar-2014 00:00

CreateControl has a constructor and the place holder (cphRelatedLinks) is being passed in.  I have tried several different combinations of creating the control.  A control always shows up in the place holder (in design mode), but it displays the error message "Object reference not set to an Instance of an object" rather than the content it is supposed to display.  

This thread is closed