ASP.NET C#中的自定义控件 [英] Custom Control in ASP.NET C#

查看:133
本文介绍了ASP.NET C#中的自定义控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个简单的自定义控件,该控件仅继承自Literal控件,并且还没有任何扩展名,代码为空.

I created a simple custom control that only inherits from the Literal control, and doesn't have any extensions yet, code is empty.

命名空间:CustomControls

Namespace: CustomControls

类名:Literal:System.Web.UI.WebControls.Literal

Class name: Literal : System.Web.UI.WebControls.Literal

接下来我要做的就是在aspx页中注册该控件,如下所示:

Next thing I do is registering this control in the aspx page as following:

<%@ Register TagPrefix="web" Namespace="CustomControls" %>

(我在一些教程中读到,除了web.config等之外,这是注册它的方法之一.)

(I read in few tutorials that this is one of the ways to register it, besides web.config etc.)

毕竟,对我来说没有任何智慧,更糟糕的是,当我尝试在其中包含控件的页面上运行时,出现解析错误未知服务器标签:web".

After all, no intellisence for me, and worse- I get a parse error 'unknown server tag: web' when I try to run the page with the control in it.

如果需要此信息,我会使用创建新项目"而不是新网站.

I used 'create new project' and not new website, in case this info is needed.

可能是我的问题吗?

谢谢.

推荐答案

这是我的工作方式,一步一步地从零开始.第一种方法使用第二个项目/程序集.对于App_code版本,请向下滚动.

Here is how I did it, step by step starting from nothing. This first method uses a second project/assembly. For the App_code version scroll down.

Web应用程序项目方法

  1. 创建一个新的ASP.Net Web应用程序.记下名称,我的名称叫做WebApplication2.如果可能已经存在一个现有的Web应用程序,则双击项目的属性部分,然后检查程序集名称"属性,记下它.
  2. 在Web应用程序中创建一个名为Literal.cs的新类.
  3. 输入类似于以下代码的类定义:

  1. Create a new ASP.Net Web Application. Take note of the name, mine is called WebApplication2. If you already have an existing web application, which is likely, double click the properties section of your project and check the "Assembly Name" property, make note of it.
  2. Create a new Class in the web applcation named Literal.cs
  3. Put in code similar to the following for the class defenition:

namespace CustomControls
{
    public class Literal : System.Web.UI.WebControls.Literal
    {
    }
}

  • 在aspx页面顶部添加以下注册标记

  • Add the following register tag to the top of your aspx page

    <%@寄存器程序集="WebApplication2" namespace ="CustomControls" tagprefix =网络" %>

    <%@ Register assembly="WebApplication2" namespace="CustomControls" tagprefix="web" %>

    如果您的程序集名称不同,则在此处进行更改.我注意到,当我在VB.Net中执行此操作时,名称空间是WebApplication1.CustomControls,而不是像C#中那样只是CustomControls,有点奇怪.

    If your assembly name was different then change it here. I noticed that when I did this in VB.Net the namespace was WebApplication1.CustomControls instead of just CustomControls like it was in C#, kind of odd.

    1. 将新控件添加到您的页面:

    1. Add the new control to your page:

    < web:Literal ID ="Literal1" runat =服务器"文字=测试"/>

    <web:Literal ID="Literal1" runat="server" Text="test" />

    单独的项目方法

    1. 创建一个新的空白网站(ASP.Net).
    2. 将新的名为CustomControls的ASP.Net服务器控件库添加到解决方案中.
    3. 向名为Literal的新项目添加新类(我使用的是C#,因此我的文件名为Literal.cs).以下是我的超级基本代码,我认为应该与问题中描述的代码匹配.

    1. Create a new Empty Website (ASP.Net).
    2. Add a new ASP.Net Server Control library named CustomControls to the solution.
    3. Add a new Class to the new project called Literal (I'm using C# so my file is named Literal.cs). Below is my super basic code, that I believe should match the code described in the question.

    namespace CustomControls
    {
        public class Literal : System.Web.UI.WebControls.Literal
        {
        }
    }
    

  • 向您的网站添加对CustomControls项目的引用.

  • Add a reference to the CustomControls project to your website.

    将程序集注册添加到aspx页面的顶部:

    Add the assembly registration to the top of your aspx page:

    <%@寄存器程序集="CustomControls" namespace ="CustomControls" tagprefix =网络" %>

    <%@ Register assembly="CustomControls" namespace="CustomControls" tagprefix="web" %>

    将控件的新实例添加到页面:

    Add a new instance of the control to your page:

    < web:Literal ID ="Literal1" runat =服务器"文字=测试"/>

    <web:Literal ID="Literal1" runat="server" Text="test" />

    在App_Code方法中

    1. 创建一个新的空白网站(ASP.Net).
    2. 向App_Code文件夹Literal2添加一个新类(我使用的是C#,因此我的文件名为Literal2.cs).以下是我的超级基本代码,我相信应该与问题中描述的代码匹配.我将其称为2,以便您可以将其与上述方法结合使用,而不会出现编译错误

    1. Create a new Empty Website (ASP.Net).
    2. Add a new Class to the App_Code folder Literal2 (I'm using C# so my file is named Literal2.cs). Below is my super basic code, that I believe should match the code described in the question. I called it 2 so that you can use this in conjunction with the method described above without getting compile errors

    namespace CustomControls
    {
        public class Literal2 : System.Web.UI.WebControls.Literal
        {
        }
    }
    

  • 通过在顶部添加以下行,在aspx页中注册app_code的程序集/名称空间

  • Register the assembly/namespace for app_code in your aspx page by adding the following line to the top

    <%@注册命名空间="CustomControls"; Assembly ="__ code" tagprefix =网络" %>

    <%@ Register Namespace="CustomControls" Assembly="__code" tagprefix="web" %>

    将新控件的实例添加到您的页面:

    Add an instance of the new control to your page:

    < web:Literal2 ID ="literal2" runat =服务器"文字="test2"/>

    <web:Literal2 ID="literal2" runat="server" Text="test2" />

    我使用Visual Studio对此进行了测试,对我来说一切正常.

    I tested this using visual studio and it all worked fine for me.

    这篇关于ASP.NET C#中的自定义控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆