視圖xsl定製之嵌入服務器控件

SharePoint 2010 視圖 xsl 文件中支持嵌入服務器控件,嵌入服務器控件時,系統先採用xsl將視圖xml解析成一個相似UserControl的存在,而後執行UserControl。html

代碼示例以下:api

 

<xsl:stylesheet xmlns:x="http://www.w3.org/2001/XMLSchema" xmlns:d="http://schemas.microsoft.com/sharepoint/dsp" version="1.0" exclude-result-prefixes="xsl msxsl ddwrt" xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime" 
                xmlns:asp="http://schemas.microsoft.com/ASPNET/20" xmlns:__designer="http://schemas.microsoft.com/WebParts/v2/DataView/designer" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" 
                xmlns:SharePoint="Microsoft.SharePoint.WebControls" xmlns:codeart="CodeArt.SharePoint.SmartQuery"   xmlns:ddwrt2="urn:frontpage:internal">
  <xsl:output method="html" indent="no" encoding="uft-8" />
  <xsl:include href="/_layouts/xsl/main.xsl" />
  <xsl:include href="/_layouts/xsl/internal.xsl" />
  <xsl:decimal-format NaN="" />
  <xsl:template match="/" xmlns:aa="System.Web.UI">
    <xsl:value-of disable-output-escaping="yes" select="'<%@ Register Tagprefix="codeart" Namespace="CodeArt.SharePoint.SmartQuery" Assembly="CodeArt.SharePoint.SmartQuery, Version=1.0.0.0, Culture=neutral, PublicKeyToken=df96d56002e5fe16" %>'"/>
      
    <hr />
    <codeart:XXXWebPart runat="server"/>      
    <hr />
    <xsl:apply-templates mode="RootTemplate" select="$XmlDefinition" />        
    <hr/>
  </xsl:template>
  <xsl:template name="FieldRef_header.ZhuanYe" match="FieldRef[@Name='ZhuanYe']" mode="header">
    <th nowrap="" scope="col" onmouseover="OnChildColumn(this)" class="ms-vh2">
      <div class="ms-vh-div">
        <xsl:copy-of select="current()" />
        專業
        <xsl:value-of select="@DisplayName"/>
      </div>
    </th>
  </xsl:template>
</xsl:stylesheet>

 

須要注意的是:服務器

並非全部服務器控件都是能夠嵌入xsl的,控件必須繼承於WebControl,而不能只繼承Control。緣由: app

XsltListViewWebPart解析xsl時,嵌入的控件解析成UserControl後,會增長額外的屬性,如:this

<hr xmlns:aa="System.Web.UI" xmlns:ddwrt2="urn:frontpage:internal" xmlns:SharePoint="Microsoft.SharePoint.WebControls" xmlns:__designer="http://schemas.microsoft.com/WebParts/v2/DataView/designer" xmlns:asp="http://schemas.microsoft.com/ASPNET/20" xmlns:d="http://schemas.microsoft.com/sharepoint/dsp" xmlns:x="http://www.w3.org/2001/XMLSchema"、>
<asp:UpdatePanel runat="server" xmlns:asp="http://schemas.microsoft.com/ASPNET/20"><ContentTemplate><asp:Calendar runat="server" xmlns:aa="System.Web.UI" xmlns:ddwrt2="urn:frontpage:internal" xmlns:SharePoint="Microsoft.SharePoint.WebControls" xmlns:__designer="http://schemas.microsoft.com/WebParts/v2/DataView/designer" xmlns:d="http://schemas.microsoft.com/sharepoint/dsp" xmlns:x="http://www.w3.org/2001/XMLSchema" /></ContentTemplate></asp:UpdatePanel>

這些強制加入的名稱控件屬性顯然不是標準的控件屬性,而Control沒有實現IAttributeAccessor, 不能任意增長自定義屬性,因此會出錯,WebControl實現了IAttributeAccessor接口,能夠增長任意屬性,全部不會出錯,以上示例代碼中嵌入了UpdatePanel,UpdatePanel直接繼承Control,因此會出差。錯誤信息通常爲:spa

分析器錯誤 
說明: 在分析向此請求提供服務所需資源時出錯。請檢查下列特定分析錯誤詳細信息並適當地修改源文件。 
分析器錯誤消息: 類型「xxx」不具備名爲「xmlns:asp」的公共屬性。
相關文章
相關標籤/搜索