ASP.NET控件开发基础(17)

http://tech.ddvip.com   2007年09月02日    社区交流

内容摘要:ASP.NET控件开发基础

  1.TemplatedListCommandEventArgs为Command事件提供数据

  2.TemplatedListItemEventArgs为一般项提供数据

  3.TemplatedListItem表示TemplatedList的项

  三.编写TemplatedList

  1.TemplatedList主要功能简介

  提供一个ItemTemplate模板属性,提供三种不同项样式,ItemCommand 事件冒泡事件及4个事件

  ASP.NET控件开发基础(17)

  2.实现主要步骤

  以下为必须

  (1)控件必须实现 System.Web.UI.INamingContainer 接口

  (2)定义至少一个模板属性

  (3)定义DataSource数据源属性

  (4)定义控件项DataItem,即模板的一个容器

  (5)重写DataBind 方法及复合控件相关方法(模板控件为特殊的复合控件)

  当然还有其他额外的属性,样式,事件

  3.具体实现

  下面我们来具体看实现方法

  (1)定义控件成员属性

    静态变量#region 静态变量
  
    private static readonly object EventSelectedIndexChanged = new object();
    private static readonly object EventItemCreated = new object();
    private static readonly object EventItemDataBound = new object();
    private static readonly object EventItemCommand = new object();
    #endregion
  
    成员变量#region 成员变量
    private IEnumerable dataSource;
    private TableItemStyle itemStyle;
    private TableItemStyle alternatingItemStyle;
    private TableItemStyle selectedItemStyle;
    private ITemplate itemTemplate;
    #endregion
  
    控件属性#region 控件属性
  
    [
    Category("Style"),
    Description("交替项样式"),
    DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
    NotifyParentProperty(true),
    PersistenceMode(PersistenceMode.InnerProperty),
    ]
    public virtual TableItemStyle AlternatingItemStyle
    {
      get
      {
        if (alternatingItemStyle == null)
        {
          alternatingItemStyle = new TableItemStyle();
          if (IsTrackingViewState)
            ((IStateManager)alternatingItemStyle).TrackViewState();
        }
        return alternatingItemStyle;
      }
    }
  
    [
    Category("Style"),
    Description("一般项样式"),
    DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
    NotifyParentProperty(true),
    PersistenceMode(PersistenceMode.InnerProperty),
    ]
    public virtual TableItemStyle ItemStyle
    {
      get
      {
        if (itemStyle == null)
        {
          itemStyle = new TableItemStyle();
          if (IsTrackingViewState)
            ((IStateManager)itemStyle).TrackViewState();
        }
        return itemStyle;
      }
    }
  
    [
     Category("Style"),
     Description("选中项样式"),
     DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
     NotifyParentProperty(true),
     PersistenceMode(PersistenceMode.InnerProperty),
     ]
    public virtual TableItemStyle SelectedItemStyle
    {
      get
      {
        if (selectedItemStyle == null)
        {
          selectedItemStyle = new TableItemStyle();
          if (IsTrackingViewState)
            ((IStateManager)selectedItemStyle).TrackViewState();
        }
        return selectedItemStyle;
      }
    }
  
    [
    Bindable(true),
    Category("Appearance"),
    DefaultValue(-1),
    Description("The cell padding of the rendered table.")
    ]
    public virtual int CellPadding
    {
      get
      {
        if (ControlStyleCreated == false)
        {
          return -1;
        }
        return ((TableStyle)ControlStyle).CellPadding;
      }
      set
      {
        ((TableStyle)ControlStyle).CellPadding = value;
      }
    }
  
    [
    Bindable(true),
    Category("Appearance"),
    DefaultValue(0),
    Description("The cell spacing of the rendered table.")
    ]
    public virtual int CellSpacing
    {
      get
      {
        if (ControlStyleCreated == false)
        {
          return 0;
        }
        return ((TableStyle)ControlStyle).CellSpacing;
      }
      set
      {
        ((TableStyle)ControlStyle).CellSpacing = value;
      }
    }
  
    [
    Bindable(true),
    Category("Appearance"),
    DefaultValue(GridLines.None),
    Description("The grid lines to be shown in the rendered table.")
    ]
    public virtual GridLines GridLines
    {
      get
      {
        if (ControlStyleCreated == false)
        {
          return GridLines.None;
        }
        return ((TableStyle)ControlStyle).GridLines;
      }
      set
      {
        ((TableStyle)ControlStyle).GridLines = value;
      }
    }
  
    [
    Bindable(true),
    Category("Data"),
    DefaultValue(null),
    Description("数据源"),
    DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)
    ]
    public IEnumerable DataSource
    {
      get
      {
        return dataSource;
      }
      set
      {
        dataSource = value;
      }
    }
  
    [
    Browsable(false),
    DefaultValue(null),
    Description("项模板"),
    PersistenceMode(PersistenceMode.InnerProperty),
    TemplateContainer(typeof(TemplatedListItem))
    ]
    public virtual ITemplate ItemTemplate
    {
      get
      {
        return itemTemplate;
      }
      set
      {
        itemTemplate = value;
      }
    }
  
    [
    Bindable(true),
    DefaultValue(-1),
    Description("选中项索引,默认为-1")
    ]
    public virtual int SelectedIndex
    {
      get
      {
        object o = ViewState["SelectedIndex"];
        if (o != null)
          return (int)o;
        return -1;
      }
      set
      {
        if (value < -1)
        {
          throw new ArgumentOutOfRangeException();
        }
        //获取上次选中项
        int oldSelectedIndex = SelectedIndex;
        ViewState["SelectedIndex"] = value;
  
        if (HasControls())
        {
          Table table = (Table)Controls[0];
          TemplatedListItem item;
  
          //第一次选中项不执行
          if ((oldSelectedIndex != -1) && (table.Rows.Count > oldSelectedIndex))
          {
            item = (TemplatedListItem)table.Rows[oldSelectedIndex];
            //判断项类型,为了将选中项还原为数据项
            if (item.ItemType != ListItemType.EditItem)
            {
              ListItemType itemType = ListItemType.Item;
              if (oldSelectedIndex % 2 != 0)
                itemType = ListItemType.AlternatingItem;
              item.SetItemType(itemType);
            }
          }
          //第一次执行此项,并一直执行
          if ((value != -1) && (table.Rows.Count > value))
          {
            item = (TemplatedListItem)table.Rows[value];
            item.SetItemType(ListItemType.SelectedItem);
          }
        }
      }
    }
  
    #endregion

责编:豆豆技术应用

正在加载评论...