.net第一个服务器控件

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SKTControl
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:ServerControl1 runat=server></{0}:ServerControl1>")]
    public class Hello : WebControl
    {
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Text
        {
            get
            {
                String s = (String)ViewState["Text"];
                return ((s == null) ? "[" + this.ID + "]" : s);
            }

            set
            {
                ViewState["Text"] = value;
            }
        }

        protected override void RenderContents(HtmlTextWriter output)
        {
            output.Write(Text);
        }
    }
}

 注册组件:

<system.web>
    <pages>
      <controls>
        <add tagPrefix="uc" namespace="SKTControl" assembly="SKTControl,Version=1.0" />
      </controls>
    </pages>
  </system.web>

调用:

<uc:hello ID="hello" runat="server"></uc:hello>

结果:[Hello]

posted @ 2013-03-30 18:01  microsoftzhcn  阅读(214)  评论(0编辑  收藏  举报