【问题标题】:Parser error when trying to Eval()尝试 Eval() 时出现解析器错误
【发布时间】:2014-09-26 20:23:31
【问题描述】:

我正在像这样绑定到 ListView:

                var authors =  (from a in db.Books
                              where a.Author.StartsWith(letter)
                              group a by a.Author into g
                              select new{Author=g.Key}).ToList();
                ListView1.DataSource = authors;
                ListView1.DataBind();

当用户单击项目时,我需要使用一些查询字符串将他重定向到另一个页面。这是我的 ItemTemplate:

<ItemTemplate>
    <td runat="server" onclick="window.location.href = 'CatalogAutor.aspx?text=<%#Eval("Author") %>';">
       <asp:Label ID="AutorLabel" runat="server" Text='<%# Eval("Author") %>' />
    </td>
</ItemTemplate>

asp:Label 中的Eval("Author") 运行良好,但为什么我在 onclick 事件中的 Eval 上收到“解析器错误:服务器标记格式不正确”? 有没有其他方法可以从点击的表格单元格中获取作者姓名?

【问题讨论】:

  • 请注意,&lt;td&gt; 不像 &lt;asp:Label&gt; 那样是 ASP.NET Web 控件。我强烈怀疑这就是问题所在。请注意,我已经很久没有使用网络表单了。

标签: javascript c# asp.net


【解决方案1】:

你真的不需要 td 标签上的 runat=server,它无论如何都会对值进行数据绑定

所以这应该可以工作:

<ItemTemplate>
      <td onclick="window.location.href = 'CatalogAutor.aspx?text=<%#Eval("Author") %>'">
             <asp:Label ID="AutorLabel" runat="server" Text='<%# Eval("Author") %>' />
       </td>
</ItemTemplate>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-02
    • 2022-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-16
    • 2012-11-23
    • 2022-01-04
    相关资源
    最近更新 更多