19

I am trying to do this:

<asp:HyperLink NavigateUrl='<%= WebContext.RootUrl %><%= WebContext.CurrentUser.UserName %>' runat="server" Text='<%= GetProfileImage(WebContext.CurrentUser.AccountId) %>'></asp:HyperLink> 

But am getting the error:

this is not scriptlet. will output as plain text.

when I mouse over my declarative statements.

Any ideas? Thanks.

Peter
  • 5,251
  • 16
  • 63
  • 98

4 Answers4

23

You cannot use <%= ... %> literals to set properties of server-side controls.

Instead, you can use a normal (client-side) <a> tag, like this:

<a href="<%= WebContext.RootUrl %><%= WebContext.CurrentUser.UserName %>"><%= GetProfileImage(WebContext.CurrentUser.AccountId) %></a>

If GetProfileImage doesn't return HTML tags, make sure to escape it.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
18

You can use data binding syntax <%# %>. Just be sure that your hyperlink is either in a databound control, such as a ListView item template, or that you explicitly call DataBind() on the control from code-behind.

kbrimington
  • 25,142
  • 5
  • 62
  • 74
  • 2
    just so i am clear, the "#" means databound and the "=" means server-side value but not necessarily databound? – Peter Aug 09 '10 at 16:57
  • 1
    That's about right. '<%= %>' denotes a string literal, and is unrelated to databinding. – kbrimington Nov 03 '10 at 13:52
9

You can still populate an <asp:HyperLink> if you provide the ID and runat="server" properties. You can then set any property of the HyperLink from code-behind.

ASP Code:

<asp:HyperLink ID="myLink" runat="server"/>

Code-behind:

public void Page_Init()
{
    myLink.NavigateURL = WebContext.RootUrl + WebContext.CurrentUser.UserName;
    myLink.Text = GetProfileImage(WebContext.CurrentUser.AccountId);
}
donperk
  • 91
  • 1
  • 2
2
<a href='<%= WebContext.RootUrl %><%= WebContext.CurrentUser.UserName %>'><%= GetProfileImage(WebContext.CurrentUser.AccountId) %></a>
Eton B.
  • 6,121
  • 5
  • 31
  • 43