You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ASPX页面embed标签字符过多错误及Eval绑定写法咨询

Fixing ASPX Embed Tag Binding Issues with Google Drive Viewer

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 src attribute so the double quotes in Eval() 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&amp;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 InvoiceFilePath to URL-safe formats, which Google Drive Viewer requires to load the file correctly.
  • Absolute Path Validation: Make sure InvoiceFilePath stores a full, publicly accessible URL (e.g., https://your-server.com/invoices/inv123.pdf). If it's a relative path, use ResolveUrl() 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 07:37:22