problem assigning declarative values in asp:hyperlink. error: this is not scriptlet. will output as plain text

这一生的挚爱 提交于 2020-01-12 06:29:40

问题


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.


回答1:


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.




回答2:


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.




回答3:


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);
}



回答4:


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


来源:https://stackoverflow.com/questions/3441841/problem-assigning-declarative-values-in-asphyperlink-error-this-is-not-script

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