Custom Page class sets all querystring values as properties, but whatabout UserControls?

D

DotNetNewbie

Hi,

I have many querystring values that I pass around in my web
application, so I created a Custom Page class that simply inherits
from System.Web.UI.Page.

I then check for all the querystrings in the URL and initialize them,
and these are all public properties so I can reference them from
within ALL my .aspx pages since they inherit from my custom page
class.

The Problem? I also have UserControls in my page class, and I need to
access these same querystring values etc. from within those!

I *could* simply create a custom user control class and then do the
same thing, but it seems like redundant work.


What other solutions are there?

Maybe create a class that I store in Context.Items and setup all the
querystring values there during BeginRequest in a HttpModule, and then
just reference this from within my Page and UserControl class?
 
M

Mufaka

If you don't want to re-design, you can use the Page property of your
control to access your properties (with a cast to your Custom Page class).
 
D

DotNetNewbie

If you don't want to re-design, you can use the Page property of your
control to access your properties (with a cast to your Custom Page class).

Mufaka,

Here is my page setup:

I have a .aspx page that inherits from a Master Page.

The Master page has the 1 content placeholder (which is my .aspx page)
and a Header and Footer user control.

I need to access the .aspx custom page class from within my Header
control.

What would the casting look like from within my Header control?
 
M

Mufaka

DotNetNewbie said:
Mufaka,

Here is my page setup:

I have a .aspx page that inherits from a Master Page.

The Master page has the 1 content placeholder (which is my .aspx page)
and a Header and Footer user control.

I need to access the .aspx custom page class from within my Header
control.

What would the casting look like from within my Header control?

The Page property gives you access to the Page hosting the control,
which still works even if your control is in a MasterPage.

For a sanity check, I created a test project with the following:

1. PageBase which derives from Page. It has one property, TestProperty.
2. WebUserControl
3. MasterPage.master that uses WebUserControl as a header.
4. Default.aspx which uses MasterPage as its master and derives from
PageBase

In WebUserConrol I can access the PageBase TestProperty property by doing:

string testProperty = ((PageBase)Page).TestProperty;
 
D

DotNetNewbie

The Page property gives you access to the Page hosting the control,
which still works even if your control is in a MasterPage.

For a sanity check, I created a test project with the following:

1. PageBase which derives from Page. It has one property, TestProperty.
2. WebUserControl
3. MasterPage.master that uses WebUserControl as a header.
4. Default.aspx which uses MasterPage as its master and derives from
PageBase

In WebUserConrol I can access the PageBase TestProperty property by doing:

string testProperty = ((PageBase)Page).TestProperty;

Yes thanks, I did the same think today.

I simply casted the Page to my custom page class and it worked great.

So what I did then was, I just added a read only property in my custom
UserControl that does the cast, so now I can just do:
CurrentCustomPageClass.MyProperty instead of having to cast each time.
 

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

Top