Server tag not parsed in asp:Hyperlink

你离开我真会死。 提交于 2019-12-13 02:06:30

问题


I have to admit to start that I am relatively new to ASP.NET and shamefully have not really used server tags on the client side page yet. I have a repeater on my page that iterates through the rows of a datatable and shows a hyperlink object for each item using the below tag:

<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/DiseaseInfo/Syndrome.aspx?SyndromeID=<%# Eval('SYNDROME_ID')%>&SpeciesID=<%# Eval('SPECIES_ID')%>" Text='<%# Eval("SYNDROME_NAME").ToString%>'></asp:HyperLink>

The problem that I am having is that the server doesn't render out the <%# %> tags. If I put this same link into an tag then it works just fine. I am sure that it has to do with the fact that the hyperlink is already being rendered on the server-side, but I can't figure out how to change things to get it to work correctly. Any help would be greatly appreciated.


回答1:


Reverse the order of your single / double quotes.

<asp:HyperLink runat="server" NavigateUrl='~/DiseaseInfo/Syndrome.aspx?
  SyndromeID=<%# Eval("SYNDROME_ID")%>&SpeciesID=<%# Eval("SPECIES_ID")%>'
  Text='<%# Eval("SYNDROME_NAME").ToString()%>'>
</asp:HyperLink>

This normally doesn't matter at a JavaScript / HTML level but the correct quote for C# / VB is a double quote which should be used within the Eval() method.

A slightly better approach would be to invoke a method to return this somewhat complex url:

<asp:HyperLink runat="server" NavigateUrl='<%# GetUrl() %>' />

protected string GetUrl()
{
    return string.format("Syndrome.aspx?SyndromeID={0}...", Eval("SYNDROME_ID");
}



回答2:


Here is how I ended up handling this:

Public Sub Repeater2_ItemDataBound(sender As Object, e As RepeaterItemEventArgs)
    If (e.Item.ItemType = ListItemType.Item) Or (e.Item.ItemType = ListItemType.AlternatingItem) Then
        Dim SpeciesID As String = CType(e.Item.DataItem, System.Data.DataRowView)("SPECIES_ID").ToString
        Dim DiseaseID As String = CType(e.Item.DataItem, System.Data.DataRowView)("DISEASE_ID").ToString
        Dim DiseaseName As String = CType(e.Item.DataItem, System.Data.DataRowView)("DISEASE_NAME").ToString
        Dim Hyperlink = CType(e.Item.FindControl("Hyperlink1"), HyperLink)

        Hyperlink.NavigateUrl = String.Format("~/DiseaseInfo/Disease.aspx?DiseaseID={0}&SpeciesID={1}", DiseaseID, SpeciesID)
        Hyperlink.Text = DiseaseName
    End If
End Sub


来源:https://stackoverflow.com/questions/14130417/server-tag-not-parsed-in-asphyperlink

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!