ASPX页面embed标签字符过多错误及Eval绑定写法咨询
Hey there, let's work through the two snags you're hitting with your ASPX TemplateField and embed tag:
1. Resolving the "Too many characters in character literal" error
This error pops up because ASP.NET is misinterpreting your data binding expression when nested inside the src attribute's quotes. The fix boils down to two key adjustments:
- Properly nest your quotes (use single quotes for the
srcattribute so the double quotes inEval()don't break parsing) - URL-encode the file path to handle special characters (like spaces,
&, or?) that would break the Google Viewer URL structure.
2. Fixing embed functionality after closing the tag
The embed tag is a self-closing HTML element, so it should end with />—this wasn't the cause of your broken functionality. The real issue was almost certainly the unencoded file path making Google Viewer unable to parse the URL correctly.
Correct Working Code
Here's the adjusted template that fixes both problems:
<asp:TemplateField HeaderText="View files"> <ItemTemplate> <embed src='https://drive.google.com/viewerng/viewer?embedded=true&url=<%# Server.UrlEncode(Eval("InvoiceFilePath").ToString()) %>' type="application/pdf" width="100%" height="600px" /> </ItemTemplate> </asp:TemplateField>
Key Notes to Keep in Mind:
- Server.UrlEncode: This converts any special characters in your
InvoiceFilePathto URL-safe formats, which Google Drive Viewer requires to load the file correctly. - Absolute Path Validation: Make sure
InvoiceFilePathstores a full, publicly accessible URL (e.g.,https://your-server.com/invoices/inv123.pdf). If it's a relative path, useResolveUrl()to convert it first:<%# Server.UrlEncode(ResolveUrl(Eval("InvoiceFilePath").ToString())) %> - Self-closing Tag Compliance: The
/>at the end of the embed tag is HTML-standard and won't interfere with functionality once the URL is properly formatted.
If you still run into issues, double-check that the InvoiceFilePath value is valid and that the file is publicly accessible (Google Viewer can't load files behind authentication walls).
内容的提问来源于stack exchange,提问作者atc

