本文通过创建一个最简单的服务器控件,演示开发服务器端控件的流程。

文章内容整理自MSDN的编程指南,原文地址在文章末尾的资源中。

本文创建一个简单的服务器控件,名为 RedLabel。 它的使用方式为:

1
<f:redlabel text="this is a test !" runat="server"></f:redlabel>

这个标签会将自己的Text属性值以红色的样式输出到页面上。运行结果如图所示:

步骤

新建一个空白解决方案,在此解决方案下新建一个类库项目,名称为MyControl。在类库中新建一个服务器端控件,名称为RedLabel。如图所示:

打开RedLabel类,将整个类的代码修改为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
namespace MyControl
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:RedLabel runat=server><!--{0}:RedLabel-->")]
    public class RedLabel : Label
{
        //重写RenderContent函数,输出控件内容
        protected override void RenderContents(HtmlTextWriter output)
        {
            //将自己的Text属性(继承自Label类)值使用红色样式输出
            output.Write("<div style="color:red">" + Text + "</div>");
        }
    }
}

至此自定义label控件编写完毕。

配置程序集属性

1. 打开类库项目下的程序集属性文件:AssemblyInfo.cs,如图所示:

1. 在文件开头添加下面代码:

1
<strong>using System.Web.UI;</strong>

2. 在文件末尾添加下面的代码:

1
<strong> [assembly: TagPrefix("MyControl", "f")]</strong>

其中MyControl是命名空间的名称。f是自定义控件的标签前缀。

代码说明

如果控件要呈现在客户端浏览器中不可见的元素(如隐藏元素或 meta 元素),则应从 System.Web.UI.Control 派生该控件。 WebControl 类从 Control 派生,并添加了与样式相关的属性,如 Font、ForeColor 和 BackColor。 另外,自定义控件通过重写 RenderContents 方法将文本写入响应流中。

在页面中使用自定义控件

要在页面中使用自定义控件,需要事先进行注册,注册的目的是将控件的前缀和命名空间进行映射,这样就能通过标签名找到标签对应的实现类。注册的方式有两种

1.在页面中使用@ Register指令,如以下示例所示:

1
<%@ RegisterAssembly="ServerControl" TagPrefix="aspSample” Namespace="ServerControl"%>

2.在 Web.config文件中指定标记前缀/命名空间映射。 如果将在 Web应用程序的多个页中使用自定义控件,则该方法非常有用。 下面的示例显示了一个 Web.config文件,该文件指定了程序集MyControl中命名空间MyControl和标签前缀f的映射。

1
 
1
2
3
4
5
6
7
8
9
10
11
<!--?xml version="1.0"?-->
<configuration>
  <system.web>   
   <pages>
     <controls>
        
       </add>
     </controls>
   </pages>
  </system.web>
</configuration>

测试控件

在解决方案下新建web项目,在web项目中新建apsx页面,在页面中引入自定义控件(注意,控件前缀需要注册)。比如以下页面:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<%@ Page Language="C#"AutoEventWireup="true"CodeBehind="Default.aspx.cs"Inherits="MyControl.Web._Default" %>
<%@ Register Assembly="MyControl"TagPrefix="f"Namespace="MyControl" %>
htmlPUBLIC "-//W3C//DTDXHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  
<htmlxmlns="http: www.w3.org="" 1999="" xhtml"="">
<headrunat="server">
   <metahttp-equiv="content-type"content="text html;="" charset="utf-8"/">
   <title></title>
 
 
   <formid="form1"runat="server">
        <div>
            <f:redlabeltext="this is="" a="" test="" !"runat="server">
        </f:redlabeltext="this></div>
    
 
</formid="form1"runat="server"></metahttp-equiv="content-type"content="text></headrunat="server"></htmlxmlns="http:>

运行效果

资源:

MSDN创建服务器控件:http://msdn.microsoft.com/zh-cn/library/yhzc935f(v=vs.100).aspx

最新文章

  1. Hadoop 中利用 mapreduce 读写 mysql 数据
  2. spring mvc+mybatis+sql server简单配置
  3. Java NIO 系列教程
  4. iOS Unicode和汉字互转
  5. Windows Store App 全球化:引用分离资源文件中的资源
  6. Swift 02.Array
  7. BZOJ1393 [Ceoi2008]knights
  8. maven 私服 配置 转
  9. 解析:用 CSS3 和 JavaScript 制作径向动画菜单
  10. java中equals方法和hashcode方法的区别和联系,以及为什么要重写这两个方法,不重写会怎样
  11. 案例学编程系列:案例认识 Spring IOC
  12. A1110. Complete Binary Tree
  13. 2019年1月6日 没有nainai吃 习题1
  14. \r\n 回车换行浅析
  15. 牛客训练:小a与黄金街道(欧拉函数+快速幂)
  16. Codeforces 12D Ball cdq分治
  17. httpclient的get和post
  18. Sublime遇见中文乱码问题?
  19. IT运营新世界大会:广通软件开启双态运维大时代
  20. [CISCO] VLAN、TRUNK 和 VTP 简介

热门文章

  1. dokuwiki工具栏添加换行回车快捷键与按钮
  2. openstack删除僵尸卷
  3. vector读入指定行数但不指定列数的数字
  4. CMS漏洞检测工具 – CMSmap
  5. 一个http请求发送到后端的详细过程
  6. 《Linux内核分析》第四周学习总结 扒开系统调用的三成皮(上)
  7. Linux内核分析——计算机是如何工作的
  8. web项目部署在不同环境中需要修改配置文件的解决方法
  9. 手写vue双向绑定数据
  10. JS基础(二)数据类型