如何在智能表单字段为空时隐藏元素



我有一个警报栏,可以从智能表单获取文本。我正在尝试进行设置,以便当智能表单字段"警报"为空时,警报栏将被隐藏。这是我的代码:

<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 中,并且没有任何内容。如果将其配置为富文本字段,则可能很难完全清除字段的 HTML - 通常最终会得到如下所示的 xml:

<root>
<Alert>
<p></p>
</Alert>
</root>

这就是我测试节点的InnerText属性的原因。我正在使用空条件运算符?.来解释节点本身可能为空的事实。如果您的 Ektron 站点没有使用较新的 C# 语言功能(我知道,null 条件已经存在了几年(,那么您必须单独检查 null。

var alertNode = xmlDoc.SelectSingleNode("/root/Alert");
if (alertNode != null && string.IsNullOrWhiteSpace(alertNode.InnerText))
{
alertBox.Visible = false;
}
else
{
alertBox.Visible = true;
}

如果可以的话,我想提出一个替代方案。我从您的代码中看到,您已经在使用 XSLT 来显示警报的内容。您可以将alertBoxdiv 的标记移动到 XSLT 中,并在其中执行"空或空"测试。然后,您不需要此特定功能的任何代码隐藏。

<?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()), ' ', '')) &gt; 0 or string-length(translate(normalize-space($content), ' ', '')) &gt; 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/富文本(。

最新更新