抽象 ASP.NET 用户控件,无需重新创建标记

发布于 2025-01-06 04:49:54 字数 1704 浏览 6 评论 0原文

我在 ASP.NET 应用程序中多次复制以下控件布局,并希望将功能封装在用户控件(或服务器或复合控件,如果需要)中。

伪标记列表

框1(可用对象)> (添加按钮) LISTBOX2(选定的对象)

基本上是 2 个列表框,一个列表框中有可用对象,另一个列表框中有选定对象(这些对象的类型将根据使用而变化,例如可用和选定产品的集合)

示例用法:添加通过添加按钮将列表框项目从列表框 1 添加到列表框 2(还有用于添加所有按钮以及从选定到可用的其他方式的按钮 - 为清楚起见,未显示这些按钮),并且该项目将从列表框 1 中删除并放置在列表框 2 中。相当简单 - 我将该控件称为 DualListbox。

我想这样做:

DualListbox.ascx 包含这样的标记:

<asp:ListBox id="AvailableListBox"  runat="server"/> 
<asp:Button  id="AddItemtoSelected" runat="server"/> 
<asp:ListBox id="SelectedListBox"   runat="server"/>

并且 DualListbox.ascx.cs 包含一系列抽象和非抽象函数、属性等。

例如

--We don't actually use CollectionBase but a class derived from it
protected abstract CollectionBase AvailableItems {get;set;}
protected abstract CollectionBase SelectedItems  {get;set;}


protected abstract void SaveContentsofAvailableListBox ();

private void FillAvailableListBox ()
{
   .....
   AvailableListBox.DataSource = AvailableItems;
   AvailableListBox.DataBind()
}
private void MoveItemfromAvailabletoSelectedListBox()
{
   ..Some code that takes item from available and puts it in selected.
}

然后我会继承 DualListbox.ascx 例如

ConcreteDualListBox : DualListBox
{
    public override WidgetCollection AvailableItems {get;set;}
    public override void SaveContentsofAvailableListBox(){}; etc.etc.
}

问题是你无法继承标记,因此 DualListBox 中的标记对于具体类不可用。我可以在具体类中定义标记,但要在基类中使用 FillAvailableListBox 等函数,我必须将 AvailableListBox 控件(以及所有其他控件)从具体类传递到基类中。 此外,对于每个具体类,都必须重复标记(我猜我可以将公共标记嵌入另一个 .ascx 文件中的每个具体类中)。

我将不胜感激任何有关定义此类控件的正确方法的建议。

对伪代码表示歉意 - 我目前正在这样做作为概念证明。

谢谢, 富有的。

I have the following control layout replicated several times in an ASP.NET application and would like to encapsulate the functionality in a user control (or server or composite control if necessary).

PSEUDO MARKUP

LISTBOX1 (Available Objects) > (Add Button) LISTBOX2 (Selected Objects)

Basically 2 listboxes with available objects in one listbox and selected objects in the other listbox (the type of these objects will change depending on use e.g. a collection of available and selected products)

Example usage: Add a listbox item from listbox1 to listbox2 via the add button (there are also buttons for adding all and going the other way from selected to available - these aren't shown for clarity) and the item is removed from listbox1 and placed in listbox2. Fairly straightforward - I'll call the control DualListbox.

I would like to do this:

DualListbox.ascx contains markup like this:

<asp:ListBox id="AvailableListBox"  runat="server"/> 
<asp:Button  id="AddItemtoSelected" runat="server"/> 
<asp:ListBox id="SelectedListBox"   runat="server"/>

and DualListBox.ascx.cs contains a series of abstract and non abstract functions, properties etc.

e.g

--We don't actually use CollectionBase but a class derived from it
protected abstract CollectionBase AvailableItems {get;set;}
protected abstract CollectionBase SelectedItems  {get;set;}


protected abstract void SaveContentsofAvailableListBox ();

private void FillAvailableListBox ()
{
   .....
   AvailableListBox.DataSource = AvailableItems;
   AvailableListBox.DataBind()
}
private void MoveItemfromAvailabletoSelectedListBox()
{
   ..Some code that takes item from available and puts it in selected.
}

Then I'd inherit from DualListbox.ascx e.g.

ConcreteDualListBox : DualListBox
{
    public override WidgetCollection AvailableItems {get;set;}
    public override void SaveContentsofAvailableListBox(){}; etc.etc.
}

The problem is that you can't inherit markup and so the markup in DualListBox is unavailable to the concrete class. I can define the markup in the concrete class but then to use functions like FillAvailableListBox in the base class I would have to pass the AvailableListBox control (and all the other controls) into the base class from the concrete class.
Also for every concrete class the markup would have to be repeated (I could embed the common markup in each concrete class from another .ascx file I guess).

I would appreciate any suggestions on the correct way to go about defining such a control.

Apologies for the pseudocode - I'm doing this as a proof of concept at the moment.

Thanks,
Rich.

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

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

发布评论

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

评论(3

万劫不复 2025-01-13 04:49:54

我认为您不能直接使用用户控件来执行此操作。您可以做的是将其分解为 2 个控件,一个用户控件和一个常规服务器控件。将标记保留在用户控件中,并让服务器控件重写 OnInit 方法并加载用户控件并将其添加为子控件。所有具有抽象方法的逻辑都可以嵌入到服务器控件中,并且可以继承。

I don't think you can do this directly with a user control. What you can do is break this down into 2 controls, a user control and a regular server control. Keep the markup in the user control and have the server control override the OnInit method and load the user control and add it as a child. All the logic with abstract methods can be embedded in the server control which can be inherited.

那请放手 2025-01-13 04:49:54

为了避免布局问题,您可以创建模板化 ASP.NET 用户控件。这是来自 MSDN 的示例

To avoid layout issues you can create Templated ASP.NET User control. Here is an example from MSDN

如歌彻婉言 2025-01-13 04:49:54

我建议创建一个函数来在基类中生成标记,子类可以调用该函数,向它们传递它们想要渲染的数据类型/模板(这甚至可以是 ascx 文件的路径)/渲染的函数( child)模板中的数据(同样在子类中)。

I would suggest creating a function to generate the markup in the base class that child classes can call passing them the type of data they want to render/a template (this could even be a path to an ascx file)/a function that renders (child) data in the template (again in the child class).

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