• ASP.NET自定义服务器控件


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

    文章内容整理自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

  • 相关阅读:
    30 Day Challenge Day 12 | Leetcode 198. House Robber
    30 Day Challenge Day 12 | Leetcode 276. Paint Fence
    30 Day Challenge Day 12 | Leetcode 265. Paint House II
    30 Day Challenge Day 12 | Leetcode 256. Paint House
    30 Day Challenge Day 11 | Leetcode 76. Minimum Window Substring
    30 Day Challenge Day 11 | Leetcode 66. Plus One
    oracle之6过滤与排序
    oracle之5多行函数之过滤分组函数
    oracle之4多行函数之分组函数
    oracle之3单行函数之条件表达式
  • 原文地址:https://www.cnblogs.com/ranran/p/4109963.html
Copyright © 2020-2023  润新知