System.Web.UI.Page Inheritance

D

dan

I have created a class called BasePage that inherits from
System.Web.UI.Page so that I can implement a "template"
for all pages in an app. My BasePage class is in a class
library project and there is also a web app in the same
solution that has a project reference back to the class
library.

When I create a new web form in VS.NET, I simply change
the code behind class to inherit from BasePage instead of
System.Web.UI.Page.

Everything works great until I try to open a web form in
design mode... Then I get the following error.
********************************************************
The file could not be loaded into the Web Forms designer.
Please correct the following error and then try loading it
again.

An exception occured while trying to create and instance
of DDN.ClassLib.BaseClasses.BasePage. The exception
was "Object reference not set to an instance of an
object.".

Make sure all of the classes used in the page are built or
referenced in the project. Click Help for more information.
********************************************************

What is this all about? Everything works great when I
compile the classlibrary (with the BasePage class) and the
project. What is Visual Studio looking for?

Any help is greatly appreciated!

Here is the (simplified) code from my BasePage.cs:
*********************************************************
using System;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

namespace DDN.ClassLib.BaseClasses
{
public class BasePage : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label
lblPgTitle;
public PageUtilities pgUtil = new
PageUtilities();

public string PgTitle
{
get
{
return ViewState
["PgTitle"] == null ? string.Empty : (string) ViewState
["MsgPgTitle"];
}
set
{
ViewState["PgTitle"] =
value;
lblPgTitle.Text = value;
}
}

protected override void OnInit
(System.EventArgs e)
{
BuildPage( GenerateHtmlForm() );
this.DataBind();
ddRelTasks.Attributes.Add
("onChange", "RelTask();");
base.OnInit(e);
}

private HtmlForm GenerateHtmlForm()
{
HtmlForm form = new HtmlForm();
form.ID = "Form1";
AddControlsFromDerivedPage(form);
return form;
}

private void AddControlsFromDerivedPage
(HtmlForm form)
{
int count = this.Controls.Count;
for( int i = 0; i<count; ++i )
{
System.Web.UI.Control
ctrl = this.Controls[0];
form.Controls.Add( ctrl );
this.Controls.Remove(
ctrl );
}
}
private void BuildPage( HtmlForm form )
{

this.Controls.AddAt( 0, new
LiteralControl( @"
<html>
<head>
<title>MyPage</title>
<link
rel='stylesheet' href='/def/inc_style.css' type='text/css'>
</head>
<body>"));

this.Controls.Add( form );

form.Controls.AddAt(1, new
LiteralControl( @"
<TABLE WIDTH='1000'
BORDER='1' CELLPADDING='0' CELLSPACING='0' BGCOLOR='white'
ID='Table1'>
<TBODY>
<TR>

<TD VALIGN='top'>

<TABLE WIDTH='100%' BORDER='0' CELLPADDING='0'
CELLSPACING='0' ID='Table2' BGCOLOR='white'>

<TBODY>

<TR class=darkbackcell>

<TD VALIGN='top'>"));

lblPgTitle = new Label();
form.Controls.AddAt(8,
lblPgTitle );
form.Controls.AddAt
(form.Controls.Count, new LiteralControl( @"


</td>

</tr>

</TABLE>

</TD>

</TR>

</TABLE>

<br>

</TD>

</TR>
</TABLE>
</TD>
</TR>
</TABLE>
<TABLE ID='Table9'><TR><TD
HEIGHT='25' COLSPAN='2' VALIGN='top'></TD></TR></TABLE>
"));

this.Controls.Add(new
LiteralControl(@"</body></html>"));
}
}
}
 
C

Craig Deelsnyder

dan said:
I have created a class called BasePage that inherits from
System.Web.UI.Page so that I can implement a "template"
for all pages in an app. My BasePage class is in a class
library project and there is also a web app in the same
solution that has a project reference back to the class
library.

When I create a new web form in VS.NET, I simply change
the code behind class to inherit from BasePage instead of
System.Web.UI.Page.

Everything works great until I try to open a web form in
design mode... Then I get the following error.
********************************************************
The file could not be loaded into the Web Forms designer.
Please correct the following error and then try loading it
again.

An exception occured while trying to create and instance
of DDN.ClassLib.BaseClasses.BasePage. The exception
was "Object reference not set to an instance of an
object.".

Make sure all of the classes used in the page are built or
referenced in the project. Click Help for more information.
********************************************************

What is this all about? Everything works great when I
compile the classlibrary (with the BasePage class) and the
project. What is Visual Studio looking for?

Any help is greatly appreciated!

Here is the (simplified) code from my BasePage.cs:
*********************************************************
using System;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

namespace DDN.ClassLib.BaseClasses
{
public class BasePage : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label
lblPgTitle;
public PageUtilities pgUtil = new
PageUtilities();

public string PgTitle
{
get
{
return ViewState
["PgTitle"] == null ? string.Empty : (string) ViewState
["MsgPgTitle"];
}
set
{
ViewState["PgTitle"] =
value;
lblPgTitle.Text = value;
}
}

protected override void OnInit
(System.EventArgs e)
{
BuildPage( GenerateHtmlForm() );
this.DataBind();
ddRelTasks.Attributes.Add
("onChange", "RelTask();");
base.OnInit(e);
}

private HtmlForm GenerateHtmlForm()
{
HtmlForm form = new HtmlForm();
form.ID = "Form1";
AddControlsFromDerivedPage(form);
return form;
}

private void AddControlsFromDerivedPage
(HtmlForm form)
{
int count = this.Controls.Count;
for( int i = 0; i<count; ++i )
{
System.Web.UI.Control
ctrl = this.Controls[0];
form.Controls.Add( ctrl );
this.Controls.Remove(
ctrl );
}
}
private void BuildPage( HtmlForm form )
{

this.Controls.AddAt( 0, new
LiteralControl( @"
<html>
<head>
<title>MyPage</title>
<link
rel='stylesheet' href='/def/inc_style.css' type='text/css'>
</head>
<body>"));

this.Controls.Add( form );

form.Controls.AddAt(1, new
LiteralControl( @"
<TABLE WIDTH='1000'
BORDER='1' CELLPADDING='0' CELLSPACING='0' BGCOLOR='white'
ID='Table1'>
<TBODY>
<TR>

<TD VALIGN='top'>

<TABLE WIDTH='100%' BORDER='0' CELLPADDING='0'
CELLSPACING='0' ID='Table2' BGCOLOR='white'>

<TBODY>

<TR class=darkbackcell>

<TD VALIGN='top'>"));

lblPgTitle = new Label();
form.Controls.AddAt(8,
lblPgTitle );
form.Controls.AddAt
(form.Controls.Count, new LiteralControl( @"


</td>

</tr>

</TABLE>

</TD>

</TR>

</TABLE>

<br>

</TD>

</TR>
</TABLE>
</TD>
</TR>
</TABLE>
<TABLE ID='Table9'><TR><TD
HEIGHT='25' COLSPAN='2' VALIGN='top'></TD></TR></TABLE>
"));

this.Controls.Add(new
LiteralControl(@"</body></html>"));
}
}
}
It's trying to run your init event, etc. to let you see it in the
designer. I'd check to see if the current HttpContext is null before
doing your BuildPage, etc. It should be able to display then hopefully
(not your template, just the derived page HTML)....
 
M

MNF

Craig Deelsnyder said:
It's trying to run your init event, etc. to let you see it in the
designer. I'd check to see if the current HttpContext is null before
doing your BuildPage, etc. It should be able to display then hopefully
(not your template, just the derived page HTML)....

Winforms.Form has DesignMode property that you can check in your base
class methods to identify is it in Run mode or Design mode. I am not
aware, is something similar exists in WebForms.Page.
Does anyone know?

Michael Freidgeim.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,708
Latest member
SherleneF1

Latest Threads

Top