Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / February 2008

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
DotNetNewbie - 28 Feb 2008 23:13 GMT
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?
Peter Bromberg [C# MVP] - 29 Feb 2008 02:01 GMT
Possibly what you really need is not a custom Page class, but a custom
Querystring class. This could then be used anywhere. Here is an example:
http://www.eggheadcafe.com/articles/20060427.asp

-- Peter
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short Urls & more: http://ittyurl.net

> Hi,
>
[quoted text clipped - 18 lines]
> querystring values there during BeginRequest in a HttpModule, and then
> just reference this from within my Page and UserControl class?
Mufaka - 29 Feb 2008 03:08 GMT
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).

> Hi,
>
[quoted text clipped - 18 lines]
> querystring values there during BeginRequest in a HttpModule, and then
> just reference this from within my Page and UserControl class?
DotNetNewbie - 29 Feb 2008 12:35 GMT
> 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).
[quoted text clipped - 21 lines]
> > querystring values there during BeginRequest in a HttpModule, and then
> > just reference this from within my Page and UserControl 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?
Mufaka - 29 Feb 2008 16:47 GMT
>> 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).
[quoted text clipped - 29 lines]
>
> 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;
DotNetNewbie - 29 Feb 2008 20:31 GMT
> >> 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).
[quoted text clipped - 44 lines]
>
> 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.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.