2016-10-24 46 views
0

我是ASP.NET新手。作爲來自THIS POST的後續問題,我在Ektron中有以下.Net控件,我想在我的網頁模板中顯示。在模板中插入我的.NET控件

控制:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="Gallery.ascx.cs" Inherits="Source_Controls_Alumni_Gallery" %> 
<asp:ListView ID="uxPhotoGallery" runat="server" ItemPlaceholderID="itemPlaceholder"> 
    <LayoutTemplate> 
     <ul> 
      <asp:PlaceHolder ID="itemPlaceholder" runat="server" /> 
     </ul> 
    </LayoutTemplate> 
    <ItemTemplate> 
     <li> 
      <%-- 
       I'm mixing up two different ways of referencing the incoming data. One is by casting 
       the DataItem to the incoming type, which gives you intellisense access to the properties. 

       The other is more of a dictionary approach in which you have to type out the property name 
       as a string. 

       I really like the casting approach, but it's mega-wordy. 
       --%> 
      <a href="<%#((Ektron.Custom.ViewModels.PressPhotoViewModel)Container.DataItem).ImageUrl %>"> 
       <img src="<%#((Ektron.Custom.ViewModels.PressPhotoViewModel)Container.DataItem).ImageUrl %>" alt="<%#Eval("Description") %>" /> 
       <div><%#Eval("Description") %></div> 
      </a> 
     </li> 
    </ItemTemplate> 
</asp:ListView> 

和後面的代碼:

using Ektron.Custom.SmartForms; 
using System; 
using System.Linq; 

public partial class Source_Controls_Alumni_Gallery : System.Web.UI.UserControl 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     var pressPhotoManager = new PressPhotoManager(); 

     // Whichever folder Id... 
     var photos = pressPhotoManager.GetList(75); 

     if (photos != null && photos.Any()) 
     { 
      uxPhotoGallery.DataSource = photos; 
      uxPhotoGallery.DataBind(); 
     } 
    } 
} 

我想插入控制到這個模板:

<%@ Page Title="" Language="C#" MasterPageFile="~/Source/Masterpages/MainMaster.master" AutoEventWireup="true" CodeFile="AlumniJobOpenings.aspx.cs" Inherits="Source_Templates_AlumniJobOpenings" %> 

<%@ Register Src="~/Source/Controls/SubHeader.ascx" TagPrefix="uc1" TagName="SubHeader" %> 
<%@ Register Src="~/Source/Controls/Shared/PrimarySection.ascx" TagPrefix="uc1" TagName="PrimarySection" %> 
<%@ Register Src="~/Source/Controls/JoinUs/StaffAndParalegals/SPOpenings.ascx" TagPrefix="uc1" TagName="SPOpenings" %> 
<%@ Register Src="~/Source/Controls/JoinUs/StaffAndParalegals/SPFilters.ascx" TagPrefix="uc1" TagName="SPFilters" %> 
<%@ Register Src="~/Source/Controls/Shared/RelatedContentModules.ascx" TagPrefix="uc1" TagName="RelatedContentModules" %> 
<%@ Register Src="~/Source/Controls/JoinUs/StaffAndParalegals/SPContactDetails.ascx" TagPrefix="uc1" TagName="SPContactDetails" %> 
<%@ Register Src="~/Source/Controls/Shared/TextImageAssetBlockModules.ascx" TagPrefix="uc1" TagName="TextImageAssetBlockModules" %> 
<%@ Register Src="~/Source/Controls/Shared/TextLinkBlockControl.ascx" TagPrefix="uc1" TagName="TextLinkBlockControl" %> 
<%@ Register TagPrefix="sp" TagName="Spinner" Src="~/Source/Controls/Alumni/Gallery.ascx" %> 




<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server"> 
    <uc1:SubHeader runat="server" ID="SubHeader" /> 
    <div class="container non-responsive"> 
     <div class="row"> 
      <div class="col-sm-8 alpha"> 
       <uc1:PrimarySection runat="server" ID="PrimarySection" /> 
       <div class="primary"> 
        <div class="container non-responsive"> 
         <div class="row"> 
          <div class="col-sm-8 alpha"> 
           <div class="primary"> 
            IMAGE GALLERY LIST SHOULD BE INSERTED HERE. 
           </div> 
          </div> 
          <div class="col-sm-4 beta"> 
          </div> 
         </div> 
        </div> 
       </div> 
      </div> 
      <div class="col-sm-4 beta"> 
       <uc1:SPContactDetails runat="server" ID="SPContactDetails" /> 
       <uc1:SPFilters runat="server" ID="SPFilters" Heading="Staff and Paralegal Openings" Text="Select an office below to learn more about current opportunities" /> 
       <uc1:RelatedContentModules runat="server" ID="RelatedContentModules" /> 
       <uc1:TextLinkBlockControl runat="server" ID="TextLinkBlockControl" /> 
       <uc1:TextImageAssetBlockModules runat="server" ID="TextImageAssetBlockModules" /> 
      </div> 
     </div> 
    </div> 
</asp:Content> 

回答

1

這裏是從頂部的一行:

<%@ Register TagPrefix="sp" TagName="Spinner" Src="~/Source/Controls/Alumni/Gallery.ascx" %> 

,並用於註冊在同一個頁面的另一個控制類似的一行:

<%@ Register Src="~/Source/Controls/SubHeader.ascx" TagPrefix="uc1" TagName="SubHeader" %> 

現在,看看上面提到的預先存在的項目的控制佈局。

<uc1:SubHeader runat="server" ID="SubHeader" /> 

什麼,你會發現,放置標籤是由在<%@ Register ... %>線配置的性能。具體來說,TagPrefix和TagName值。您可以使用這些值來設置自己的控制位置,按照以下格式:

<TagPrefix:TagName runat="server" ID="SomeUniqueID" [optional parameters] /> 

所以,在你的控制的情況下,你已經設置TagPrefix="sp"TagName="Spinner"。所以,你的控制位置會是這樣的:

<sp:Spinner runat="server" ID="uxAlumniSpinner" /> 

(ID就是一個例子)

從你的控制代碼,你沒有配置任何參數,因此上面可以正常工作。但是你可以提供至少一個參數,並且可能應該使控件更加可重用。

例如,您的方法調用中有一個硬編碼值75。我認爲這是指向Ektron文件夾,分類或集合。無論如何,這是一些容器ID。您可能希望在具有不同數據源的多個位置使用此控件 - 不同的容器ID。按照您設置的方式,您必須每次都創建一個新的控件來更新該值。

所以,如果我們的公共屬性添加到您的控制,使代碼隱藏看起來是這樣的:

using Ektron.Custom.SmartForms; 
using System; 
using System.Linq; 

public partial class Source_Controls_Alumni_Gallery : System.Web.UI.UserControl 
{ 

    // Added Property 
    private long _containerId = 0; 
    public long ContainerID { 
     get { return _containerId; } 
     set { _containerId = value; } 
    } 
    ///////// 

    protected void Page_Load(object sender, EventArgs e) 
    { 
     // Added inverted conditional to escape method 
     // if the _containerId is invalid. 
     if(_containerId <= 0) return; 
     /////////// 

     var pressPhotoManager = new PressPhotoManager(); 

     // Whichever folder Id... 
     var photos = pressPhotoManager.GetList(_containerId); 

     if (photos != null && photos.Any()) 
     { 
      uxPhotoGallery.DataSource = photos; 
      uxPhotoGallery.DataBind(); 
     } 
    } 
} 

然後,你可以指定容器ID無論何時何地您將控制。像這樣:

<sp:Spinner runat="server" ID="uxAlumniSpinner" ContainerID="75" /> 

作出最後在模板標記:

<%@ Register Src="~/Source/Controls/JoinUs/StaffAndParalegals/SPContactDetails.ascx" TagPrefix="uc1" TagName="SPContactDetails" %> 
<%@ Register Src="~/Source/Controls/Shared/TextImageAssetBlockModules.ascx" TagPrefix="uc1" TagName="TextImageAssetBlockModules" %> 
<%@ Register Src="~/Source/Controls/Shared/TextLinkBlockControl.ascx" TagPrefix="uc1" TagName="TextLinkBlockControl" %> 
<%@ Register TagPrefix="sp" TagName="Spinner" Src="~/Source/Controls/Alumni/Gallery.ascx" %> 


<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server"> 
    <uc1:SubHeader runat="server" ID="SubHeader" /> 
    <div class="container non-responsive"> 
     <div class="row"> 
      <div class="col-sm-8 alpha"> 
       <uc1:PrimarySection runat="server" ID="PrimarySection" /> 
       <div class="primary"> 
        <div class="container non-responsive"> 
         <div class="row"> 
          <div class="col-sm-8 alpha"> 
           <div class="primary"> 
            <sp:Spinner runat="server" ID="uxAlumniSpinner" ContainerID="75" /> 
           </div> 
          </div> 
          <div class="col-sm-4 beta"> 
          </div> 
         </div> 
        </div> 
       </div> 
      </div> 
      <div class="col-sm-4 beta"> 
       <uc1:SPContactDetails runat="server" ID="SPContactDetails" /> 
       <uc1:SPFilters runat="server" ID="SPFilters" Heading="Staff and Paralegal Openings" Text="Select an office below to learn more about current opportunities" /> 
       <uc1:RelatedContentModules runat="server" ID="RelatedContentModules" /> 
       <uc1:TextLinkBlockControl runat="server" ID="TextLinkBlockControl" /> 
       <uc1:TextImageAssetBlockModules runat="server" ID="TextImageAssetBlockModules" /> 
      </div> 
     </div> 
    </div> 
</asp:Content> 
+0

如若仍75背後的代碼?不應該是var photos = pressPhotoManager.GetList(ContainerID); ? – fred1234

+1

糟糕。糾正。您可以在Page_Load方法中和自定義代碼中的其他地方使用_containerId或ContainerID。只要保持一致即可。不要在一個地方使用_containerId,在另一個地方使用ContainerID。 ContainerID的主要目的是成爲一個公共財產,您可以在您放置的控件中進行配置。 – egandalf