简体   繁体   English

如何将用户控件分配给后面的扩展代码?

[英]How to assign a user control to an extended code behind?

I have a series of ASP.NET pages that are pretty much the same, except for some abstract properties in the code behind. 除了后面代码中的某些抽象属性外,我有一系列的ASP.NET页面几乎相同。

Each page (/default.aspx) includes a user control: 每个页面(/default.aspx)都包含一个用户控件:

<UI:SingleRaterControl runat="server" />

Previously, the various code behinds were copy and pastes of essentially the same thing. 以前,各种代码背后都是本质上相同的东西的复制和粘贴。 In each version of /default.aspx.cs there was a protected property that would hold the user control. 在/default.aspx.cs的每个版本中,都有一个受保护的属性可以保存用户控件。

public class _default: SingleRaterBase
{
    protected SingleRaterControl singleRaterControl;
    ...
}

In an effort to reduce the amount of code I have to maintain, I created at abstract class called EProfileDefault that would hold all the common code of the _default classes (each _default class is in a different namespace). 为了减少必须维护的代码量,我在名为EProfileDefault的抽象类中创建了该类,该抽象类将保存_default类的所有通用代码(每个_default类位于不同的命名空间中)。

So now I have 所以现在我有

public abstract class EProfileDefault: SingleRaterBase
{
    protected SingleRaterControl singleRaterControl;
    ...
}

and each _default class has 并且每个_default类都有

public class _default : EProfileDefault
{
    ...
}

However, the SingleRaterControl is no longer getting automatically assigned to the code behind. 但是,不再将SingleRaterControl自动分配给后面的代码。

Is this because ASP.NET magic will only automatically assign the user control to the immediate instance of the code behind class, as long as it has the same name (as suggested by this article )? 这是因为ASP.NET magic只会将用户控件自动分配给类后面代码的直接实例,只要它具有相同的名称(如本文所建议)?

If so, is it possible to manually find and assign the user control? 如果是这样,是否可以手动查找并分配用户控件?

如@aquinas所建议,给控件一个ID,然后该控件将被分配给与该控件的ID同名的受保护属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM