以简单的方式继承 Web 服务器控件?
我有一个中继器用作一种分页标签云。 为此,我向页面的 ViewState 添加了简单的属性,例如页面、行计数等...
我觉得它不属于那里,但我在服务器控制、调试、dll 和部署方面有不好的经验。
我是否可以继承 Repeater 类,添加一些 ControlState/ViewState 属性,并能够像直接从工具箱拖动的 Repeater 一样使用它?
在这里,有以下简单的类:
public class TagCloud : Repeater
{
public int selectedIndex;
public TagCloud()
{
selectedIndex = -1;
//
// TODO: Add constructor logic here
//
}
public int SelectedIndex
{
get { return selectedIndex; }
set { selectedIndex = value; }
}
}
在不创建新的 WebControlLibrary 项目的情况下,这个 cs 文件能否位于 App_Code 文件夹中并按预期工作?
谢谢。
I'm having a Repeater used as a sort of Paging TagCloud. To do so, I've added simple properties to the Page's ViewState such as a Page, RowCount, etc...
I feel like it doesn't belong there but I had bad experiences with server controls, debugging, dll and deployment.
Could I just inherit the Repeater class, add a few ControlState/ViewState properties and be able to use it exactly as a Repeater dragged straight from the ToolBox?
Here, having the following simple class:
public class TagCloud : Repeater
{
public int selectedIndex;
public TagCloud()
{
selectedIndex = -1;
//
// TODO: Add constructor logic here
//
}
public int SelectedIndex
{
get { return selectedIndex; }
set { selectedIndex = value; }
}
}
Without creating a new WebControlLibrary project, could this cs file stands in the App_Code folder and work like expected?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,这样做没有问题。 只要使用控件的属性语法:
public int RowCount
{
}
此外,为了使您的属性看起来与默认属性相同,您可以添加 Description 或 DefaultValue 属性。
Yes, there is no problem doing this. As long as you use the control's property syntax:
public int RowCount
{
}
Also, to make your properties look the same as the default ones you can add Description or DefaultValue attributes.
这是可行的,并且是构建服务器控件的一种建议方法。 尝试一下看看。
This works, and is one suggested way of building server controls. Try it and see.
从 ASP Repeater 继承是一种完全有效的方法,只要您正在构建的控件是具有附加属性的转发器。
如果您觉得您需要的控件实际上是“其他东西”,并且恰好有一个转发器作为其控件集的一部分,那么您可能需要制作一个复合控件,将转发器添加到其控件集合中,以及任何其他控件需要。
例如,您可能想要一个具有转发器、搜索结果标签、转到转发器内容顶部和底部的链接等的控件。此复合控件不是转发器,但确实使用了转发器。
Inheriting from the ASP Repeater is a completely valid approach, so long as the control you are building IS a repeater with additional properties.
If you feel the control you need is actually "something else" that happens to have a repeater as part of its control set, then you likely need to make a composite control which adds a repeater to its control collection, along with whatever other controls are needed.
For example, you might want to have a control that has a repeater, a label of search results, links to go to the top and the bottom of the repeater's contents, etc. This composite control is not a repeater, but does use a repeater.