• 自己开发asp.net服务器控件(2)-复杂的属性声明


    上次做了一个HyperLink,不浪费代码,继续用那个再加一个复杂的属性,不过这个属性纯粹只是声明,主要是为了测试,所以显示不用到,先看代码:

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

    namespace WebControlsByLyj
    {
         [DefaultProperty(
    "Text")]
         [ToolboxData(
    "<{0}:HyperLink runat=server></{0}:HyperLink>")]
         
    public class HyperLink : System.Web.UI.WebControls.WebControl
         
    {
             [Description(
    "要显示的链接名")]
             [DefaultValue(
    "Socan信息港")]
             [Category(
    "Appearance")]
             [Bindable(
    true)]
             
    public string Text
             
    {
                 
    get
                 
    {
                     
    string s = (string)ViewState["Text"];
                     
    return (s == null? "Socan信息港" : s;
                 }

                 
    set
                 
    {
                     ViewState[
    "Text"= value;
                 }

             }


             [Description(
    "要链接到的地址")]
             [DefaultValue(
    "http://www.socan.com.cn")]
             [Category(
    "Data")]
             [Bindable(
    true)]
             
    public string Link
             
    {
                 
    get
                 
    {
                     
    string s = (string)ViewState["Link"];
                     
    return (s == null? "http://www.socan.com.cn" : s;
                 }

                 
    set
                 
    {
                     ViewState[
    "Link"= value;
                 }

             }


             [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
             [NotifyParentProperty(
    true)]
             [Description(
    "控件摆放的位置")]
             [Category(
    "Appearance")]
             
    public LocationInfo Location
             
    {
                 
    get
                 
    {
                     LocationInfo l 
    = (LocationInfo)ViewState["Location"];
                     
    return l == null ? new LocationInfo(00) : l;
                 }

                 
    set
                 
    {
                     ViewState[
    "Location"= value;
                 }

             }


             
    protected override void Render(System.Web.UI.HtmlTextWriter writer)
             
    {
                 
    //这是好的方式
                 writer.AddAttribute("class", CssClass);
                 writer.AddAttribute(
    "href", Link);
                 writer.AddAttribute(
    "target""_blank");
                 
    //writer.AddStyleAttribute("color", "red");
                 writer.RenderBeginTag("a");
                 writer.Write(Text);
                 writer.RenderEndTag();
                 
    //这是不好的方式
                 
    //writer.Write("<a href='" + Link + "' style='color:red'>" + Text + "</a>");
             }

         }


         [TypeConverter(
    typeof(ExpandableObjectConverter))]
         
    public class LocationInfo
         
    {
             
    private int _x;

             [NotifyParentProperty(
    true)]
             
    public int X
             
    {
                 
    get return _x; }
                 
    set { _x = value; }
             }


             
    private int _y;

             [NotifyParentProperty(
    true)]
             
    public int Y
             
    {
                 
    get return _y; }
                 
    set { _y = value; }
             }


             
    public LocationInfo(int x, int y)
             
    {
                 _x 
    = x;
                 _y 
    = y;
             }

         }

    }

    比起上一个示例所增加的部分都加粗了,如果以前设计过Winform的控件,比较容易理解,其它的就不讲了,主要看一下属性的描述:

    DesignerSerializationVisibility:指定属性是否以及如何在代码中序列化
    NotifyParentProperty(true):指示当此特征应用到的属性的值被修改时是否通知其父属性
    TypeConverter(typeof(ExpandableObjectConverter)):告诉属性浏览器提供扩展和折叠样式

  • 相关阅读:
    mysql5.7-Group Replication
    yum安装mariadb-galera同步
    开启tomcat的apr模式,并利用redis做tomcat7的session的共享。
    利用Xtrabackup在不停机的情况下备用数据库迁移
    open-falcon(v0.2)安装grafana部署
    open-falcon(v0.2)部署手册(源码编译)
    阿里云ECS主机自定义进程监控
    Open-Falcon第七步安装报警模块(小米开源互联网企业级监控系统)
    Open-Falcon第六步安装Dashboard(小米开源互联网企业级监控系统)
    Open-Falcon第五步安装Query(小米开源互联网企业级监控系统)
  • 原文地址:https://www.cnblogs.com/yvesliao/p/856285.html
Copyright © 2020-2023  润新知