各位我发现
自定义验证控件不好用啊
不知道为什么,我今天晚上刚做了个验证用户已经存在的登陆信息
接着又建立一个项目结果就不好用了...
代码也没有错啊,几乎全是拷贝的第一个的,关联也没有错..
郁闷啊
第一个程序代码:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace zhuce
{
/// <summary>
/// tao 的摘要说明。
/// </summary>
public class tao : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Panel Panel1;
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.CustomValidator CustomValidator1;
protected System.Web.UI.WebControls.Button Button1;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.CustomValidator1.ServerValidate += new System.Web.UI.WebControls.ServerValidateEventHandler(this.CustomValidator1_ServerValidate);
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void CustomValidator1_ServerValidate(object source, System.Web.UI.WebControls.ServerValidateEventArgs args)
{ string userName=args.Value;
SqlConnection con=new SqlConnection("server=;database=login;uid=sa;pwd=;");
con.Open();
SqlCommand cmd=new SqlCommand("select count(*) from login where userName='"+userName+"'",con);
int count=Convert.ToInt32(cmd.ExecuteScalar());
if (count>0)
{
args.IsValid=false;
}
else
{args.IsValid=true;
}
con.Close();
}
private void Button1_Click(object sender, System.EventArgs e)
{
if(this.IsValid)
{Response.Write("<script>window.alert('成功');</script>");
}
}
}
}
第二个代码是:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace zhuce
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Panel Panel2;
protected System.Web.UI.WebControls.Panel Panel3;
protected System.Web.UI.WebControls.Panel Panel4;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.RadioButton rbtAgree;
protected System.Web.UI.WebControls.RadioButton rbtNotAgree;
protected System.Web.UI.WebControls.Button Button2;
protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator1;
protected System.Web.UI.WebControls.TextBox userPwd;
protected System.Web.UI.WebControls.ValidationSummary ValidationSummary1;
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.CustomValidator CustomValidator2;
protected System.Web.UI.WebControls.Panel Panel1;
private void Page_Load(object sender, System.EventArgs e)
{
this.Panel1.Visible=true;
this.Panel2.Visible=false;
this.Panel3.Visible=false;
this.Panel4.Visible=false;
// 在此处放置用户代码以初始化页面
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Button2.Click += new System.EventHandler(this.Button2_Click);
this.CustomValidator2.ServerValidate += new System.Web.UI.WebControls.ServerValidateEventHandler(this.CustomValidator2_ServerValidate);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void Button1_Click(object sender, System.EventArgs e)
{
if(this.rbtAgree.Checked)
{
Panel1.Visible=false;
Panel2.Visible=true;
}
}
private void CustomValidator2_ServerValidate(object source, System.Web.UI.WebControls.ServerValidateEventArgs args)
{
string userName=args.Value;
SqlConnection con=new SqlConnection("server=;database=login;uid=sa;pwd=;");
con.Open();
SqlCommand cmd=new SqlCommand("select count(*) from login where userName='"+userName+"'",con);
int count=Convert.ToInt32(cmd.ExecuteScalar());
if (count>0)
{
args.IsValid=false;
}
else
{
args.IsValid=true;
}
con.Close();
}
private void Button2_Click(object sender, System.EventArgs e)
{
if(this.IsValid)
{
Panel1.Visible=false;
Panel2.Visible=false;
Panel3.Visible=true;
}
}
}
}
请大家帮忙给看看是怎么回事??