主题: 制作用户注页面代码
//在页面加载时,声明以下变量:
if (!IsPostBack)
{
//声明变量并保存在ViewState中,作为数组的下标
ViewState["ArrayIndex"] = 0;
/*声明一个包含20个元素的字符串型数组,并保存在ViewState中,用来保存用户名*/
ViewState["Names"] = new string[20];
ViewState["Pwds"] = new string[20]; //保存密码的数组
}
//在页面类中声明属性,用来读取和设置属性的值:
public int ArrayIndex //声明属性
{
//读取ArrayIndex的值
get { return Convert.ToInt32(ViewState["ArrayIndex"]); }
//设置读取ArrayIndex的值
set { ViewState["ArrayIndex"] = value; }
}
public string[] Names
{
get { return (string[])ViewState["Names"]; }
set { ViewState["Names"] = value; }
}
public string[] Pwds
{
get { return (string[])ViewState["Pwds"]; }
set { ViewState["Pwds"] = value; }
}
//Button控件的单击事件代码:
msg_suc.Text = "";
msg_pwd.Text = "";
msg_name.Text = "";
string UName = UserName.Text;
string UPwd = Pwd.Text;
int j = 0;
int m = 0;
//验证用户名和密码是否为空,如果为空则进行提示
if (UName != "")
{
if (UPwd != "")
{
msg_name.Text = "";
//验证当前用户名在数组中是否已经存在
while (j < Names.Length)
{
if (UName == Names[j]) //用户名与数组中的元素值相同
{
m = 1;
break; //中断循环
}
j++;
}
if (m == 0)
{
if (ArrayIndex < Names.Length) //判断下标是否超出数组长度
{
msg_pwd.Text = "";
msg_name.Text = "";
Names[ArrayIndex] = UName; //给数组元素赋值
Pwds[ArrayIndex] = UPwd;
ArrayIndex = ArrayIndex + 1; //数组下标增加1
msg_suc.Text = "恭喜 <font color='#000000'>" + UName + "</font> 你已经注册成功";
}
else
{
msg_suc.Text = "本站最多注册20名会员,不能继续注册了!";
}
}
else
{
msg_suc.Text = "此用户名已经存在,请重输入新的用户名";
}
}
else
{
msg_pwd.Text = "请输入密码";
}
}
else
{
msg_name.Text = "请输入用户名";
}
show.Text = "成功注册的会员:<br>";
for (int i = 0; i < ArrayIndex; i++)
{
string str;
if ((i + 1) % 10 == 0) //判断是否需要换行
{
str = "<br>";
}
else
{
str = " ";
}
show.Text = show.Text + Names[i] + str;
}
信息来源[url]http://www.ithuhang.cn[/url]
if (!IsPostBack)
{
//声明变量并保存在ViewState中,作为数组的下标
ViewState["ArrayIndex"] = 0;
/*声明一个包含20个元素的字符串型数组,并保存在ViewState中,用来保存用户名*/
ViewState["Names"] = new string[20];
ViewState["Pwds"] = new string[20]; //保存密码的数组
}
//在页面类中声明属性,用来读取和设置属性的值:
public int ArrayIndex //声明属性
{
//读取ArrayIndex的值
get { return Convert.ToInt32(ViewState["ArrayIndex"]); }
//设置读取ArrayIndex的值
set { ViewState["ArrayIndex"] = value; }
}
public string[] Names
{
get { return (string[])ViewState["Names"]; }
set { ViewState["Names"] = value; }
}
public string[] Pwds
{
get { return (string[])ViewState["Pwds"]; }
set { ViewState["Pwds"] = value; }
}
//Button控件的单击事件代码:
msg_suc.Text = "";
msg_pwd.Text = "";
msg_name.Text = "";
string UName = UserName.Text;
string UPwd = Pwd.Text;
int j = 0;
int m = 0;
//验证用户名和密码是否为空,如果为空则进行提示
if (UName != "")
{
if (UPwd != "")
{
msg_name.Text = "";
//验证当前用户名在数组中是否已经存在
while (j < Names.Length)
{
if (UName == Names[j]) //用户名与数组中的元素值相同
{
m = 1;
break; //中断循环
}
j++;
}
if (m == 0)
{
if (ArrayIndex < Names.Length) //判断下标是否超出数组长度
{
msg_pwd.Text = "";
msg_name.Text = "";
Names[ArrayIndex] = UName; //给数组元素赋值
Pwds[ArrayIndex] = UPwd;
ArrayIndex = ArrayIndex + 1; //数组下标增加1
msg_suc.Text = "恭喜 <font color='#000000'>" + UName + "</font> 你已经注册成功";
}
else
{
msg_suc.Text = "本站最多注册20名会员,不能继续注册了!";
}
}
else
{
msg_suc.Text = "此用户名已经存在,请重输入新的用户名";
}
}
else
{
msg_pwd.Text = "请输入密码";
}
}
else
{
msg_name.Text = "请输入用户名";
}
show.Text = "成功注册的会员:<br>";
for (int i = 0; i < ArrayIndex; i++)
{
string str;
if ((i + 1) % 10 == 0) //判断是否需要换行
{
str = "<br>";
}
else
{
str = " ";
}
show.Text = show.Text + Names[i] + str;
}
信息来源[url]http://www.ithuhang.cn[/url]