一个TextBox 输入Label的内容 一个Button验证TextBox的内容是否等于Label的内容
再一个Label2显示验证结果
if(TextBox.Text==Label1.Text)
{
Label1.Visable=fasle;
Label2.Text="正确";
}
else
{
Label2.Text="错误";
}
为什么每次结果都是 Label2显示 "错误" 呢?
private void Page_Load(object sender, System.EventArgs e)
{
Random ds = new Random();
this.Label1.Text=ds.Next(999,10000).ToString().Trim();
}
private void Button1_Click(object sender, System.EventArgs e)
{
if(this.TextBox1.Text.Trim()=this.Label1.Text.Trim())
{
this.Label2.Text="正确";
this.Label1.Visible=false;
this.sqlDataAdapter1.Fill(dataSet21,"tel");
this.DataGrid1.DataBind();
}
else
{
this.Label2.Text="错误";
}
}
就这段代码 只会有一个结果 Label2.Text只会显示 "错误" 不会显示"正确"
直接写一个随机生成验证码的函数~
private void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
string checkCode = CreateRandomCode(4);
}
}
public string CreateRandomCode(int codeCount)//随即4位验证码
{
string allChar = "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,W,X,Y,Z";
string[] allCharArray = allChar.Split(',');
string randomCode = "";
int temp = -1;
Random rand = new Random();
for (int i = 0; i < codeCount; i++)
{
if (temp != -1)
{
rand = new Random(i * temp * ((int)DateTime.Now.Ticks));
}
int t = rand.Next(35);
if (temp == t)
{
return CreateRandomCode(codeCount);
}
temp = t;
randomCode += allCharArray[t];
}
return randomCode;
}
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
Random ds = new Random();
this.Label1.Text = ds.Next(999,10000).ToString().Trim();
}
}
private void Button1_Click(object sender, System.EventArgs e)
{
if(this.TextBox1.Text.Trim() == this.Label1.Text.Trim())
{
this.Label2.Text="正确";
this.Label1.Visible=false;
this.sqlDataAdapter1.Fill(dataSet21,"tel");
this.DataGrid1.DataSource = dataSet2;
this.DataGrid1.DataBind();
}
else
{
this.Label2.Text="错误";
}
}
就这段代码 只会有一个结果 Label2.Text只会显示 "错误" 不会显示"正确"