我正在尝试这样做:
<asp:HyperLink NavigateUrl='<%= WebContext.RootUrl %><%= WebContext.CurrentUser.UserName %>' runat="server" Text='<%= GetProfileImage(WebContext.CurrentUser.AccountId) %>'></asp:HyperLink>
但我得到了错误:
这不是scriptlet。将输出为 纯文本。
当我将鼠标悬停在声明性陈述上时。
有什么想法吗?感谢。
答案 0 :(得分:23)
您不能使用<%= ... %>
文字来设置服务器端控件的属性。
相反,您可以使用普通(客户端)<a>
标记,如下所示:
<a href="<%= WebContext.RootUrl %><%= WebContext.CurrentUser.UserName %>"><%= GetProfileImage(WebContext.CurrentUser.AccountId) %></a>
如果GetProfileImage
未返回HTML标记,请务必将其转义。
答案 1 :(得分:18)
您可以使用数据绑定语法<%# %>
。只需确保您的超链接位于数据绑定控件(例如ListView项目模板)中,或者您从代码隐藏中明确调用控件上的DataBind()
。
答案 2 :(得分:9)
您仍然可以填充&lt; asp:HyperLink&gt;如果您提供 ID 和 runat =&#34;服务器&#34; 属性。然后,您可以从代码隐藏设置HyperLink的任何属性。
ASP代码:
<asp:HyperLink ID="myLink" runat="server"/>
代码隐藏:
public void Page_Init()
{
myLink.NavigateURL = WebContext.RootUrl + WebContext.CurrentUser.UserName;
myLink.Text = GetProfileImage(WebContext.CurrentUser.AccountId);
}
答案 3 :(得分:2)
<a href='<%= WebContext.RootUrl %><%= WebContext.CurrentUser.UserName %>'><%= GetProfileImage(WebContext.CurrentUser.AccountId) %></a>