2

为什么

 <a id="link" runat="server" href="javascript:downloadFile('<%#Eval("TempKey") %>')"><%#Eval("ShortFileName") %></a>

在我的 asp.net 应用程序中给了我The server tag is not well formed例外吗?

怎么了 ?如果我删除runat="server",那就没问题了。但我需要从 .

非常感谢 !!

4

4 回答 4

3

In my grid view i have used Linkbutton and applied following code on OnClientClick.

    <asp:TemplateField HeaderText="Print">
    <ItemTemplate>
     <asp:LinkButton ID="lbtn_prntmenu" runat="server" CssClass="mlinks" CommandArgument='<%#Eval("ID")%>' CommandName="Print" Text="Print" OnClientClick='<%#Eval("ID","javascript:downloadFile(\"{0}\");")%>'></asp:LinkButton>
</ItemTemplate>

</asp:TemplateField>

Similarly you can do with you <a> element on href

于 2013-01-21T10:24:36.553 回答
3

You get The server tag is not well formed error because you are using double quotes within double quotes making attribute-name="value" kind of syntax going hay-wire. It would be parsed by ASP.NET compiler as something like

href="javascript:downloadFile('<%#Eval(" TempKey ") %>')">

TempKey would appear as separate attribute with no value etc.
When you remove server tags, ASP.NET would not parse the html element syntax it but rather emit it as it is (its invalid html as well as but browsers are far more forgiving).

You should probably try it within single quotes such as

href='javascript:downloadFile("<%#Eval("TempKey") %>")'

EDIT Above would still produce problematic html as there would be un-escaped double quote in href value. So try this:

href='javascript:downloadFile(&quot;<%#Eval("TempKey") %>&quot;)'

EDIT It appears that data binding expression is not getting evaluated in above. Please try below expression which uses Eval overload for formatting

href='<%# Eval("TempKey", "javascript:downloadFile(&quot;{0}&quot;)") %>'

EDIT Yet another alternative is to use some code-behind method - for example,

href='<%# GetFileLink(Container.DataItem) %>)'

And in code-behind

protected string GetFileLink(object dataItem)
{
   return string.Format("javascript:downloadFile('{0}');", 
           DataBinder.Eval(dataItem, "TempKey")); 
}
于 2013-01-21T10:21:09.227 回答
2

怎么了 ?

ASP.NET 解析器与嵌套的双引号混淆。我只是意识到VS2012似乎不再发生这种情况了。

如果我删除 runat="server",那就没问题了。但我需要从 .

如果您的 DataItem 有一个类,则可以尝试强制转换:

<a id="link" runat="server" href="javascript:downloadFile('<%#((YourNamespace.YourClass)Container.DataItem).TempKey%>')"><%#Eval("ShortFileName") %></a>

最好的办法应该是在代码隐藏中设置 href(这是拥有 runat="server" 控件的一点)

希望这会有所帮助,

于 2013-01-21T10:41:25.410 回答
1

试试下面的代码:

onclick='<%# "return jsPopup('UpdateProbAct.aspx?Table=problems&Deptid=" + Eval("department") %>' 
于 2013-01-21T11:03:10.263 回答