Runtime error with custom web control using a template

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I get this error when i run my application with a custom web control that uses a template.
ERROR: "Page cannot be null. Please ensure that this operation is being performed in the context of an ASP.NET request."
Specifically the error occurs when i added the <span style="font-family:inherit; font-size:13px; line-height:16px; text-align:left CollapsiblePanelContainer<span style="font-size:13px; line-height:16px; text-align:left . see code below.
Custom Control class file:

<div style="color:black; background-color:white
<pre><span style="color:blue using System;
<span style="color:blue using System.Collections.Generic;
<span style="color:blue using System.ComponentModel;
<span style="color:blue using System.Linq;
<span style="color:blue using System.Text;
<span style="color:blue using System.Web;
<span style="color:blue using System.Web.UI;
<span style="color:blue using System.Web.UI.WebControls;
<span style="color:blue using AjaxControlToolkit;

<span style="color:blue namespace CustomControls
{
[DefaultProperty(<span style="color:#a31515 "Text")]
[ToolboxData(<span style="color:#a31515 "<{0}:CollapsiblePanel runat=server></{0}:CollapsiblePanel>")]
<span style="color:blue public <span style="color:blue class CollapsiblePanel : WebControl, INamingContainer
{
<span style="color:blue private Label TitleLabel = <span style="color:blue new Label();
<span style="color:blue private PlaceHolder ContentPlaceHolder = <span style="color:blue new PlaceHolder();
<span style="color:blue private Panel ContainerPanel = <span style="color:blue new Panel();

[Category(<span style="color:#a31515 "Appearance")]
[DefaultValue(<span style="color:#a31515 "Title")]
[Localizable(<span style="color:blue true)]
<span style="color:blue public String Title
{
<span style="color:blue get { <span style="color:blue return TitleLabel.Text; }
<span style="color:blue set { TitleLabel.Text = value; }
}

[Category(<span style="color:#a31515 "Appearance")]
[DefaultValue(<span style="color:#a31515 "~/assets/collapse.jpg")]
[Localizable(<span style="color:blue true)]
<span style="color:blue public String CollaspedImage { <span style="color:blue get; <span style="color:blue set; }

[Category(<span style="color:#a31515 "Appearance")]
[DefaultValue(<span style="color:#a31515 "~/assets/expand.jpg")]
[Localizable(<span style="color:blue true)]
<span style="color:blue public String ExpandedImage { <span style="color:blue get; <span style="color:blue set; }

[TemplateContainer(<span style="color:blue typeof(CollapsiblePanel))]
[PersistenceMode(PersistenceMode.InnerProperty)]
[Browsable(<span style="color:blue true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
<span style="color:blue public ITemplate ContentTemplate { <span style="color:blue get; <span style="color:blue set; }

<span style="color:blue void Page_Init()
{
<span style="color:blue if (ContentTemplate != <span style="color:blue null)
{
CollapsiblePanelContainer container = <span style="color:blue new CustomControls.CollapsiblePanelContainer();
ContentTemplate.InstantiateIn(container);
ContentPlaceHolder.Controls.Add(container);
}
}

<span style="color:blue protected <span style="color:blue override <span style="color:blue void RenderContents(HtmlTextWriter output)
{
TitleLabel.ID = <span style="color:#a31515 "TitleLabel";


<span style="color:blue var Panel2 = <span style="color:blue new Panel();
Panel2.Height = 30;
Panel2.ID = <span style="color:#a31515 "Panel2";

<span style="color:blue var OuterDiv = <span style="color:blue new Panel();
OuterDiv.Style.Value = <span style="color:#a31515 "padding:5px; cursor: pointer; vertical-align: middle;";

<span style="color:blue var TitleDiv = <span style="color:blue new Panel();
TitleDiv.Style.Value = <span style="color:#a31515 "float: left;";

<span style="color:blue var MessageDiv = <span style="color:blue new Panel();
MessageDiv.Style.Value = <span style="color:#a31515 "float: left; margin-left: 20px;";

Label Label1 = <span style="color:blue new Label();
Label1.ID = <span style="color:#a31515 "Label1";
Label1.Text = <span style="color:#a31515 "(Show Details...)";

<span style="color:blue var ImageButtonDiv = <span style="color:blue new Panel();
ImageButtonDiv.Style.Value = <span style="color:#a31515 "float: right; vertical-align: middle;";

ImageButton Image1 = <span style="color:blue new ImageButton();
Image1.ID = <span style="color:#a31515 "Image1";
Image1.AlternateText = <span style="color:#a31515 "(Show Details...)";
Image1.ImageUrl = ExpandedImage;

<span style="color:blue var Panel1 = <span style="color:blue new Panel();
Panel1.ID = <span style="color:#a31515 "Panel1";
Panel1.Height = 0;

ContentPlaceHolder.ID = <span style="color:#a31515 "ContentPlaceHolder";

Panel1.Controls.Add(ContentPlaceHolder);

TitleDiv.Controls.Add(TitleLabel);
Panel2.Controls.Add(TitleDiv);

MessageDiv.Controls.Add(Label1);
Panel2.Controls.Add(MessageDiv);

ImageButtonDiv.Controls.Add(Image1);
Panel2.Controls.Add(ImageButtonDiv);

ContainerPanel.Controls.Add(Panel2);
ContainerPanel.Controls.Add(Panel1);

CollapsiblePanelExtender cpeDemo = <span style="color:blue new CollapsiblePanelExtender();
cpeDemo.ID = <span style="color:#a31515 "cpeDemo";
cpeDemo.ExpandControlID = <span style="color:#a31515 "Panel2";
cpeDemo.CollapseControlID = <span style="color:#a31515 "Panel2";
cpeDemo.Collapsed = <span style="color:blue true;
cpeDemo.TextLabelID = <span style="color:#a31515 "Label1";
cpeDemo.ImageControlID = <span style="color:#a31515 "Image1";
cpeDemo.ExpandedText = <span style="color:#a31515 "(Hide Details...)";
cpeDemo.CollapsedText = <span style="color:#a31515 "(Show Details...)";
cpeDemo.ExpandedImage = ExpandedImage;
cpeDemo.CollapsedImage = CollaspedImage;
cpeDemo.SuppressPostBack = <span style="color:blue true;
cpeDemo.TargetControlID = <span style="color:#a31515 "Panel1";

ContainerPanel.Controls.Add(cpeDemo); //If i comment this out, the control works fine.

<span style="color:blue if (Page != <span style="color:blue null)
{
ContainerPanel.RenderControl(output); <span style="color:green //errors here with:
<span style="color:green //"Page cannot be null. Please ensure that this operation is being performed in the context of an ASP.NET request."
<span style="color:green //Funny i thought i just tested for that...
}
}
}

[ParseChildren(<span style="color:blue true, <span style="color:#a31515 "ContentTemplate")]
[Browsable(<span style="color:blue true)]
<span style="color:blue public <span style="color:blue class CollapsiblePanelContainer : Panel, INamingContainer
{
<span style="color:blue public CollapsiblePanelContainer()
{

}
}
}
[/code]


Implementation in Web Page:

<div style="color:black; background-color:white
<pre><span style="background-color:yellow <%<span style="color:blue @ <span style="color:#a31515 Page <span style="color:red Language<span style="color:blue =<span style="color:blue "C#" <span style="color:red AutoEventWireup<span style="color:blue =<span style="color:blue "true" <span style="color:red CodeBehind<span style="color:blue =<span style="color:blue "Default.aspx.cs" <span style="color:red Inherits<span style="color:blue =<span style="color:blue "CustomControlsTest._Default" <span style="background-color:yellow %>

<span style="background-color:yellow <%<span style="color:blue @ <span style="color:#a31515 Register <span style="color:red Assembly<span style="color:blue =<span style="color:blue "AjaxControlToolkit" <span style="color:red Namespace<span style="color:blue =<span style="color:blue "AjaxControlToolkit" <span style="color:red TagPrefix<span style="color:blue =<span style="color:blue "asp" <span style="background-color:yellow %>

<span style="color:blue <!<span style="color:#a31515 DOCTYPE <span style="color:red html <span style="color:red PUBLIC <span style="color:blue "-//W3C//DTD XHTML 1.0 Transitional//EN" <span style="color:blue "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"<span style="color:blue >
<span style="background-color:yellow <%<span style="color:blue @ <span style="color:#a31515 Register <span style="color:red Assembly<span style="color:blue =<span style="color:blue "CustomControls" <span style="color:red TagPrefix<span style="color:blue =<span style="color:blue "customASP" <span style="color:red Namespace<span style="color:blue =<span style="color:blue "CustomControls"<span style="background-color:yellow %>

<span style="color:blue <<span style="color:#a31515 html <span style="color:red xmlns<span style="color:blue =<span style="color:blue "http://www.w3.org/1999/xhtml"<span style="color:blue >
<span style="color:blue <<span style="color:#a31515 head <span style="color:red runat<span style="color:blue =<span style="color:blue "server"<span style="color:blue >
<span style="color:blue <<span style="color:#a31515 title<span style="color:blue ><span style="color:blue </<span style="color:#a31515 title<span style="color:blue >
<span style="color:blue </<span style="color:#a31515 head<span style="color:blue >
<span style="color:blue <<span style="color:#a31515 body<span style="color:blue >
<span style="color:blue <<span style="color:#a31515 form <span style="color:red id<span style="color:blue =<span style="color:blue "form1" <span style="color:red runat<span style="color:blue =<span style="color:blue "server"<span style="color:blue >
<span style="color:blue <<span style="color:#a31515 div<span style="color:blue >
<span style="color:blue <<span style="color:#a31515 customASP<span style="color:blue :<span style="color:#a31515 CollapsiblePanel <span style="color:red runat<span style="color:blue =<span style="color:blue "server" <span style="color:red Title<span style="color:blue =<span style="color:blue "Test Title"<span style="color:blue >
<span style="color:blue <<span style="color:#a31515 ContentTemplate<span style="color:blue >
<span style="color:blue <<span style="color:#a31515 label<span style="color:blue >Hi There<span style="color:blue </<span style="color:#a31515 label<span style="color:blue >
<span style="color:blue </<span style="color:#a31515 ContentTemplate<span style="color:blue >
<span style="color:blue </<span style="color:#a31515 customASP<span style="color:blue :<span style="color:#a31515 CollapsiblePanel<span style="color:blue >
<span style="color:blue </<span style="color:#a31515 div<span style="color:blue >
<span style="color:blue </<span style="color:#a31515 form<span style="color:blue >
<span style="color:blue </<span style="color:#a31515 body<span style="color:blue >
<span style="color:blue </<span style="color:#a31515 html<span style="color:blue >
[/code]

<br/>
What am i doing wrong? Thanks for any help. :)



<br/>

View the full article
 
Back
Top