简体   繁体   English

Gridview中的锚标记没有很好地形成服务器标记

[英]The server tag is not well formed for anchor tag in Gridview

Why 为什么

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

is giving me The server tag is not well formed exception in my asp.net application? 给我The server tag is not well formed在我的asp.net应用程序中The server tag is not well formed异常?

What' is wrong ? 怎么了 ? If I remove runat="server" , then it is OK . 如果我删除runat="server" ,那就没问题。 But I need to handle it (which is inside a gridview) from the code behind . 但我需要从后面的代码处理它(在gridview中)。

Thanks a lot !! 非常感谢 !!

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. 你得到The server tag is not well formed错误,因为你在双引号中使用双引号使attribute-name="value"类型的语法变为hay-wire。 It would be parsed by ASP.NET compiler as something like 它将被ASP.NET编译器解析为类似的东西

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

TempKey would appear as separate attribute with no value etc. TempKey将显示为单独的属性,没有值等。
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). 当您删除服务器标签时,ASP.NET不会解析它的html元素语法,而是按原样发出它(它的无效html以及浏览器更宽容)。

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. 编辑上面仍然会产生有问题的html,因为href值会有未转义的双引号。 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 请尝试下面的表达式,它使用Eval重载进行格式化

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")); 
}

In my grid view i have used Linkbutton and applied following code on OnClientClick . 在我的网格视图中,我使用了Linkbutton并在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 类似地,你可以在href上使用<a>元素

What' is wrong ? 怎么了 ?

The ASP.NET parser messes with the nested double quotes. ASP.NET解析器与嵌套的双引号混淆。 I just realize it does not seem to happen with VS2012 anymore. 我只是意识到VS2012似乎不再发生了。

If I remove runat="server", then it is OK . 如果我删除runat =“server”,那就没问题。 But I need to handle it (which is inside a gridview) from the code behind . 但我需要从后面的代码处理它(在gridview中)。

If you have a class for your DataItem, you could just try casting : 如果你有一个DataItem类,你可以尝试转换:

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

Best thing to do should be to set the href in codebehind (this is a point of having a runat="server" control) 最好的办法应该是在codebehind中设置href(这是一个拥有runat =“server”控件的点)

Hope this will help, 希望这会有所帮助,

请尝试以下代码:

onclick='<%# "return jsPopup('UpdateProbAct.aspx?Table=problems&Deptid=" + Eval("department") %>' 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM