I have an alert bar that gets its text from a smartform. I'm trying to set this up so when the smartform field "Alert" is null then the alert bar will be hidden. Here's my Code:
<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>
Here's my Code Behind for this so far:
XmlDocument al = new XmlDocument();
if (al.SelectSingleNode("/root/Alert") != null)
{
alertBox.Visible = false;
}
else
{
alertBox.Visible = true;
}
Let's start out making sure your XmlDocument is referencing the smartform xml from your content block control.
With that out of the way, here's how I would test to verify that (a) the node exists and (b) the node contains text.
But since you have the content block control rendering the actual alert content, you could get away with something like this.
In Ektron SmartForms, there is the option of making a field optional, so it's possible that the
/root/Alertnode will be null. But it's also possible that the node will exist in the xml and simple not have any content. If it is configured as a rich-text field, then it can be difficult to completely clear out the field's HTML - often you end up with xml that looks like the following:That's why I'm testing the
InnerTextproperty of the node. I'm using the Null-Conditional Operator?.to account for the fact that the node itself might be null. If your Ektron site is not using the newer C# language features (I know, null-conditionals have been around for a few years now.), then you'd have to check for null separately.I'd like to suggest an alternative, if I may. I see from your code that you are already using an XSLT to display the content of the alert. You could move the markup of the
alertBoxdiv into your XSLT and perform the "null-or-empty" test in there. Then you wouldn't need any code-behind for this particular feature.Note that the
alertBoxtemplate in this XSLT will work for both plain-text and rich-text fields. It is usingcopy-ofto display whatever is passed in, so when calling the template, care needs to be taken to pass in either thetext()(plain-text) ornode()(html/rich-text) of the element according to its type.