• ASP.NET自定义控件入门Demo


    最近看了MSDN关于自定义控件的介绍,根据官方的文档,自己学着做了一个简单的Demo给需要的朋友参考。

    ASP.NET 源生的TextBox是不带Label标签的,这里我要实现的是创建一个带Label标签的TextBox,并且默认填充Text值为GUID(只读) 

    实际上现在很多第三方组件都有这种控件,如Ext.Net,FineUI等等。这里只是为了学习了解自定义控件的开发。

    步入正题

    1.在VS2010中创建一个类库项目,我选择的框架是3.5。名称“MyTextBoxControl”

    2.然后新建一个类文件,名称“MyTextBoxControl.cs”

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Web;
    using System.ComponentModel;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Security.Permissions;
    namespace MyTextBoxControl
    {
        [
        AspNetHostingPermission(SecurityAction.Demand,
            Level = AspNetHostingPermissionLevel.Minimal),
        AspNetHostingPermission(SecurityAction.InheritanceDemand,
            Level = AspNetHostingPermissionLevel.Minimal),
        DefaultProperty("Text"),
        ToolboxData("<{0}:MyTextBoxControl runat="server"> </{0}:MyTextBoxControl>")
        ]
        public class MyTextBoxControl : TextBox//继承源生TextBox
        {
            /// <summary>
            /// 为TextBox扩展的Label标签
            /// </summary>
            [
            Bindable(true),
            Category("Appearance"),
            DefaultValue("Label1:"),
            Description("The control content text."),
            Localizable(true)
            ]
            
            public virtual string Label
            {
                get
                {
                    string s = (string)ViewState["Label"];
                    return (s == null) ? "Label1:" : s;//如果控件Label为空,默认值为"Label1"
                }
                set
                {
                    ViewState["Label"] = value;
                }
            }
    
            /// <summary>
            /// 重绘控件
            /// </summary>
            /// <param name="writer"></param>
            protected override void Render(System.Web.UI.HtmlTextWriter writer)
            {
                System.Web.UI.WebControls.Label lable = new Label();
                lable.ID = "Label1";
                lable.Text = this.Label;//设置标签文本
                lable.Font.Bold = true;//设置文本显示为粗体
                lable.RenderControl(writer);//将Label输出到控件中
                this.Width = Unit.Pixel(255);//设置默认宽度
                this.Text = Guid.NewGuid().ToString("N").ToUpper();//默认文本,设置默认Text值
                this.ReadOnly = true;//设置Text默认只读
                base.Render(writer);//输出控件
            }
    
        }
    }

    编译项目,会在Debug目录下生成MyTextBoxControl.dll文件

    3.新建一个网站项目测试自定义控件,项目名称“TestControl”先引用刚才的控件

    在VS2010菜单上选择“工具”》“选择工具箱”选择MyTextBoxControl.dll所在目录,确定

    然后在VS2010的工具栏上会出现刚才添加的控件了

    接着大家就可以像其他控件一样拖动使用了,请看下面的截图

    这里Label属性默认为空,我们可以设置成其他值。最后上一张运行图

    我这个Demo做的比较简单,只是入门。更复杂的官方文档有很详细的介绍 有兴趣的朋友可以多研究下http://msdn.microsoft.com/zh-cn/library/bb386519(v=vs.90).aspx

    Demo下载

  • 相关阅读:
    P1962 斐波那契数列(矩阵加速DP)
    P1939 【模板】矩阵加速(数列)
    剑指04.重建二叉树
    剑指03.从尾到头打印链表
    剑指02.替换空格
    剑指01.二维数组中的查找
    Python绘图学习笔记(待完善)
    时间观念 目标观念
    购房交税
    可以不做会计,但是不能不懂会计
  • 原文地址:https://www.cnblogs.com/hhwan/p/3948799.html
Copyright © 2020-2023  润新知