在密码字段中生成随机字母数字字符串 [英] Generate random alphanumeric string into password field

查看:43
本文介绍了在密码字段中生成随机字母数字字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在单击按钮时在密码字段中生成一个随机的字母数字字符串.

I want to generate a random alphanumeric string into password field when click a button.

<asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox>

按钮背后的代码

var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
var stringChars = new char[8];
var random = new Random();

            for (int i = 0; i < stringChars.Length; i++)
            {
                stringChars[i] = chars[random.Next(chars.Length)];
            }

var finalString = new String(stringChars);

txtPassword.Text = finalString.ToString();

这个执行没有任何错误..但在文本框中没有出现任何文本.当我使用

<asp:TextBox ID="txtPassword" runat="server"></asp:TextBox> 

这可以正常工作..

我不知道为什么我的代码不起作用..

I have no any idea why my code is not working..

请帮我解决这个问题...

Please help me to solve this problem...

推荐答案

如果你想在密码模式文本字段中添加文本,那么你需要写.

If you want to add text to password mode textfield then you need to write.

txtPassword.Attributes.Add("value", finalString.ToString());

这篇关于在密码字段中生成随机字母数字字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆