程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> ASP.NET >> ASP.NET基礎 >> 如何為CheckBoxList和RadioButtonList添加滾動條

如何為CheckBoxList和RadioButtonList添加滾動條

編輯:ASP.NET基礎

如何給CheckBoxList和RadioButtonList添加滾動條?
繼承基類CheckBoxListRadioButtonList,添加滾動屬性,重寫Render方法即可。
屬性列表:

#region 滾動控制
    private bool _ShowScrollBar = false;
    /// <summary>
    /// 顯示滾動條
    /// </summary>
    [
    System.ComponentModel.Description("是否顯示顯示滾動條")
    , System.ComponentModel.DefaultValue(false)
    , System.ComponentModel.Category("滾動條設置")
    , System.ComponentModel.Bindable(System.ComponentModel.BindableSupport.Yes)
    ]
    public bool ShowScrollBar
    {
      get { return _ShowScrollBar; }
      set { _ShowScrollBar = value; }
    }
    private Overflow _OverflowY = Overflow.auto;
    /// <summary>
    /// 豎直滾動條
    /// </summary>
    [
    System.ComponentModel.Description("豎直滾動條")
    , System.ComponentModel.DefaultValue(Overflow.auto)
    , System.ComponentModel.Category("滾動條設置")
    , System.ComponentModel.Bindable(System.ComponentModel.BindableSupport.Yes)
    ]
    public Overflow OverflowY
    {
      get { return _OverflowY; }
      set { _OverflowY = value; }
    }
    private Overflow _OverflowX = Overflow.auto;
    /// <summary>
    /// 水平滾動條
    /// </summary>
    [
    System.ComponentModel.Description("水平滾動條")
    , System.ComponentModel.DefaultValue(Overflow.auto)
    , System.ComponentModel.Category("滾動條設置")
    , System.ComponentModel.Bindable(System.ComponentModel.BindableSupport.Yes)
    ]
    public Overflow OverflowX
    {
      get { return _OverflowX; }
      set { _OverflowX = value; }
    }
    private Unit _ScrollHeight = Unit.Parse("0px");
    /// <summary>
    /// 滾動高度
    /// </summary>
    [
    System.ComponentModel.Description("滾動高度")
    , System.ComponentModel.Category("滾動條設置")
     , DefaultValue("0px")
    , System.ComponentModel.Bindable(System.ComponentModel.BindableSupport.Yes)
    ]
    public Unit ScrollHeight
    {
      get { return _ScrollHeight; }
      set { _ScrollHeight = value; }
    }
    private Unit _ScrollWidth = Unit.Parse("0px");
    /// <summary>
    /// 滾動寬度
    /// </summary>
    [
    System.ComponentModel.Description("滾動寬度")
    , System.ComponentModel.Category("滾動條設置")
    , DefaultValue("0px")
    , System.ComponentModel.Bindable(System.ComponentModel.BindableSupport.Yes)
    ]
    public Unit ScrollWidth
    {
      get { return _ScrollWidth; }
      set { _ScrollWidth = value; }
    }
    private string _ScrollCssClass = "";
    /// <summary>
    /// 滾動樣式設置
    /// </summary>
    [
    System.ComponentModel.Description("滾動樣式設置")
    , System.ComponentModel.Category("滾動條設置")
    , System.ComponentModel.DefaultValue("")
    , System.ComponentModel.Bindable(System.ComponentModel.BindableSupport.Yes)
    ]
    public string ScrollCssClass
    {
      get { return _ScrollCssClass; }
      set { _ScrollCssClass = value; }
    }

    #region 書寫標簽
    void WriteBeginSpan(HtmlTextWriter writer)
    {
      if (this._ShowScrollBar)
      {
        StringBuilder strSpan = new StringBuilder();
        strSpan.Append("<span ");
        strSpan.Append(string.Format("style='overflow-y:{0};overflow-x:{1};",
          System.Enum.GetName(typeof(Overflow), this._OverflowY),
          System.Enum.GetName(typeof(Overflow), this._OverflowX)));
        if (this._ScrollHeight.ToString() != "0px")
        {
          strSpan.Append(string.Format("height:{0};", this._ScrollHeight));
        }
        if (this._ScrollWidth.ToString() != "0px")
        {
          strSpan.Append(string.Format("width:{0};", this._ScrollWidth));
        }
        strSpan.Append("';");
        if (!string.IsNullOrEmpty(_ScrollCssClass))
        {
          strSpan.Append(string.Format(" class='{0}'", _ScrollCssClass));
        }
        strSpan.Append(">");
        writer.Write(strSpan.ToString());
      }
    }
    void WriteEndSpan(HtmlTextWriter writer)
    {
      if (this._ShowScrollBar)
      {
        writer.Write("</span>");
      }
    }
    #endregion
    #endregion

重寫Render方法: 

 protected override void Render(HtmlTextWriter writer)
    {
      this.WriteBeginSpan(writer);
      base.Render(writer);
      this.WriteEndSpan(writer);
    } 

就這樣就可以了。
還要定義一個枚舉:

public enum Overflow
  {
    auto = 0,
    hidden = 1,
    scroll = 2,
    visible = 3,
    inherit = 4
  }

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持 。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved