CustomValidator控件的自定义验证
本人在做一个网站,在注册用户的页面使用.net的自定义控件验证输入的注册学生名是否在数据库中已存在这一功能!
使用CustomValidator服务器端验证函数方法。代码如下
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection cn = new SqlConnection();
cn.ConnectionString = "Data Source=.\\SQLEXPRESS;Initial Catalog=Xk;User ID=sa;Password=123456";
SqlCommand cmd = new SqlCommand();
cmd.Connection = cn;
cmd.CommandText = "INSERT INTO [Student] " + "([StuNO],[ClassNo],[StuName],[Pwd],[Email],[Telephone],[photourl],[StuSex])"
+"VALUES(@StuNo,@ClassNo,@StuName,@Pwd,@Email,@Telephone,@photourl,@StuSex)";
cmd.Parameters.Add("@StuNo",SqlDbType.Char,8).Value=txtStuNo.Text;
cmd.Parameters.Add("@ClassNo", SqlDbType.Char, 8).Value = dpClass.SelectedValue;
cmd.Parameters.Add("@StuName", SqlDbType.VarChar,30).Value = txtStuName.Text;
cmd.Parameters.Add("@Pwd", SqlDbType.VarChar,20).Value = txtPwd.Text;
cmd.Parameters.Add("@Email", SqlDbType.VarChar,100).Value = txtEmail.Text;
cmd.Parameters.Add("@Telephone",SqlDbType.VarChar,50).Value=txtTelephone.Text;
cmd.Parameters.Add("@photourl",SqlDbType.VarChar,200).Value=txtPhotoUrl.Text;
cmd.Parameters.Add("@StuSex", SqlDbType.Bit).Value = rbtSex.SelectedValue; ;
cn.Open();
int result = cmd.ExecuteNonQuery();
cn.Close();
String msg;
if (result > 0)
msg = "<script>alert('信息注册成功!')</script>";
else
msg = "<script>alert('信息注册失败!')</script>";
Literal lit = new Literal();
lit.Text = msg;
Page.Controls.Add(lit);
/*
if (Page.IsValid)
{
Response.Write("<script>alert('信息注册成功!')</script>");
}
else
{
Response.Write("<script>alert('信息注册失败!')</script>");
}
*/
}
protected void CustomValidator3_ServerValidate(object source, ServerValidateEventArgs args)
{
SqlConnection cn = new SqlConnection();
cn.ConnectionString = "Data Source=.\\SQLEXPRESS;Initial Catalog=Xk;User ID=sa;Password=123456";
//con.ConnectionString = "Data Source=.\\SQLEXPRESS;Initial Catalog=Xk;Integrated Security= SSPI;";
cn.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "select count(*) from [Student] where [StuName]='" + args.Value + "'";
cmd.Connection = cn;
//数据类型转换
int result = Convert.ToInt32(cmd.ExecuteScalar());
//指定控件是否通过验证
if (result > 0)
{
args.IsValid = false;
Response.Write("<script>alert('信息注册成功!')</script>");
}
else
{
args.IsValid = true;
Response.Write("<script>alert('信息注册失败!')</script>");
}
cn.Close();
/*
String msg2;
if (result > 0)
msg2 = "<script>alert('信息注册失败!')</script>";
else
msg2 = "<script>alert('信息注册成功!')</script>";
Literal lit = new Literal();
lit.Text = msg2;
Page.Controls.Add(lit);
*/
}
因为我刚接触.net1个多月,在搜索的文章看的有点吃力。
希望高手不吝赐教清晰代码,万分感谢 ! --------------------编程问答-------------------- 判断用户名是否存在:
http://www.google.com.hk/search?hl=zh-CN&inlang=zh-CN&newwindow=1&safe=strict&client=aff-360daohang&hs=bDC&affdom=360.cn&q=asp.net+%E5%88%A4%E6%96%AD%E7%94%A8%E6%88%B7%E5%90%8D%E6%98%AF%E5%90%A6%E5%AD%98%E5%9C%A8&oq=asp.net+%E5%88%A4%E6%96%AD%E7%94%A8%E6%88%B7%E5%90%8D%E6%98%AF%E5%90%A6%E5%AD%98%E5%9C%A8&aq=f&aqi=&aql=&gs_l=serp.3...295469l301329l0l301547l4l4l0l0l0l0l0l0ll0l0.llsin. --------------------编程问答--------------------
判断用户名是否存在:
http://www.cnblogs.com/beniao/archive/2008/03/29/1129141.html
里面还有源码下载
补充:.NET技术 , ASP.NET