[英]How to hide elements when a smartform field is null
我有一個警報欄,它從smartform獲取其文本。 我正在嘗試進行設置,以便當smartform字段“ Alert”為空時,警報欄將被隱藏。 這是我的代碼:
<div runat="server" ID="alertBox" class="alert alert-danger"> <a href="#" class="close" data-dismiss="alert" aria-label="close">×</a> <center> <CMS:ContentBlock ID="alert" runat="server" Visible="true" DisplayXslt="/xmlfiles/Alert.xslt" DefaultContentID="2147499035" CssClass="text" /> </center> </div>
到目前為止,這是我的背后代碼:
XmlDocument al = new XmlDocument();
if (al.SelectSingleNode("/root/Alert") != null)
{
alertBox.Visible = false;
}
else
{
alertBox.Visible = true;
}
讓我們開始確保您的XmlDocument從內容塊控件中引用smartform xml。
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(alert.EkItem.Html);
有了這些,我將測試如何驗證(a)節點是否存在以及(b)節點是否包含文本。
string alertContent;
// If dealing with a plain-text field...
var txtAlertNode = xmlDoc.SelectSingleNode("/root/txtAlert");
alertContent = txtAlertNode?.InnerText;
if (string.IsNullOrWhiteSpace(alertContent))
{
Console.WriteLine("No txtAlert content.");
// alertBox.Visible = false;
}
else
{
Console.WriteLine("ALERT: " + alertContent);
// alertBox.Visible = true;
}
// If dealing with a rich-text field...
var rtfAlertNode = xmlDoc.SelectSingleNode("/root/rtfAlert");
if (string.IsNullOrWhiteSpace(rtfAlertNode?.InnerText))
{
Console.WriteLine("No rtfAlert content.");
// alertBox.Visible = false;
}
else
{
alertContent = rtfAlertNode.InnerXml;
Console.WriteLine("ALERT: " + alertContent);
// alertBox.Visible = true;
}
但是,由於您具有呈現實際警報內容的內容塊控件,因此可以避免類似的事情。
var alertNode = xmlDoc.SelectSingleNode("/root/Alert");
if (string.IsNullOrWhiteSpace(alertNode?.InnerText))
{
alertBox.Visible = false;
}
else
{
alertBox.Visible = true;
}
在Ektron SmartForms中,可以選擇使字段為可選,因此/root/Alert
節點可能為空。 但是該節點也可能存在於xml中,並且簡單地沒有任何內容。 如果將其配置為RTF字段,則可能很難完全清除該字段的HTML-通常您最終得到的XML如下所示:
<root>
<Alert>
<p></p>
</Alert>
</root>
這就是為什么我要測試節點的InnerText
屬性。 我正在使用Null-Conditional運算符 ?.
考慮到節點本身可能為null的事實。 如果您的Ektron網站沒有使用較新的C#語言功能(我知道,空條件已經存在了幾年。),那么您就必須單獨檢查空值。
var alertNode = xmlDoc.SelectSingleNode("/root/Alert");
if (alertNode != null && string.IsNullOrWhiteSpace(alertNode.InnerText))
{
alertBox.Visible = false;
}
else
{
alertBox.Visible = true;
}
如果可以的話,我想提出一個替代方案。 從您的代碼中可以看到,您已經在使用XSLT來顯示警報的內容。 您可以將alertBox
div的標記移到XSLT中,然后在其中執行“ null-or-empty”測試。 然后,您將不需要任何代碼隱藏此特定功能。
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
<xsl:output method="html" indent="yes"/>
<xsl:template match="/">
<xsl:call-template name="alertBox">
<xsl:with-param name="content" select="root/txtAlert/text()"/>
</xsl:call-template>
<xsl:call-template name="alertBox">
<xsl:with-param name="content" select="root/rtfAlert/node()"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="alertBox">
<xsl:param name="content"/>
<xsl:if test="$content and (string-length(translate(normalize-space($content/text()), ' ', '')) > 0 or string-length(translate(normalize-space($content), ' ', '')) > 0)">
<div runat="server" ID="alertBox" class="alert alert-danger">
<a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
<center>
<xsl:copy-of select="$content"/>
</center>
</div>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
請注意,此XSLT中的alertBox
模板將適用於純文本和富文本字段。 它使用copy-of
來顯示任何傳入的內容,因此在調用模板時,需要小心地傳入其中的text()
(純文本)或node()
(html / rich-text)根據其類型的元素。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.