views:

252

answers:

2

Hi

Looking for advice on the net but not getting very far, so I thought I would ask for some advice. I've seen it done, so know what I want to do, but looking I can work out how it was done

What I want to do, is allow users to modify the layout of an ASPX file, so that they can use it as a letter template, moving address lines, format of the page e.t.c. Now in the example that I was shown, the page inheritied a standard class, and this class had a set of generic functions, such as foreName / sureName / addressLine1 e.t.c, then when designing the ASPX file, if the user wanted addressLine1 to be displayed they would add the tag content = "addressLine1"

Any advice on how to achieve the above would be greatly appreciated, think I'm missing lots of simple stuff

+2  A: 

It might be possible to use a baseclass with the properties they need to define as virtual members. Just create a class (no page, just a class) in you project with the virtual members you want your users to use.

public class BasePage : Page {
  public virtual string Content;
}

In the pages you (or your users) create you need to inherit from that class. Your page would look like:

public class MyPage : BasePage {
  public override string Content = "My Content";

  //Other logic can go here
}

(since the BasePage inherits from the Page class the page will function just like any other Aspx page).

Please note that the base page class needs to be defined in the App_Code folder to be available for other pages/items within you project.

This can also be done for functions.

More reading about virtual members and functions can be done at MSDN

Gertjan
App_Code would only be required for a web site, not for web application projects.
John Saunders
That is true. My experience is that web site projects are used most of the time(use them myself all the time) and many people try to cross reference pages (which will fail). But thanks for the clarification! As far as I know web applications were introduced in 2.0 to ensure backward compatibility with 1.0 and 1.1. The web applications behave like they did in the older versions. Website projects are more recent (and therefore I advise using websites most of the time).
Gertjan
OK I see that, however, what I don't see how is how placing content = "addressLine1" in the markup, will cause the addressLine1 function to be called .... am I making sense, do I need to change my question ?
spacemonkeys
When you write code in the BasePage that uses the Content property (for example a Response.Write(Content) in the Page_Load) .NET will use the value of the override property. So you can write code in your BasePage that uses virtual properties while the users override them in their code. When opening the page created by the user (which inherits your BasePage and overrides your virtuals) your code will automatically use the assigned values (as far as I know virtuals must be overriden in inheriting classes). For more details you could do some reading about Object Oriention.
Gertjan
Hi, Yeap thats how I would usually do it ... but I'm trying to work out how somebody did the code that I saw earlier today. This didn't use the BasePage, all 50 pages inherited from one class. There was no ising of the response.write. But when they entered a html tag along the lines of <label id="Label1" Content = "addressLine1"/> the output of function addressLine1 in the inherited (in the page header) class was placed into the label. Just trying to work out how they did it. Might not be the best way, but I want to know how they did it rather than whats the best way .. if that makes sense
spacemonkeys
Ah right, wasn't completely clear that you understood the concept of virtuals and inheritance (and I interpreted the story a bit different). What you say looks a bit like a MasterPage like construction. That is also based on the ID of a tag. Maybe that is what they used? Masterpages work with <Content> tags.
Gertjan
Ahhhhhh even clearer now, cheers
spacemonkeys
A: 

Handy little article at codebetter took me in the right direction

spacemonkeys