简体   繁体   中英

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?

What' is wrong ? If I remove runat="server" , then it is OK . But I need to handle it (which is inside a gridview) from the code behind .

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

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

What' is wrong ?

The ASP.NET parser messes with the nested double quotes. I just realize it does not seem to happen with VS2012 anymore.

If I remove runat="server", then it is OK . But I need to handle it (which is inside a gridview) from the code behind .

If you have a class for your DataItem, you could just try casting :

<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)

Hope this will help,

请尝试以下代码:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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