构建通用搜索结果 Web 控件

发布于 2024-09-02 15:54:15 字数 506 浏览 5 评论 0原文

在我目前正在从事的一个项目中,我们偶然发现需要多种搜索结果呈现控件。搜索结果相似,但不完全相同。例如,在“办公室搜索”结果中我们可能希望显示办公室名称和位置,而在“文档搜索”结果中可能包含文档名称、作者和发布日期。这些字段应该是可排序的。

我当前的策略是采用工厂模式并执行以下操作:

ISearchResult officeResults = SearchResultFactory.CreateOfficeSearchResults(data);
ISearchResult documentResults = SearchResultFactory.CreateDocumentSearchResults(data);

问题是:我不知道如何实现标记代码。我应该只

Controls.Add(officeResults);

在包含页面中执行吗?或者是否有一些 ASPX 技巧来创建通用 Web 控件?或者也许我想得太多了,应该创建五个类? ;)

In a project I'm currently working for we've stumbled upon the need for several kinds of search results presentation controls. The search result are similar, but not identical. For example, in the "office search" result we might want to present the office name and location, while in the "document search" could contain document name, author and publishing date. These fields should be sortable.

My current strategy is to employ the Factory pattern and do something like this:

ISearchResult officeResults = SearchResultFactory.CreateOfficeSearchResults(data);
ISearchResult documentResults = SearchResultFactory.CreateDocumentSearchResults(data);

The problem is: I don't know how to implement the markup code. Should I just do

Controls.Add(officeResults);

in the containing page? Or is there some ASPX trickery to create generic web controls? Or maybe I'm overthinking this and just should create five classes? ;)

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

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

发布评论

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

评论(1

烟火散人牵绊 2024-09-09 15:54:16
Controls.Add(officeResults);

很好。您只需要确保它是一个 WebControl(即:它继承自 WebControl)。实际上,“控件”是所期望的,但就我个人而言,如果我正在编写“Web 控件”,我将使用/继承 WebControl。

我发现将其添加“两次”很有用:

Controls.Add(officeResults);   // adds the control to the page (or parent controls) object collection.
myPanel.Controls.Add(officeResults);  // adds the control to where you want it to appear.

一次添加到父控件/页面(在可执行结构意义上),然后一次添加到我希望它出现的容器(在视觉/物理层次结构意义上)。我注意到,当我这样做时,回发往往会按预期工作——但这​​可能是因为我当时做错了其他事情。我确信如果我是这样,其他人会纠正我。

我还建议使用 INAmingContainer接口(您不需要添加任何代码,它在运行时自行完成)。

不确定我会使用工厂 - 我并不是说这是错误的,但通常如果我向页面添加控件(即使是动态的),我就会知道我想要哪个。文本框、按钮和表格都是 WebControl,您不会通过工厂构造/添加它们(至少我从未见过,或者觉得有必要这样做)。如果您要使用工厂,您是否会拥有一个工厂方法来根据显式参数或可能根据传入的数据来确定要返回哪个 WebControl?

只是一个想法,希望有帮助。

顺便说一句,我已经完成了一些 WebControl 开发 - 我并不是说我是世界专家,但您可能会发现一些有用的想法 此处:(查看“Morphfolia.Core”sln;在 Morphfolia.PublishingSystem 项目、WebControls 文件夹和 Morphfolia.WebControls 项目中)。

Controls.Add(officeResults);

Is fine. You just need to ensure it's a WebControl (i.e.: it inherits from WebControl). Actually a "Control" is what's expected, but personally if I'm writing a "web control" I'll use/inherit from WebControl.

I've found it's useful adding it "twice":

Controls.Add(officeResults);   // adds the control to the page (or parent controls) object collection.
myPanel.Controls.Add(officeResults);  // adds the control to where you want it to appear.

Once to the parent control / page (in the executable structural sense), and then once to the container I want it to appear in (in the visual / physical hierarchal sense). I noticed that postbacks tended to work as expected when I did this - but it might be because I was doing something else wrong at the time. I'm sure others will correct me if I am.

I'd also advise using the INamingContainer interface (you don't need to add any code, it does it all itself at runtime).

Not sure I'd use a factory - I'm not saying it's wrong, but generally if I'm adding a control to a page (even dynamically) I'll know which one I want. TextBoxes, Buttons and Tables are WebControls and you wouldn't construct / add them via a Factory (at least not that I've ever seen it, or felt compelled to do so). If you were to use a factory wouldn't you have one factory method which worked out which WebControl to return based on an explicit parameter, or maybe based on the data passed in?

Just a thought, hope it helps.

BTW, I've done a bit of WebControl development - I'm not saying I'm a world expert but you might find some useful ideas here: (look in the "Morphfolia.Core" sln; in the Morphfolia.PublishingSystem project, WebControls folder and the Morphfolia.WebControls project).

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