简体   繁体   English

如何在分配其.Te​​xt属性后显示ASP.net密码文本框控件的字符?

[英]How can I show characters for an ASP.net password textbox control after assigning its .Text property?

I am assigning a value to .Text of a textbox retrieved from a database in Page_Load() but no characters are shown in the textbox after doing this. 我正在为从Page_Load()中的数据库检索的文本框的.Text赋值。但是在执行此操作后,文本框中没有显示任何字符。 The value is indeed there when I try to read from it after the assignment. 当我在分配后尝试从中读取时,该值确实存在。

I would like to present some characters to the user even though they are the "dotted" password characters so they know a password has been entered here. 我想向用户呈现一些字符,即使它们是“点缀”的密码字符,因此他们知道在这里输入了密码。 Is it possible to do this? 是否有可能做到这一点?

TextBox myTextBox = (TextBox)ControlTextBox;
string pwVal = myTextBox.Text;
myTextBox.Attributes.Add("value", "*******");

Simply put, get a reference to your Textbox, save the password to a value elsewhere in your code, and set the value attribute (since the controls are rendered as an HTML form element anyway) to whatever you want. 简而言之,获取对文本框的引用,将密码保存到代码中的其他位置,并将值属性(因为控件呈现为HTML表单元素)设置为您想要的任何内容。

You could use JavaScript to do this on the client side once the page loads. 一旦页面加载,您可以使用JavaScript在客户端执行此操作。 However, I'm not so sure you'd want to do this... What kind of page is this, a profile page or something? 但是,我不太确定你想要这样做......这是什么类型的页面,个人资料页面还是什么? Maybe you can change it to say "Change Password" or something like that. 也许你可以改变它来说“更改密码”或类似的东西。 Otherwise, you'd have to double check to make sure the text entered there isn't the text you prefilled in automatically, etc. and it could lead to some problems. 否则,您必须仔细检查以确保输入的文本不是您自动预填的文本等,这可能会导致一些问题。

You can't do that with the Textbox in Password TextMode. 您无法使用密码TextMode中的文本框执行此操作。

If this is a ready only field, use the default TextMode, and replace all the characters except the last few with asterisks. 如果这是一个就绪字段,请使用默认的TextMode,并用星号替换除最后几个字符之外的所有字符。

If not, display the last few characters in a label next to the password textbox. 如果没有,则在密码文本框旁边的标签中显示最后几个字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM