up vote 0 down vote favorite

I think it might have been asked before but i was unable to find the right answer, so i am asking here. i have added a data source which is working fine, i wanted a feature where i query the top n entries from the database and add it with a hyperlink. Think of it like Latest News! The markup for the hyperlink inside the ItemTemplate of DataList is this.

 <asp:HyperLink ID="HyperLink1" runat="server" 
                Text='<%# Eval("News_Id") %>' NavigateUrl="~/News.aspx?NewsId=<%#Eval("News_Id") %> " runat="server" /> </asp:HyperLink>

however i get The error as "Error Creating Control, Server tag is not well formed". It reports the error where the quotes are placed.

I know i can use datanavigateurl property but i want to write it in this way. as written in the markup above. How can i?

Upon re writing it to

NavigateUrl='~/Product.aspx?DVDID=<%#Eval("Title") %> '

i get the following as the url

http://localhost:61221/Product.aspx?DVDID=<%#Eval("Title") %>

link|flag

75% accept rate
The title of this question is definitely misleading. This is about databinding, not an SQL query per se. – Cerebrus Jul 7 '09 at 7:12

1 Answer

up vote 2 down vote accepted

try this :

<asp:HyperLink ID="HyperLink1" runat="server"
    Text='<%# Eval("News_Id") %>' 
    NavigateUrl='<%#Eval("News_Id", "~/News.aspx?NewsId={0}") %>' 
    runat="server" /> 
</asp:HyperLink>

<%# Eval() %> must be inside single quotes, otherwise it throws error.

To concatenate string in your binding tag, you can use this :

<%# "~/News.aspx?NewsId=" + Eval("News_Id").ToString() %>
link|flag
upon doing so i get the following as the hyperlink localhost:61221/Product.aspx?DVDID=<%#Eval("Title";) %> it is not processing Eval – Anirudh Goel Jul 7 '09 at 6:45
Updated my answer, try the new one please. – Canavar Jul 7 '09 at 6:47
i know that method too..i wanted to concatenate literal text and eval method. – Anirudh Goel Jul 7 '09 at 6:50
got it '<%# "~/News.aspx?NewsId=" + Convert.ToString( Eval("id"))%>' this works! Though i know it is primitive way and what you have suggested is the new one and better being datanavigateurl. Thanks nevertheless for helping. – Anirudh Goel Jul 7 '09 at 6:56
yeap I was writing my new answer :) – Canavar Jul 7 '09 at 6:57

Your Answer

get an OpenID
or
never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.