Asp.Net Web控制項 (八)(TabControl 索引標籤控制項)

來源:互聯網
上載者:User

在項目開發中經常會用到索引標籤控制項,網上也有很多,其實只是簡單的功能,很多卻實現的很複雜,功能很強大,並不是我需要的。

下面來實現一個簡單的TabControl 。

先看示範:

位置:TabContorl示範
<hxj:TabControl ID="TabControl1" runat="server" TabSelectedIndex="0">
<hxj:TabItem Text="Section 1" runat="server">
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh
euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.
</hxj:TabItem>
<hxj:TabItem Text="Section 2" runat="server">
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh
euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Lorem ipsum dolor
sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt
ut laoreet dolore magna aliquam erat volutpat.
</hxj:TabItem>
<hxj:TabItem Text="Section 3" runat="server">
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh
euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Lorem ipsum dolor
sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt
ut laoreet dolore magna aliquam erat volutpat. Lorem ipsum dolor sit amet, consectetuer
adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna
</hxj:TabItem>
<hxj:TabItem Text="Section 4" runat="server">
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh
euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Lorem ipsum dolor
sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt
ut laoreet dolore magna aliquam erat volutpat. Lorem ipsum dolor sit amet, consectetuer
adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna
aliquam erat volutpat. Lorem ipsum dolor sit amet, consectetuer adipiscing elit,
sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.
</hxj:TabItem>
<hxj:TabItem Text="Section 5" runat="server">
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh
euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Lorem ipsum dolor
sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt
ut laoreet dolore magna aliquam erat volutpat. Lorem ipsum dolor sit amet, consectetuer
adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna
aliquam erat volutpat. Lorem ipsum dolor sit amet, consectetuer adipiscing elit,
sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh
euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.
</hxj:TabItem>
</hxj:TabControl>

 

運行出來的效果:

 

下面開始貼代碼:

[ControlBuilder(typeof(TabItemBuilder))]
[DefaultProperty("TabSelectedIndex")][ToolboxData("<{0}:TabControl runat=server></{0}:TabControl>")][ParseChildren(typeof(TabItem))][Description("選項卡Web控制項")][Designer(typeof(Hxj.Web.UI.Design.HxjControlDesigner))]public class TabControl : WebControl

其中[ParseChildren(typeof(TabItem))] 表示控制項嵌套的子節點必須是TabItem控制項,就是上面的範例程式碼。

 

控制項比較簡單值定義了兩個自訂屬性:

[Description("預設顯示的選項卡項")]public int TabSelectedIndex
[Description("自訂選項卡CSS樣式")]public string HeaderCssClass

 

下面是控制項輸出HTML:

    int totalTab = TabItems.Count;    if (!string.IsNullOrEmpty(HeaderCssClass))    {        writer.AddAttribute(HtmlTextWriterAttribute.Class, HeaderCssClass);    }    else    {        writer.AddAttribute(HtmlTextWriterAttribute.Class, "tabheader");    }    writer.AddAttribute(HtmlTextWriterAttribute.Id, this.ClientID + "header");    writer.RenderBeginTag(HtmlTextWriterTag.Div);    writer.RenderBeginTag(HtmlTextWriterTag.Ul);    for (int i = 0; i < totalTab; i++)    {        if (i == TabSelectedIndex)        {            writer.AddAttribute(HtmlTextWriterAttribute.Class, "current");        }        if (!TabItems[i].Visible)        {            writer.AddStyleAttribute(HtmlTextWriterStyle.Display, "none");        }        writer.RenderBeginTag(HtmlTextWriterTag.Li);        writer.AddAttribute(HtmlTextWriterAttribute.Href, "javascript:void(0)");        writer.AddAttribute(HtmlTextWriterAttribute.Onclick, string.Concat("htabs('", this.ClientID, "',", i.ToString(), ")"));        writer.AddAttribute(HtmlTextWriterAttribute.Title, TabItems[i].ToolTip);        writer.RenderBeginTag(HtmlTextWriterTag.A);        writer.Write(TabItems[i].Text);        writer.RenderEndTag();        writer.RenderEndTag();    }    writer.RenderEndTag();    writer.RenderEndTag();    for (int i = 0; i < totalTab; i++)    {        if (i != TabSelectedIndex)        {            writer.AddStyleAttribute(HtmlTextWriterStyle.Display, "none");        }        writer.AddAttribute(HtmlTextWriterAttribute.Id, string.Concat(this.ClientID, "Content", i.ToString()));        writer.RenderBeginTag(HtmlTextWriterTag.Div);        TabItems[i].RenderControl(writer);        writer.RenderEndTag();    }
輸出的代碼也是很簡單。
 
其實也可以這麼寫:
<hxj:TabControl ID="TabControl1" runat="server" TabSelectedIndex="0">    <tab Text="123">     tab123    </tab></hxj:TabControl>
 主要是由於TabControl的特性
[ControlBuilder(typeof(TabItemBuilder))]

其中TabItemBuilder是自訂的ControlBuilder。

代碼如下:

public class TabItemBuilder : ControlBuilder{    public override Type GetChildControlType(string tagName, IDictionary attribs)    {        if (String.Compare(tagName, "tab", true) == 0)            return typeof(TabItem);        else            return null;    }}

即通過ControlBuilder 來實現自訂解析邏輯。

 

下載

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.