zoukankan      html  css  js  c++  java
  • 自己开发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)):告诉属性浏览器提供扩展和折叠样式

  • 相关阅读:
    Java Calendar详解
    Java Calendar 类的时间操作
    java获取properties配置文件值
    解决Eclipse中.properties文件中文乱码问题
    java web每天定时执行任务(四步轻松搞定)
    为经典版eclipse增加web and JavaEE插件
    VMware创建虚拟机教程详解及问题解决
    Eclipse对svn操作切换账号或更换svn地址方法
    oracle 查看用户所在的表空间
    (转)CentOS之7与6的区别
  • 原文地址:https://www.cnblogs.com/yvesliao/p/856285.html
Copyright © 2011-2022 走看看