如何使用自定义服务器控件
我似乎在这方面遇到了很多麻烦。
我想要创建一个实现一些核心逻辑的 ASP.NET 控件,然后我希望能够从中派生出多个控件,每个控件都实现自己的专用逻辑。
我从用户控件开始,但找不到从中派生的方法。我尝试在派生控件中设置 Inherits 属性,但是无论我做什么,派生控件似乎都无法识别基本控件。
然后我通过使用继承自 Control 的常规类尝试了自定义服务器端控件。 (请注意,我的所有渲染都是通过代码完成的。)但我似乎找不到任何方法让页面识别该控件。我在 @Register 指令中尝试了不同的语法,但它要么告诉我 src 属性丢失,要么就是找不到控件。 (请注意,如果不需要,我宁愿不创建单独的程序集。)如果控件来自当前程序集,我不知道将什么作为程序集放置。
有人可以就此提出建议吗?有适合我的配置的示例,或者可能是完全不同的方法?
请注意,我当前没有使用页面/控件代码隐藏。我的所有页面脚本都与标记存储在同一文件中。
I seem to be having a lot of trouble with this.
I want to create an ASP.NET control that implements some core logic, and then I want to be able to derive several controls from that, which each implement their own specialized logic.
I started with a User Control, but I couldn't find a way to derive from it. I tried setting the Inherits attribute in the derived control but, no matter what I did, the derived control just didn't seem able to recognize the base control.
So then I tried a custom server side control by using a regular class that inherits from Control. (Note that all my rendering is done from code.) But I can't seem to find any way to get a page to recognize the control. I've tried different syntax in the @Register directive but either it tells me the src attribute is missing or it just can't find the control. (Note that I prefer not to create a separate assembly if I don't have to.) I have no idea what to put as the assembly if the control is from the current assembly.
Can anyone make suggestions on this? Any examples that would work for my configuration, or perhaps a different approach entirely?
Note that I am not currently using page/control code-behind. All my page scripting is stored in the same file as my markup.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
事实上,尽管有各种关于 @Register 指令中缺少属性的错误消息,但我发现如果我将该指令中的属性数量减少到仅 tagprefix 和命名空间,它就可以正常工作。
In fact, despite various error messages about missing attributes in the @Register directive, I found it works just fine if I reduce the number of attributes in this directive to just tagprefix and namespace.