Print code behind function return value in aspx page

时光总嘲笑我的痴心妄想 提交于 2019-12-13 00:46:14

问题


I'm sure this is trivial, but why isn't the Windows Authentication user printing in my ASP.NET page inline?

Code behind function:

public string GetCurrentUserWindowsLogin()
{
    string windowsLogin = Page.User.Identity.Name;
    int hasDomain = windowsLogin.IndexOf(@"\");
    if (hasDomain > 0)
    {
        windowsLogin = windowsLogin.Remove(0, hasDomain + 1);
    }
    return windowsLogin;
}

Inline code:

<div class="loginDisplay">[ <%#GetCurrentUserWindowsLogin() %> ]</div>

回答1:


The <%#... %> is used for Binding Expressions like Eval and Bind.

So if you call Page.DataBind() in page_load it should work.

Another way that should work is to use code render blocks which run normal code:

<% GetCurrentUserWindowsLogin() %>

or the <%= %> construct used for small chunks of information:

<%= GetCurrentUserWindowsLogin() %>




回答2:


Just a follow up on the above answer, the <%= is like response.write.

http://msdn.microsoft.com/en-us/library/vstudio/6dwsdcf5(v=vs.100).aspx



来源:https://stackoverflow.com/questions/11398174/print-code-behind-function-return-value-in-aspx-page

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