在后面的aspx页面函数的返回值打印code [英] Print code behind function return value in aspx page
问题描述
我敢肯定,这是微不足道的,但为什么不是在我的ASP.NET页面中内嵌的Windows身份验证用户打印?
背后的功能code:
公共字符串GetCurrentUserWindowsLogin()
{
字符串windowsLogin = Page.User.Identity.Name;
INT hasDomain = windowsLogin.IndexOf(@\\);
如果(hasDomain大于0)
{
windowsLogin = windowsLogin.Remove(0,hasDomain + 1);
}
返回windowsLogin;
}
在线code:
< DIV CLASS =loginDisplay><%#GetCurrentUserWindowsLogin()%GT; < / DIV>
<格CLASS =h2_lin>解决方案
的<$ C C>和LT;%#......%&GT; $ C C>用于的绑定前pressions 的如评估
和绑定
。
所以,如果你叫的Page.DataBind()
在的Page_Load
它应该工作。
这应该工作的另一种方法是使用 code呈现块一>这正常运行code:
&LT;%GetCurrentUserWindowsLogin()%GT;
或 &LT; %=%&GT;
结构中使用的信息小块:
&LT;%= GetCurrentUserWindowsLogin()%GT;
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>
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() %>
这篇关于在后面的aspx页面函数的返回值打印code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!