I am trying to concat a string inside the attribute. I get an error. I think that it is related to my Eval
. Is there a proper way to concat strings, or is this
Try this instead:
<asp:HyperLink ID="lb" runat="server" Text='<%#Eval("Key.Id") %>' NavigateUrl='ViewItem.aspx?id=<%# Eval("Key.Id") %>'/>
You don't need to concatenate
Here's what I do when I have something in a gridview like this:
<img src='<%# GetDisImageLink(Eval("Disabilities").ToString()) %>'
alt="Disabilities" />
[CS code-behind]
public string GetDisImageLink(string dis)
{
return "../../Content/Images/CardContactInfo/" +
(dis.Trim() == "Y" ? "DIS.png" : "Blank.png");
}
Short answer: NavigateUrl='<%# Eval("Key.Id", "ViewItem.aspx?id={0}") %>'
Longer explanation:
The problem in your code is that you are use data binding expression only for part of your web control attribute. You need to move everything inside the data binding expression.
First of all, a data binding expression is this:
<%# EXPRESSION %>
Basically the rule for using a data binding expression for a web control attribute is that the expression must be the only thing in the attribute:
<asp:HyperLink ID="lb" runat="server"
Text='<%# EXPRESSION %>'
NavigateUrl='<%# EXPRESSION %>' />
So your first attribute, Text
, is correct. But your second attribute, NavigateUrl
is not correct. Because you put ViewItem.aspx?id=
as the value for the attribute, leaving + '<%# Eval("Key.Id") %>'
outside any attribute but inside the control tag.
Here is the correct syntax:
<asp:HyperLink ID="lb" runat="server"
Text='<%# Eval("Key.Id") %>'
NavigateUrl='<%# Eval("Key.Id", "ViewItem.aspx?id={0}") %>'/>
Notice that we are using a format string as the second parameter for Eval()
. This is equivalent to the following, more explicit, syntax:
<asp:HyperLink ID="lb" runat="server"
Text='<%# Eval("Key.Id") %>'
NavigateUrl='<%# String.Format("ViewItem.aspx?id={0}", Eval("Key.Id")) %>'/>