首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> ASP.NET 基礎 >> 關於ASP.NET

ASP.NET 自訂控件開發 第五篷:範本控件開發實戰

     編輯:關於ASP.NET

範本控件開發比組合控件更簡單,核心在於繼承 CompositeControl 並定義 ITemplate 屬性。本章將改造上章的登入控件,讓使用者能自訂輸入欄位的控件類型(TextBox、DropDownList 等)。

範本控件結構

定義 LoginTemplate、PasswordTemplate 等屬性,並在 CreateChildControls 中實例化:

[TemplateContainer(typeof(LoginContainer))]
[PersistenceMode(PersistenceMode.InnerProperty)]
public ITemplate LoginTemplate { get; set; }

原始登入控件 範本化後效果 設計時支援

使用時只需在標記中嵌入自訂內容:

<cc:TemplatedLogin runat="server">
    <LoginTemplate>
        <asp:DropDownList ID="UserList" runat="server" />
    </LoginTemplate>
    <PasswordTemplate>
        <asp:TextBox TextMode="Password" runat="server" />
    </PasswordTemplate>
</cc:TemplatedLogin>

範本控件賦予使用者極大彈性,是構建可重用 UI 元件的利器。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved