如何设置/使用自定义验证器语法
本文关键字:自定义 验证 语法 何设置 设置 | 更新日期: 2023-09-27 18:18:51
早上好
我最近正在学习c#和。net,并尝试制作注册页面。
如果Username =' HelloWorld1' and Password ='Password'
基本上我试着测试如果用户是admin或employees我已经在数据库中硬编码了,
则该测试框将显示
然后<!--this is html code -->
<tbody id ="tbody" runat="server" visible ="false">
<tr>
<td class="auto-style1">IsEmployee</td>
<td class="auto-style1">
<asp:CheckBox ID="CheckBox1" runat="server" />
</td>
<td class="auto-style1">
</td>
</tr>
<tr>
<td>IsAdmin</td>
<td>
<asp:CheckBox ID="CheckBox2" runat="server" />
</td>
<td> </td>
</tr>
</tbody>
出现我的代码是
protected void CustomValidator3_ServerValidate(object source, ServerValidateEventArgs args)
{
String Artists = System.Configuration.ConfigurationManager.ConnectionStrings["FleetManagementConnectionString"].ConnectionString;
System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(Artists);
SqlCommand objcmd = new SqlCommand("Select * from dbo.UserLoggins where UserName ='HelloWorld1' and Password ='password'", con);
SqlDataReader objReader;
con.Open();
objReader = objcmd.ExecuteReader();
if (objReader.HasRows)
{
args.IsValid = false;
tbody.Visible = true;
args.IsValid = true;
}
con.Close();
}
我如何使这段代码工作?
非常感谢,祝你有美好的一天。
嗯,是的,有问题,但如果你只是想让你的例子工作尝试这个。假设您的自定义验证器控件已正确配置,您只需返回IsValid是true还是false。
int count = 0;
using (SqlConnection con = new System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings["FleetManagementConnectionString"].ConnectionString))
{
SqlCommand command = new SqlCommand("Select Count(*) from dbo.UserLoggins where UserName =@User and Password =@Password", con);
SqlParameter paramName = new SqlParameter("@User", SqlDbType.VarChar, 255) { Value = "HelloWorld1" };
command.Parameters.Add(paramName);
SqlParameter paramName = new SqlParameter("@Password", SqlDbType.VarChar, 255) { Value = "password" };
command.Parameters.Add(paramName);
con.Open();
count = (int)cmd.ExecuteScalar();
con.Close();
}
args.IsValid = count > 0;
return;