Reusing ItemTemplates

Discussion in 'ASP .Net' started by Jonathan Wood, May 15, 2009.

  1. My Web application displays some data-driven tables using ItemTemplates.

    The tables are on different pages and will be bound to different SQL
    statements. However, I want table items to appear exactly the same in all
    tables.

    Is there anyway to create an ItemTemplate and then reuse that in different
    tables on different pages?

    Thanks.

    Jonathan
     
    Jonathan Wood, May 15, 2009
    #1
    1. Advertisements

  2. Yes. You can create your template in a .ascx file like this:

    <%@ Control Language="C#" AutoEventWireup="true" Inherits="myItemTemplate"
    %>

    <div class="ListGridCommandItem">
    <div style="float: left; margin-right: 20px">
    <asp:LinkButton ID="lnbAddRecord" runat="server"
    CommandName="InitInsert">
    <img alt="" runat="server"
    src="~/Images/GridEditing/AddRecord.gif"/>
    Add new
    </asp:LinkButton>
    </div>
    <div style="float: left; width: 100%; margin-left: -100%">
    &nbsp;
    </div>
    <div style="float: right; margin-left: 20px;">
    <asp:LinkButton ID="lnbRefreshRecords" runat="server"
    CommandName="RebindGrid">
    <img alt="" runat="server"
    src="~/Images/GridEditing/Refresh.gif"/>
    Refresh
    </asp:LinkButton>
    </div>
    <div style="float: right; margin-left: 10px;">
    <asp:LinkButton ID="lnbExportToWord" runat="server"
    CommandName="ExportWord">
    <img alt="" runat="server" src="~/Images/GridEditing/Word.jpg"/>
    Export to Word
    </asp:LinkButton>
    </div>
    <div style="float: right;">
    <asp:LinkButton ID="lnbExportToExcel" runat="server"
    CommandName="ExportExcel">
    <img alt="" runat="server"
    src="~/Images/GridEditing/Excel.jpg"/>
    Export to Excel
    </asp:LinkButton>
    </div>
    </div>


    And use it like this:

    myListView.ItemTemplate = LoadTemplate("myItemTemplate.ascx");

    --
    Eliyahu Goldin,
    Software Developer
    Microsoft MVP [ASP.NET]
    http://msmvps.com/blogs/egoldin



    __________ Information from ESET NOD32 Antivirus, version of virus signature database 4080 (20090515) __________

    The message was checked by ESET NOD32 Antivirus.

    http://www.eset.com
     
    Eliyahu Goldin, May 17, 2009
    #2
    1. Advertisements

  3. Thanks. May I ask what "myItemTemplate" refer to here (in the inherits
    property for the control)?

    Jonathan

     
    Jonathan Wood, May 19, 2009
    #3
  4. That's the name of the class in code-behind. I don't think you need to worry
    about it in a template. It is there because you start it as a user control.

    --
    Eliyahu Goldin,
    Software Developer
    Microsoft MVP [ASP.NET]
    http://msmvps.com/blogs/egoldin



    __________ Information from ESET NOD32 Antivirus, version of virus signature database 4088 (20090519) __________

    The message was checked by ESET NOD32 Antivirus.

    http://www.eset.com
     
    Eliyahu Goldin, May 19, 2009
    #4
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.