access the base Page class from user controls??

D

Davíð Þórisson

Please can someone tell me how on earth to create an instance of my top
level (base) Page class so that I can access it's objects from an user
control?? Someone told me

public myParent = (default_aspx) this.Page;

where default_aspx is the class name of the base Page...
 
J

Jeffrey Palermo [MCP]

I make a User Control base class that does the following:
public new MyBasePage Page{
get { return (MyBasePage)this.Page; }
}

I use this all over the place.
of course (MyBasePage)this.Page works too.

Best regards,
Jeffrey Palermo
 
D

Davíð Þórisson

Jeffrey could you please take a look here since this is driving me cray...
this is the code I have in Initialise.aspx.cs (the user control), the error
I get is "CS1513: } expected"

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

public partial class Initialise_ascx
{
void Page_Load(object sender, System.EventArgs e)
{
public new MyBasePage Page
{
get { return (MyBasePage)this.Page; }
}
MyBasePage.myLiteral.Text = "asdafds";
}
}
 
S

Scott Allen

Hi Davíð:

You have a property defined inside of a method. I think what you are
looking for is:

using System;
sing System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

public partial class Initialise_ascx
{
void Page_Load(object sender, System.EventArgs e)
{
MyBasePage.myLiteral.Text = "asdafds";
}
public new MyBasePage Page
{
get { return (MyBasePage)this.Page; }
}
}

Now inside of Page_Load, you'll be calling the Page property to
retrieve a reference to the base page.

HTH,
 
D

Davíð Þórisson

right you are, it was inside the method :(
still it's not working when I move it below the method constructor, this
time the error is (how .Net loves giving me error messages):

"CS0246: The type or namespace name 'MyBasePage' could not be found (are you
missing a using directive or an assembly reference?)"


Scott Allen said:
Hi Davíð:

You have a property defined inside of a method. I think what you are
looking for is:

using System;
sing System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

public partial class Initialise_ascx
{
void Page_Load(object sender, System.EventArgs e)
{
MyBasePage.myLiteral.Text = "asdafds";
}
public new MyBasePage Page
{
get { return (MyBasePage)this.Page; }
}
}

Now inside of Page_Load, you'll be calling the Page property to
retrieve a reference to the base page.

HTH,

--
Scott
http://www.OdeToCode.com/blogs/scott/

Jeffrey could you please take a look here since this is driving me cray...
this is the code I have in Initialise.aspx.cs (the user control), the
error
I get is "CS1513: } expected"

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

public partial class Initialise_ascx
{
void Page_Load(object sender, System.EventArgs e)
{
public new MyBasePage Page
{
get { return (MyBasePage)this.Page; }
}
MyBasePage.myLiteral.Text = "asdafds";
}
}


"Jeffrey Palermo [MCP]" <http://dotnetjunkies.com/weblog/jpalermo> wrote
in
message news:[email protected]...
I make a User Control base class that does the following:
public new MyBasePage Page{
get { return (MyBasePage)this.Page; }
}

I use this all over the place.
of course (MyBasePage)this.Page works too.

Best regards,
Jeffrey Palermo

Please can someone tell me how on earth to create an instance of my top
level (base) Page class so that I can access it's objects from an user
control?? Someone told me

public myParent = (default_aspx) this.Page;

where default_aspx is the class name of the base Page...
 
S

Scott Allen

Check the namespace declaration in the .cs file where you MyBasePage
class lives. You'll have to include it in the user control file in a
using statement or fully qualify the type ('<namespace>.MyBasePage).

HTH,

--
Scott
http://www.OdeToCode.com/blogs/scott/

right you are, it was inside the method :(
still it's not working when I move it below the method constructor, this
time the error is (how .Net loves giving me error messages):

"CS0246: The type or namespace name 'MyBasePage' could not be found (are you
missing a using directive or an assembly reference?)"


Scott Allen said:
Hi Davíð:

You have a property defined inside of a method. I think what you are
looking for is:

using System;
sing System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

public partial class Initialise_ascx
{
void Page_Load(object sender, System.EventArgs e)
{
MyBasePage.myLiteral.Text = "asdafds";
}
public new MyBasePage Page
{
get { return (MyBasePage)this.Page; }
}
}

Now inside of Page_Load, you'll be calling the Page property to
retrieve a reference to the base page.

HTH,

--
Scott
http://www.OdeToCode.com/blogs/scott/

Jeffrey could you please take a look here since this is driving me cray...
this is the code I have in Initialise.aspx.cs (the user control), the
error
I get is "CS1513: } expected"

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

public partial class Initialise_ascx
{
void Page_Load(object sender, System.EventArgs e)
{
public new MyBasePage Page
{
get { return (MyBasePage)this.Page; }
}
MyBasePage.myLiteral.Text = "asdafds";
}
}


"Jeffrey Palermo [MCP]" <http://dotnetjunkies.com/weblog/jpalermo> wrote
in
message I make a User Control base class that does the following:
public new MyBasePage Page{
get { return (MyBasePage)this.Page; }
}

I use this all over the place.
of course (MyBasePage)this.Page works too.

Best regards,
Jeffrey Palermo

Please can someone tell me how on earth to create an instance of my top
level (base) Page class so that I can access it's objects from an user
control?? Someone told me

public myParent = (default_aspx) this.Page;

where default_aspx is the class name of the base Page...
 
D

Davíð Þórisson

Scott I think the namespaces are there...
just to go over things again, I have default.aspx (and
classname="default_aspx") with default.aspx.cs as code behind. Then I have a
user control which links to initialise.aspx with initialise.aspx.cs as
codebehind. I put the public new MyBasePage Page property definition in the
initialise.aspx.cs file within page_load event (see whole file here below),
ok? Well, the default.aspx page is not residing within any namespace so I
cant see what else to have in the using statements? Still the error I get is
"The type or namespace name 'MyBasePage' could not be found "

Why is this so difficult??!!
 
S

Scott Allen

Hi David:

There is a lot going on to learn which can make it difficult. One
suggestion would be to start with some easy console mode applications
and lean the C# language a bit first.

I was assuming you had a class named MyBasePage, apologies if that is
incorrect. If you want to return a reference to the parent page
(Default), than substitute Default for MyBasePage. The problem you'll
find though, is that the user control can only be used on the Default
page, which sort of defeats the whole reusability factor of user
controls, know what I mean?
 
D

Davíð Þórisson

Scott, thx for your patience. Having been programming ASP for 4 years now
and reading c# + .Net for several weeks I thought I could jump on the train.
I have already done some console programming and thought I had grasped the
whole thing, obviously not yet! Maybe what does my situation a bit more
difficult is that I'm doing this in text editor, I refuse to use VS because
that way I don't learn the c#/.Net combination nearly as "intimately"...
So please if you could kindly help me over this treshold I'll be able to do
more experimenting with what I've read so far. I really can't understand why
that default_aspx Page class isn't accessible from my user control, I've
been trying to Google this stuff but everyone seems to do it in VS and so
don't know anything about this at all! I'll send you the codes here below,
plz let me know if you can see where the error lies. Please notice I'm using
..Net framework 2.0 (that explains that partial class)

===default.aspx===
<%@ Page CompileWith="default.aspx.cs" ClassName="default_aspx" %>
<%@ Register TagPrefix="UCs" TagName="UCInitialise" Src="initialise.ascx" %>
<UCs:UCInitialise runat="server" />
<body>
<asp:Literal ID="myLiteral" runat="server"/>
</body>

===default.aspx.cs===
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
public partial class default_aspx
{
void Page_Load(object sender, System.EventArgs e)
{
myLiteral.Text = "asdafds";
}
}

===initialise.aspx===
<%@ Control ClassName="Initialise_ascx" CompileWith="initialise.ascx.cs"
EnableViewState="False" %>
<asp:label ID="myLabel" runat=server/>

===initialise.aspx.cs===
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

public partial class Initialise_ascx
{
public new default_aspx Page
{
get { return (default_aspx)this.Page; }
}

void Page_Load(object sender, System.EventArgs e)
{
default_aspx.myLiteral.Text = "SUCCESS!";
}
}
 
J

Jeffrey Palermo [MCP]

David,
This is another issue altogether. You are using ASP.NET v2.0!! The
aspx page classes in v2.0 are not available to code against from user
controls. Your misunderstanding aren't trivial, and I respectfully suggest
that you step back and do some research in ASP.NET as well as OOP before
expecting to be productive with ASP.NET. Also, if you refuse to use a
powerful IDE, you will waste a lot of time fixing syntax errors at runtime.

Best regards,
Jeffrey Palermo
 
D

Davíð Þórisson

ok I'll have to admit I'm not the best at c# but I'd thought that years of
programming (even assembly for whole 3 years on the good'n' old Amiga -
Motorola 68000!!) would give me a handicap. I've seen people asking about
many ridicilous things here ("hi, I've just started .Net and what does class
mean?...") so I would have thought I could experiment around to get the
"feeling for c# and .Net". I don't feel like reading a whole book for a week
just because of some entry points I'm not understanding at the moment.
It's crucial for me to be able to continue experimenting to get hold of this
user control and write to the good damn base page!! As for IDE or not IDE -
maybe, maybe not - at the moment I feel this is the right thing to do to if
I'm ever going to learn c#/.Net more than just inserting objects and doing
simple buisiness logic.

Thanks though for your time though.
 

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,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top