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?
基础教程推荐
- SonarQube C# 分析失败“不是指针的有效行偏移" 2022-01-01
- 重新排序 WPF TabControl 中的选项卡 2022-01-01
- 获取C#保存对话框的文件路径 2022-01-01
- 从 C# 控制相机设备 2022-01-01
- 在 VB6 或经典 ASP 中使用 .NET 2022-01-01
- 如果条件可以为空 2022-01-01
- C# 9 新特性——record的相关总结 2023-04-03
- Mono https webrequest 失败并显示“身份验证或解密失败" 2022-01-01
- 将数据集转换为列表 2022-01-01
- 更新 Visual Studio 中的 DataSet 结构以匹配新的 SQL 数据库结构 2022-01-01
