ASP.NET自定义控件 [英] ASP.NET Custom Controls

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

问题描述

如何创建一个自定义的控制(而不是ASCX控制),更重要的是,在项目中使用它?我想preFER不为它创建一个单独的项目,或者编译成一个DLL

How do you create a custom control (not an ASCX control) and, more importantly, use it in your project? I'd prefer not to create a separate project for it or compile it as a DLL

推荐答案

服务器控件应该编译成DLL。不应该有任何理由害怕在你的项目的附加组件,它有助于创造良好的项目组织。

Server controls should be compiled into a DLL. There should be no reason to be afraid of having an additional assembly in your project, and it helps create good project organization.

ASP.NET服务器控件实际上是在装配自定义类。它们没有与之关联的ASCX标记文件。

ASP.NET Server controls are actually custom classes in an assembly. They do not have an "ascx" markup file associated to them.

要使用ASP.NET服务器控件,你需要告诉ASP.NET到哪里寻找它的类。

To use a ASP.NET Server Control, you need to tell ASP.NET where to look for its class.

首先,创建一个类库项目并将其添加到您的解决方案,我会打电话给我的CustomControls.dll。

First, create a Class Library project and add it to your solution, I'll call mine "CustomControls.dll".

一旦出现,添加一个简单的类是你的夏精:

Once there, add a simple class to be your webcontrol:

public class Hello : System.Web.UI.WebControl
{
    public override Render(HtmlTextWriter writer)
    {
        writer.Write("Hello World");
        base.Render(writer);
    }
}

生成项目,并将其添加作为主Web项目的引用。

Build your project, and add it as a reference to your main web project.

现在,在您要使用该控件的ASPX页面,需要注册,所以将其添加为在ASPX第一线的页面指令之后:

Now, in the ASPX page that you want to use this control, you need to register it, so add this as the first line in the aspx AFTER the Page Directive:

<%@ Register TagPrefix="Example" Namespace="CustomControls" Assembly = "CustomControls" %>

现在,你应该有一个名为控&lt;例:你好&GT;提供给您。它可能把它一分钟,智能感知显示出来。

Now, you should have a control named <Example:Hello> available to you. It might take it a minute to show up in intellisense.

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

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