New to Telerik ReportingStart a free 30-day trial

XHTML Validation in HtmlTextBox

The HtmlTextBox requires you to provide valid XHTML and properly handle the content you set as its value.

To validate the defined content of the HtmlTextBox, use the IsValidXhtml expressions function or the Telerik.Reporting.Processing.XhtmlValidator.IsValidXhtml static method.

Specifically, the HtmlTextBox supports the following approaches for validating its XHTML content:

Using the IsValidXhtml Method in Expression

The following example shows how to use the IsValidXhtml method inside the HtmlTextBox Expression:

C#

            const string validXhtml = "<b>valid xhtml</b>.";
            const string systemXhtml = "Provided html is not acceptable.";

            Telerik.Reporting.HtmlTextBox txt = new Telerik.Reporting.HtmlTextBox();
            txt.Value = string.Format("=If(IsValidXhtml('{0}'), '{0}', '{1}')", validXhtml, systemXhtml);

Using Events and the IsValidXhtml Method

The following example demonstrates how to apply the IsValidXhtml method inside the HtmlTextBox ItemDataBinding event handler:

C#

            const string validXhtml = "<b>valid xhtml</b>.";
            const string systemXhtml = "Provided html is not acceptable.";

            Telerik.Reporting.HtmlTextBox txt = new Telerik.Reporting.HtmlTextBox();
            txt.ItemDataBinding += delegate(object sender, EventArgs args)
                {
                    Telerik.Reporting.Processing.HtmlTextBox procTxt = (Telerik.Reporting.Processing.HtmlTextBox)sender;
                    if (Telerik.Reporting.Processing.XhtmlValidator.IsValidXhtml(validXhtml))
                    {
                        procTxt.Value = validXhtml;
                    }
                    else
                    {
                        procTxt.Value = systemXhtml;
                    }
                };

Using Events and the ValueError Exception

The following try-catch-block approach shows how to handle the exception:

C#

            const string invalidXhtml = "<b>invalid xhtml.";
            const string systemXhtml = "Provided html is not acceptable.";

            Telerik.Reporting.HtmlTextBox txt = new Telerik.Reporting.HtmlTextBox();
            txt.ItemDataBinding += delegate(object sender, EventArgs args)
                {
                    Telerik.Reporting.Processing.HtmlTextBox procTxt = (Telerik.Reporting.Processing.HtmlTextBox)sender;
                    try
                    {
                        procTxt.Value = invalidXhtml;
                        // html that will not be accepted
                    }
                    catch (Exception)
                    {
                        procTxt.Value = systemXhtml;
                    }
                };

See Also