Is it possible to create a main event handler for many pages using a user control or the main page?

I need to create a general action menu for my pages. On all pages, some basic functions will be implemented, that is, adding new rows to any table, editing them, calling filters. To perform this function, many of these pages will require only basic logic. But some pages will implement their own logic for the options in the menu.

I want this to happen using the events in the menu. Therefore, I need not only events, but also a basic event handler for all pages. And this handler must be built in such a way that it can be overestimated. The problem is that I do not know how to create a handler for future use of the menu. Sounds somehow utopian. Is it really possible to create such an architecture?

I thought of two ways to do this: the main page or user control. But I don’t even know if this is possible. So what do you think?

UPD: Support both answers about the main pages: you guys probably know what you're talking about. Thank you Sorry that the correct answer can only be selected once.

+3
source share
3 answers

You really do not need to create objects on the pages without the need, as this approach will certainly be.

With this in mind, you can make a base page on which the corresponding pages are inherited, so you have access to the methods that you need.

  • Create a class of type System.Web.UI.Page. (Name it MyBasePageClass)
  • The implementation of the general methods required in this class
  • Inherit this class on the pages to which methods need to be exposed. For example, change the page class definition of Default.aspx as follows: public partial class Default: MyBasePageClass

Methods created in MyBasePageClass must be publicly available so that they can be seen by default .aspx.cs.

+1
source

, . , .

:

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

virtual .

:

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

, :

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

, " " .

, , , - , , , .

, .

0

Source: https://habr.com/ru/post/1759513/


All Articles