zoukankan      html  css  js  c++  java
  • 自定义 Web 服务器控件 拓荒者

    MSDN中关于本节内容的演练:http://msdn.microsoft.com/zh-cn/library/yhzc935f(v=vs.100).aspx

    微软的介绍已经足够详尽,我这里只是简单的说一下如何自定义Web 服务器控件。步骤大致如下:

    1. 新建一个控件类,使其继承自WebControl或要扩展的其它控件(例如 Button、TextBox等)
    2. 添加一些自己需要的属性和方法,然后重写Render、AddAttribute方法,使其可以在客户端展示。
    3. 功能完善以后,编译项目,在需要引用的地方添加Web.Config的配置项,使其可以正常的工作。

    就拿MSDN中的演练来说,我们可以新建一个空的web项目,然后添加一个Controls目录(为了层次结构,不是必须的),然后新建一个Web Control的类,名字为WelcomeLabel,代码如下:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Linq;
    using System.Security.Permissions;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace SampleCustomWebControl.Controls
    {
        [
            AspNetHostingPermission(SecurityAction.Demand,
                Level = AspNetHostingPermissionLevel.Minimal),
            AspNetHostingPermission(SecurityAction.InheritanceDemand,
                Level = AspNetHostingPermissionLevel.Minimal),
            DefaultProperty("Text"),
            ToolboxData("<{0}:WelcomeLabel runat=\"server\"> </{0}:WelcomeLabel>")
        ]
        public class WelcomeLabel : WebControl
        {
            [
                Bindable(true),
                Category("Appearance"),
                DefaultValue(""),
                Description("The welcome message text."),
                Localizable(true)
            ]
            public virtual string Text
            {
                get
                {
                    string s = (string)ViewState["Text"];
                    return (s == null) ? String.Empty : s;
                }
                set
                {
                    ViewState["Text"] = value;
                }
            }
    
            protected override void RenderContents(HtmlTextWriter writer)
            {
                writer.WriteEncodedText(Text);
                if (Context != null)
                {
                    string s = Context.User.Identity.Name;
                    if (s != null && s != String.Empty)
                    {
                        string[] split = s.Split('\\');
                        int n = split.Length - 1;
                        if (split[n] != String.Empty)
                        {
                            writer.Write(", ");
                            writer.Write(split[n]);
                        }
                    }
                }
                writer.Write("!");
            }
        }
    }

    完全的微软的代码,没有什么改动。需要说一下的是,上面的一堆特性主要是为了在设计时使用的。

    这里推荐从WebControl继承,而不是直接从Control类继承,因为WebControl 类从 Control 派生,并添加了与样式相关的属性,如 Font、ForeColor 和 BackColor。更贴近我们的使用习惯。

    控件的绘制过程

    在这个例子中,由于只设置了Text属性,并且是作为控件内容来呈现的,所以只需要重写RenderContents就可以了。

    控件在绘制的过程中,先调用Render方法,在Render方法中再调用RenderBeginTag、RenderContents和RenderEndTag方法。RenderBeginTag和RenderEndTag 方法是绘制标签的开始和结束的,他们依赖与TagName字段,只要重写了类的TagName字段,这两个方法就没有必要进行重写了。

    如果需要向控件中添加一些属性,则需要重写AddAttributesToRender方法。

    使用自定义Web服务器控件

    将项目编译为.dll文件以后,在web项目中引用。

    添加webConfig的配置:

          <pages>
            <controls>
              <add tagPrefix="cc" namespace="SampleCustomWebControl.Controls" assembly="SampleCustomWebControl" />
            </controls>
          </pages>

    在页面中使用:

    <cc:WelcomeLabel ID="lblWelcome" runat="server" Text="欢迎访问"></cc:WelcomeLabel>

    程序中也可以动态的改变其Text属性:

    lblWelcome.Text = "欢迎访问,服务器时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  • 相关阅读:
    gcd
    主流浏览器对HTML5的兼容性
    Adobe与苹果之争落败:停止开发移动版Flash
    谷歌程序员年薪高达25万美元以上
    Delphi开发人员指南 第一部份快速开发的基础 第2章 Object Pascal 语言(二)
    Delphi开发人员指南 第一部份快速开发的基础 第2章 Object Pascal 语言(三)
    Delphi开发人员指南 第一部份快速开发的基础 第2章 Object Pascal 语言(一)
    2011年10月编程语言排行榜
    第一个Flash游戏,小到几乎看不出来是做什么的
    'release' is unavailable: not available in automatic reference counting mode
  • 原文地址:https://www.cnblogs.com/youring2/p/2847572.html
Copyright © 2011-2022 走看看