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)):告诉属性浏览器提供扩展和折叠样式

  • 相关阅读:
    js实现延迟加载
    异步处理的方式
    Server Tomcat v7.0 Server at localhost was unable to start within 45 seconds
    H-ui出现提交后没办法关闭
    css中zoom:1以及z-index的作用
    【转载】CSS3 常用四个动画(旋转、放大、旋转放大、移动)
    [转]png图片压缩大小但是不改变透明部分
    CSS颜色代码大全
    转载自前端开发:CSS设置滚动条样式
    转:css中overflow:hidden 不起作用了吗?
  • 原文地址:https://www.cnblogs.com/yvesliao/p/856285.html
Copyright © 2011-2022 走看看