Extending System.Web.UI.Page/MasterPage with custom properties

G

Guest

I am trying to create a BasePage for use in a large asp.net application, that
will centrally provide certain extra properties and logic to the application.
(The web project makes use Master Pages as well.)

I want to make a property named TitleResourceKey, for example, that contains
the resource key used to translate the page title and, using reflection and
the CustomLocalisationKey attribute that points to the property this key
translates, automatically translate the Title value.

My problem (simple as it seems) is that I don't seem to be able to add
properties to the BasePage class that show up in the pages that inherit from
it. Should I be adding this property to the BaseMasterPage class (extends
MasterPage) or the BasePage class (extends Page)? (Either way it doesn't
show up for me in the property explorer when I am editing the page in
VS2005.) Any ideas why this isn't showing up? I'm sure it's something
completely stupid on my part, since it seems to bloody obvious.


/// <summary>
/// Adds common functionality to all Pages.
/// </summary>
public class BasePage : Page
{
#region Private Fields

private string m_titleResourceKey;

#endregion

#region Public Properties

[CustomLocalisationKey("Title")]
[Bindable(true)]
[Description("The resource key used when translating the page
Title.")]
[Category("Appearance")]
public string TitleResourceKey
{
get
{
return m_titleResourceKey;
}
set
{
m_titleResourceKey = value;
}
}

#endregion
}

I'm, of course, inheriting from this class on each page: public partial
class SupportHome : BasePage

Any ideas?
 
G

Guest

I'm not great a C#, but when I wrote a MVC class I had to do that to expose
page methods to other classes and it worked just fine by simply declaring the
methods either protected or public depending on where I wanted to access them
from.

In VB this looked like:

Partial Class MyPage
Inherits BasePage
End Class

Public Class BasePage
Inherits System.Web.UI.Page

Public Property MyProp() As Object
...
End Property
Public Sub MySub()
...
End Sub
End Class

Everything worked, intellisense, access by classes off the page, etc.
 
G

Guest

Larry:

Thanks for the reply.

Normally I would expect this to work, as it does for you (my code is more or
less the equivalent of what you wrote), but there is something apparently
missing in my case. My suspicion is that it might be the 'partial' class bit
in 2005 ... perhaps in the other half of the partial class (the part VS2005
generates for you) it is only declaring a normal page (not a specialized
BasePage, etc.), and so it gets confused and uses the normal page not the
specialized one. Unfortunately, there is no 'nice' way to edit the other
half of the page's code if this is indeed the problem.

Christophe.
 
G

Guest

Here's a snippet I just ran in 2005, works great. The only catch is
BaseClass needs to be in App_Code if you're writing a Web Site (versus a Web
Application Project).

Imports Microsoft.VisualBasic

Public Class BasePage
Inherits System.Web.UI.Page
Public Function Test() As String
Return "Hi"
End Function
End Class

Partial Class _Default
Inherits BasePage

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
Response.Write(Test())
End Sub
End Class
 
G

Guest

Due to the system architecture, I am obliged to store the BasePage and
BaseMasterPage class in an external assembly, and not in the App_Code folder,
since it is referenced by other projects as well, and it is easier to
maintain for us this way. Perhaps this is the problem (though I don't see
why it should be). Any ideas how I can avoid having to put this in App_Code?
 
G

Guest

If you need page specific properties to be visible to other classes (outside
of the page), here's a sample. In the other classes just refer to the page
as a BaseClass.

Imports Microsoft.VisualBasic

Public MustInherit Class BasePage
Inherits System.Web.UI.Page
Public Function Test() As String
Return "Hi"
End Function
Public MustOverride Property Test2() As String
End Class

Partial Class _Default
Inherits BasePage

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
Response.Write(Test())
End Sub
Public Overrides Property Test2() As String
Get
Return Me.Title
End Get
Set(ByVal value As String)
Me.Title = value
End Set
End Property
End Class
 
G

Guest

Well that works too. The program I'm using acutally does that as well. I
have a class library that gets linked with each application. I'm assuming
the class library is in the bin folder then and you are using the namespace
from the library when inheriting?
 
G

Guest

Larry:

Thanks again for your help/input.

Looking at this on the code side, I see I do in fact have the
..TitleResourceKey property available (which I expected since the beginning)
.... it simply doesn't appear in the Property Explorer when I look at the
visual page layout, which the other developer's on our team will use to enter
the resource keys for the page Title, etc. Sorry for misleading you
initially ... I only noticed this now, and so was looking in the wrong
direction for the problem.

I have tried setting the [Browsable(true)] attribute to the TitleResourceKey
property (though the property should show up by default ... [Browsable(xxx)]
is only useful for hiding properties from the PropertyExplorer, at least as I
understand it. Anything not set to [Browsable(false)] should show up.) ...
it doesn't show up even with browsable set to true.

This is becoming more of a component design question than general
inheritance, but if you have any thoughts I'd appreciate it. Thanks again
for all your time on this.
 
G

Guest

Sorry, I've never tried to extend web page and get settable properties at
design time. I know it will work with User controls and composite server
controls, but never worked with extending a page. Hopefully someone else
will have some insight.
 
G

Guest

Larry:

Thanks for your time on this anyway ... at least I see the problem isn't
where I was initially looking, and know what to look at now. I will move
this to the control creation group instead.

Best regards.
 
S

Steven Cheng[MSFT]

Hi Larry,

Actually this is the limitation of the VS IDE design-time services (also
those design-time specific attributes), they're provided to serve webserver
control only. And Page or Usercontrol(asax) are template based controls
which has different design-time implementation from webserver
controls(somewhat hardcoded) that can not be customized through those
design-time attributes.

Regards,

Steven Cheng
Microsoft Online Community Support


==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.



Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

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,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top