ASP.NET 2.0 generated HTML not XHTML/XML compliant

Y

yoga weazel

OK, so let me start with what I'm trying to accomplish.

I have several screens with data displays using various controls (DataList,
DataGrid...). I need to provide functionality so that the user can click a
"Printable Version" button and a clean print formatted version of the data
chosen is displayed in a new window. I'd prefer to do as much of this client
side as possible so data services are not re-hit and not storing extraneous
data in Session or View state.

The approach I have currently is to provide a button which when clicked
calls javascript to open the GenericPrintReport.aspx page with the ID of the
original documents HTML element to print and the name of a XSL stylesheet to
use in a new window. The body of the GenericPrintReport.aspx page contains
the following javascript:

<pre>
<script language="javascript" type="text/javascript">
//get data from parentwindow.ctrl
var ctrl =
window.opener.document.getElementById('<%=Request.Params("CtrlId") %>');
//set DivData content to data
var xml = new ActiveXObject("Microsoft.XMLDOM");
xml.async = false;
// make sure to remove any invalid XML (such as )
var ctrlText = '<?xml version="1.0" encoding="UTF-16"?>' +
ctrl.innerHTML.replace(/\&nbsp\;/gi, "");
xml.loadXML(ctrlText); // Load XML Content

document.write("<br>Error Code: ")
document.write(xml.parseError.errorCode)
document.write("<br>Error Reason: ")
document.write(xml.parseError.reason)
document.write("<br>Error Line: ")
document.write(xml.parseError.line)

// load the requested transformation
var xsl = new ActiveXObject("Microsoft.XMLDOM");
xsl.async = false;
var xslFilename = '<%=Request.Params("Transform") %>';
if (xslFilename.length > 0)
{
xsl.load('<%=Request.Params("Transform") %>'); // Transformation
}
else
{
xsl.load('default.xsl');
}

// write results to output
var transformedXml = xml.transformNode(xsl);
document.write(transformedXml);
</script>
</pre>

OK. So now the problem... When using some of the .NET controls (e.g.
DataGrid) the HTML generated at runtime is not XHTML compliant therefore the
xml.loadXML(ctrlText) call gets a parse error:

Error Code: -1072896766
Error Reason: A string literal was expected, but no opening quote character
was found.
Error Line: 1

Since several of the attributes generated by the controls aren't contained
in quote characters.

Is there a way to change the behavior of the parser used by Microsoft.XMLDOM
or change the way the HTML is generated by the framework WebControls? Or is
there another way around this problem (besides writing my own pre-parser to
clean the HTML)? Thanks.



Yoga Weazel
- Eagles may soar but weasels don't get sucked into jet engines.
 
Y

yoga weazel

Additionally, there is a good bit of formatting of the data that is displayed
in the source HTML element trying to be printed therefore do not really want
to reproduce this if at all possible. Thanks again.
 
Y

yoga weazel

After more research I noticed that the problem isn't actually in the
generated HTML but in the use of the ctrl.innerHTML javascript property on
the HTML element. For some reason innerHTML seems to remove the " marks from
all attributes that do not contain whitespace. Now I need to figure out
why???
 
Y

yoga weazel

Never figured out why but this is how IE implements the innerHTML property.
Therefore there wasn't really any work around except (for my use case) to
remove all attributes from the elements prior to loading into an XML
document. IMHO, innerHTML should return the exact HTML as it has been
encoded on the page but maybe that's just me ranting :).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top