How to inherit from Control, rather than UserControl?(如何从 Control 继承,而不是 UserControl?)
问题描述
我有一个继承自 UserControl
的 ASP.net 控件:
I have an ASP.net control that inherits from UserControl
:
文件 1:
public partial class Controls_AllocationDuplicate : System.Web.UI.UserControl
文件 2:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="AllocationDuplicate.ascx.cs" Inherits="Controls_AllocationDuplicate" %>
我需要控件从 Control
,而不是 UserControl
:
File.aspx.cs (已更改):
public partial class Controls_AllocationDuplicate : System.Web.UI.Control
File.aspx (未更改):
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="AllocationDuplicate.ascx.cs" Inherits="Controls_AllocationDuplicate" %>
当我这样做时,Visual Studio 会给出一个编译错误:
When i do this, Visual Studio gives a compile error:
确保此代码文件中定义的类与继承"属性匹配,并且它扩展了正确的基类(例如 Page 或 UserControl).
Make sure that the class defined in this code file matches the 'inherits' attribute, and that it extends the correct base class (e.g. Page or UserControl).
我怎样才能让我的 control 从 Control
而不是 UserControl
继承?
How can I have my control inherit from Control
rather than UserControl
?
这个问题是 Stackoverflow 系列中的一个,模板化用户控件":
This question is one in the ongoing Stackoverflow series, "Templating user controls":
- 如何向用户控件添加模板?一个>
- 如何继承自Control,而不是继承自UserControl?
- UserControl 有 IsPostBack,但 Control 没有
- UserControl 没有名为 ContentTemplate 的公共属性
- 如何从 web.config 指定 CodeFileBaseClass?
推荐答案
您尝试制作自定义控件而不是用户控件,在这种情况下您不能使用 ascx 文件来保存您的设计,但需要务实地设计它们.
You try to make a custom control and not a user control in this case you can't use ascx file to hold your design but need to design them pragmatically.
这篇关于如何从 Control 继承,而不是 UserControl?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何从 Control 继承,而不是 UserControl?


基础教程推荐
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01