This is how I'm creating my checkbox:
HtmlInputCheckBox checkbox = new HtmlInputCheckBox();
checkbox.ID = _Data.ControlID;
checkbox.Attributes.Add("class", "checkbox");
checkbox.Attributes.Add("autocomplete", "off");
sReplacementString = element.RenderToString();
RenderToString
is an extension that does this:
public static string RenderToString(this Control control)
{
StringBuilder sb = new StringBuilder();
using (StringWriter writer = new StringWriter(sb))
{
using (HtmlTextWriter htmlWriter = new HtmlTextWriter(writer))
{
control.RenderControl(htmlWriter);
}
}
return sb.ToString();
}
This produces a string with a closing tag on the input, I can see this when debugging.
<input name="ttWBF_1" type="checkbox" id="ttWBF_1" autocomplete="off" class="checkbox" />
It's then added to the HTML using the Agility Pack as such:
HtmlNode temp = doc.CreateElement("temp");
temp.InnerHtml = sReplacementString;
HtmlNode current = inputNode;
foreach (HtmlNode child in temp.ChildNodes)
{
inputNode.ParentNode.InsertAfter(child, current);
current = child;
}
inputNode.ParentNode.RemoveChild(inputNode);
However in the HTML the input tag for the checkbox is missing its self-closing slash and therefore fails WC3 validation.
<input name="ttWBF_1" type="checkbox" id="ttWBF_1" autocomplete="off" class="checkbox">
This happens with my textboxes which are generated in the same way. it looks like they're getting lost when adding the HTML to the page using the agility pack.
How do I prevent this?
is input a self closing tag? Yes, input is a self closing tag. You need not close it as it is an empty tag. It only has attributes but no content.
A self-closing tag is an element of HTML code that has evolved in the language. Typically, the self-closing tag makes use of a “/” character in order to effectively close out a beginning tag enclosed in sideways carets.
Optional HTML Closing TagsSome closing tags are optional, however. The tags are optional because it's implied that a new tag would not be able to be started without closing it.
Html Agility Pack (HAP) is a free and open-source HTML parser written in C# to read/write DOM and supports plain XPATH or XSLT. It is a . NET code library that allows you to parse "out of the web" HTML files.
Try setting "OptionWriteEmptyNodes" flag:
HtmlDocument doc = new HtmlDocument();
doc.OptionWriteEmptyNodes = true;
// ....
UPDATE
Since my original answer was rejected, here's another possible solution.
Are you passing the correct DOCTYPE to your HTML document before rendering it? Take a look at this SO question for how to insert a DOCTYPE: Add a doctype to HTML via HTML Agility pack
In the end it pains me to say that I fell back on processing the HTML with regex to add in the mising self-closing tag. I'd love a better solution as this is hacky and not future proof - it has to be added in for every tag that needs correcting:
sXHTML = Regex.Replace(sXHTML, "<input(.*?)>", "<input $1 />");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With