views:

30

answers:

3

I need to create general actions menu for my pages. All of pages will implement some basical functionality, i.e adding new rows to some table, editing them, calling filters. Many of this pages will need only basical logic to run this functionality. But some of the pages will implement their own logic for options in menu.

I want to make this happen by using events in menu. So I'll need not the only events, but some basic event handler for all pages. And this handler schould be constructed in a way it could be overriden. The problem is I don't know how to create handler for all future uses of menu. Sounds kind of utopic in some way. Is it real to create architecture like this?

I thought about two ways of doing this: Master page or user control. But I don't really know if it's even possible. So what do you think?

UPD: Upvoted both answers about basic pages: you guys are surely know what you're talking about. Thanks. Sorry that right answer can be selected only once.

A: 

I think that to "create handler for all future uses" sounds scary.

What I would do was to create the pages individually, and only when I identify some piece of logic that is equal or similar I would refactor that out to a separate class that several forms could use.

After all, code needs to be used before it can be reused.

Per-Frode Pedersen
That is right, of course, but I'm just rebuilding Winforms project, written in Delphi, to Asp.Net. And in old version there is this architecture, but of course I can't just copy Delphi algorithms written with Deplhi's special features and hacks, all the more so for the web.
Chaotic_one
I believe you will have a hard time moving a project from WinForms to WebForms if you try to recreate the same application flow and logic. The semantics of the two technologies are just too different.
Per-Frode Pedersen
+1  A: 

You really shouldn't instantiate objects on pages unnecessarily, as this approach would most certainly do.

Keeping that in mind, you can make a base page that relevant pages inherit from, so you have access to the methods you need.

  1. Create a class of type System.Web.UI.Page. (Let's name it MyBasePageClass)
  2. Implement common methods needed in this class
  3. Inherit this class on the pages you need to expose the methods to. For example, change your definition of the Default.aspx page class to this: public partial class Default : MyBasePageClass

The methods created in MyBasePageClass must be public to be seen by Default.aspx.cs.

TheGeekYouNeed
Yes - creating a base class is a way to go as explained above but with two corrections: first event handler methods need not be public, they can be protected. Secondly you want to make them virtual so that some page can override the default event handling behavior and supply its own.
VinayC
yes, sorry, protected because you're inheriting from the class.
TheGeekYouNeed
+1  A: 

In many of my applications, I use a common base class if I have functionality to share across many pages. It's not uncommon practice, really.

The implementation might look something like this:

public class MyApplicationPage : System.Web.UI.Page
{
    public virtual void RaiseMyCustomEvent(EventArgs e)
    {
    }
}

The virtual keyword makes the method overridable.

In my individual code behinds, the page inheritance might look like this:

public partial class MyPage : MyApplicationPage
{
    public override void RaiseMyCustomEvent(EventArgs e)
    {
        // ...
    }
}

Finally, in my master page, I might find code like this in a method:

if (Page is MyApplicationPage)
{
    ((MyApplicationPage)Page).RaiseMyCustomEvent(EventArgs.Empty);
}
kbrimington