如何为 Page 和 UserControl 创建基类?

发布于 2024-12-11 08:15:41 字数 779 浏览 0 评论 0原文

Page 和 UserControl 的基类:

public class MyWebPage : System.Web.UI.Page { }

public class MyUserControl : System.Web.UI.UserControl { }

它们中的任何一个都可能使用的帮助程序:

void SetSessionValue<T>(string key, T value) { Session[key] = value; }

如何实现类似以下内容?

public class WebObject // can't inherit from both Page and UserControl { 
   protected void SetSessionValue<T>(string key, T value) { 
      Session[key] = value; 
   }
}  

public class MyWebPage : WebObject { }

public class MyUserControl : WebObject { }

更新:我兴奋了一秒钟,希望我能用这种方式解决它,但遗憾的是它无法编译。

public class WebObject<T> : T
{
}
public class MyWebPage : WebObject<System.Web.UI.Page>
{
}

Base classes for Page and UserControl:

public class MyWebPage : System.Web.UI.Page { }

public class MyUserControl : System.Web.UI.UserControl { }

Helper that either of them might use:

void SetSessionValue<T>(string key, T value) { Session[key] = value; }

How can I achieve something like the following?

public class WebObject // can't inherit from both Page and UserControl { 
   protected void SetSessionValue<T>(string key, T value) { 
      Session[key] = value; 
   }
}  

public class MyWebPage : WebObject { }

public class MyUserControl : WebObject { }

Update: I got excited for a second hoping I could solve it this way, but alas it doesn't compile.

public class WebObject<T> : T
{
}
public class MyWebPage : WebObject<System.Web.UI.Page>
{
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

时光暖心i 2024-12-18 08:15:41

你不能。反正不容易。我建议只为页面和用户控件创建一个基类,并在两者中复制通用代码。由于用户控件包含在页面中,因此您还可以将基本用户控件类中的方法委托给基本页面类,只需将 Page 属性强制转换为您自己的类型:

// Code in the MyUserControlBase class
public int SomeCommonMethod() {
    return ((MyBasePageType)this.Page).SomeCommonMethod();
}

您还可以通过以下方式让您的生活变得悲惨:创建由两个基类实现的接口,并使用 DI 拦截方法和属性访问器调用,然后将其路由到实际提供实现的某种公共代理类。我可能不会去那里:)

You can't. Not easily anyway. I'd recommend just creating a base class for pages and user controls, and duplicating the common code in both. Since user controls are contained in pages, you can also delegate methods in the base user control class to the base page class simply by casting the Page property to your own type:

// Code in the MyUserControlBase class
public int SomeCommonMethod() {
    return ((MyBasePageType)this.Page).SomeCommonMethod();
}

You can also make your life miserable by creating an interface implemented by both base classes and using DI to intercept method and property accessor calls, which would then be routed to some kind of common surrogate class that actually provides the implementation. I probably wouldn't go there :)

半步萧音过轻尘 2024-12-18 08:15:41

IIRC Page 和 UserControl 均继承自 TemplateControl,这样您就可以继承它。

IIRC both Page and UserControl inherit from TemplateControl so you might be able to inherit from that.

做个ˇ局外人 2024-12-18 08:15:41

避免重复的一种方法是使用一个帮助器类,通过静态属性实例化并可以从 UI 中的任何位置(Page、UserControl 或 UI 层中的任何其他类)访问。

类似于:

public class ApplicationContext
{
    // Private constructor to prevent instantiation except through Current property.
    private ApplicationContext() {}

    public static ApplicationContext Current
    {
        get
        {
            ApplicationContext current = 
                 HttpContext.Current.Items["AppContext"] as ApplicationContext;
            if (current = null)
            {
                current = new ApplicationContext();
                HttpContext.Current.Items["AppContext"] = current;
            }
            return current;
        }
    }

    public void SetSessionValue<T>(string key, T value) 
    { 
        HttpContext.Current.Session[key] = value; 
    }
    ... etc  ... 
}  

单个 ApplicationContext 实例将在当前请求的生命周期内有效,并且您可以在 UI 层代码中的任何位置使用 ApplicationContext.Current.SetSessionValue 和其他常见成员。

我经常比将 SetSessionValue 这样的通用方法放在这样的帮助器类中更进一步,并且可能在其中具有应用程序特定的属性,例如

public class ApplicationContext
{
    ... as above ...

    public ShoppingBasket ShoppingBasket
    {
        ShoppingBasket shoppingBasket = 
           HttpContext.Current.Session["Basket"] as ShoppingBasket;
        if (shoppingBasket == null)
        {
            shoppingBasket = ... e.g. retrieve from database
            HttpContext.Current.Session["Basket"] = shoppingBasket;
        }
        return shoppingBasket;
    }
}

通过这种方式,您可以访问当前的 ShoppingBasket code> 实例在 UI 中的任何位置,而不知道或关心它是否缓存在 Session 中 - 这是只有您的 ApplicationContext 类知道的实现细节。

One way to avoid duplication is to have a helper class, instantiated through a static property and accessible from anywhere in your UI (Page, UserControl or any other classes in the UI tier).

Something like:

public class ApplicationContext
{
    // Private constructor to prevent instantiation except through Current property.
    private ApplicationContext() {}

    public static ApplicationContext Current
    {
        get
        {
            ApplicationContext current = 
                 HttpContext.Current.Items["AppContext"] as ApplicationContext;
            if (current = null)
            {
                current = new ApplicationContext();
                HttpContext.Current.Items["AppContext"] = current;
            }
            return current;
        }
    }

    public void SetSessionValue<T>(string key, T value) 
    { 
        HttpContext.Current.Session[key] = value; 
    }
    ... etc  ... 
}  

A single ApplicationContext instance will live for the lifetime of the current request, and you can use ApplicationContext.Current.SetSessionValue and other common members from anywhere in your UI tier code.

I often go further than putting general-purpose methods like your SetSessionValue in such a helper class, and might have application specific properties in there, e.g.

public class ApplicationContext
{
    ... as above ...

    public ShoppingBasket ShoppingBasket
    {
        ShoppingBasket shoppingBasket = 
           HttpContext.Current.Session["Basket"] as ShoppingBasket;
        if (shoppingBasket == null)
        {
            shoppingBasket = ... e.g. retrieve from database
            HttpContext.Current.Session["Basket"] = shoppingBasket;
        }
        return shoppingBasket;
    }
}

In this way you can access the current ShoppingBasket instance anywhere in your UI without knowing or caring that it's cached in Session - that's an implementation detail known only to your ApplicationContext class.

云雾 2024-12-18 08:15:41
  • 1) 创建一个静态类,其中包含您需要的所有变量、函数
  • 2) 创建您自己的 UserControl 和 Page 类,并创建这些类
    在 Pre_Init 和 Load 覆盖中调用静态函数
  • 3) 让每个页面都继承您的基类。

这是最简单的方法。

  • 1) Make a static class that has all the variables, functions you need
  • 2) Make your own UserControl and Page class, and make these classes
    call your static functions in the Pre_Init and Load overrides
  • 3) Make every page inherit your base classes.

That's the easiest way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文