C# 在 ASP.NET LoginName 控件中显示全名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/633950/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Display Full Name in ASP.NET LoginName control
提问by Leon Tayson
I have an .aspx page using a login control with custom authentication. I was wondering if it's possible to have a "Welcome [FirstName] [LastName]" message using the LoginName control instead of the [UserName] that is accessed by default.
我有一个使用带有自定义身份验证的登录控件的 .aspx 页面。我想知道是否可以使用 LoginName 控件而不是默认访问的 [UserName] 来显示“欢迎 [FirstName] [LastName]”消息。
I'm thinking of storing these info in the Session object if it's not possible.
如果不可能,我正在考虑将这些信息存储在 Session 对象中。
Thanks!
谢谢!
回答by John Feminella
You'll need to override the RenderContents
method or make your own LoginName control. Something like this will do the trick:
您需要覆盖该RenderContents
方法或制作您自己的 LoginName 控件。像这样的事情可以解决问题:
protected override void RenderContents(HtmlTextWriter writer)
{
if (string.IsNullOrEmpty(Profile.FullName))
return;
nameToDisplay = HttpUtility.HtmlEncode(Profile.FullName);
string formatExpression = this.FormatString;
if (formatExpression .Length == 0)
{
writer.Write(nameToDisplay);
}
else
{
try
{
writer.Write(string.Format(CultureInfo.CurrentCulture, formatExpression, new object[1] { nameToDisplay });
}
catch (FormatException exception)
{
throw new FormatException("Invalid FormatString", exception1);
}
}
}
Also, see here for a brief article on working with LoginName.
另外,请参阅此处了解有关使用 LoginName的简短文章。
回答by John Saunders
First of all, see Personal names in a global application: What to store. Even if your site is limited to the US, I'm pretty sure I've seen some foreigners around here.
首先,请参阅全局应用程序中的个人名称:要存储的内容。即使您的网站仅限于美国,我也很确定我在这里看到了一些外国人。
回答by Daniel Ballinger
You could use the FormatStringproperty to set the welcome message to any string you want. When combined with expression builders (e.g. <%$ expressionPrefix: expressionValue %>) you would have a flexible way to define output.
您可以使用FormatString属性将欢迎消息设置为您想要的任何字符串。当与表达式构建器(例如<%$ expressionPrefix: expressionValue %>)结合使用时,您将有一种灵活的方式来定义输出。
回答by agiles
create a LoginName control in redirect page it may be Masterpage.aspx or any other page.
在重定向页面中创建一个 LoginName 控件,它可以是 Masterpage.aspx 或任何其他页面。
<asp:LoginName ID="LoginName1" runat="server" />
then insert these line of code inside the page_load in .cs file
然后在 .cs 文件中的 page_load 中插入这些代码行
protected void Page_Load(object sender, EventArgs e)
{
//this can come from anywhere like session, database
string fullName = "ABC XYZ";
LoginName1.FormatString = "welcome" + " - " + fullName ; //output: welcome - ABC XYZ
or
LoginName1.FormatString = fullName; // output: ABC XYZ
}
is this helpful for you???
这对你有帮助吗???