简体   繁体   English

C#ASP.NET自定义控件未显示

[英]C# ASP.NET Custom Control not showing up

I'm trying to construct a custom control for ASP.NET I started by creating a Web Application in VS2010 and creating a new .ascx page. 我正在尝试为ASP.NET构造自定义控件,我从在VS2010中创建Web应用程序并创建新的.ascx页开始。 The page is called "TestBox" and it's just a single TextBox control with "This is a test" as the text. 该页面称为“ TestBox”,它只是一个带有“ This is a test”作为文本的TextBox控件。

I built the project and then included the DLL in another website in order to make sure I would be able to move controls. 我构建了该项目,然后将该DLL包含在另一个网站中,以确保可以移动控件。 Based on a tutorial I found here I added the following line of code to the top of the page: 根据我在这里找到的教程,我在页面顶部添加了以下代码行:

<%@ Register TagPrefix="TestControl" Namespace="TestControl" Assembly="TestControl" %>

Then I added this to the page itself: 然后我将其添加到页面本身:

<TestControl:TestBox ID="TestBox1" runat="server" />

The code compiles and the page loads without throwing up any errors, but when it loads it's completely blank. 代码会编译,页面加载不会引发任何错误,但是加载时页面完全空白。 By introducing a deliberate runtime error, I determined that the TextBox is definitely being loaded, but the control itself still isn't showing up. 通过引入故意的运行时错误,我确定确实已加载TextBox,但控件本身仍未显示。

Am I missing something? 我想念什么吗?

Code for the TestControl: TestControl的代码:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="TestBox.ascx.cs" Inherits="TestControl.TestBox" %>
<asp:TextBox ID="TextBox1" runat="server" ontextchanged="TextBox1_TextChanged">This is a test</asp:TextBox>

I haven't touched the Designer code or the .cs code in any way. 我没有以任何方式接触过Designer代码或.cs代码。

EDIT: Figured it out. 编辑:想通了。 I had declared a namespace for the .CS file but not the .ASPX file itself. 我已经声明了.CS文件的名称空间,但没有声明.ASPX文件本身的名称空间。

The answer was that I had to add a namespace to the ASPX file itself and not just the underlying code file. 答案是我必须向ASPX文件本身添加名称空间,而不仅仅是基础代码文件。 I forgot to add Class="TestControl.TestBox" to the page declaration. 我忘记将Class="TestControl.TestBox"添加到页面声明中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM