验证注册页面中的文本框 [英] Validating Textbox in signup page
本文介绍了验证注册页面中的文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用c#验证asp.net中的文本框以检查数据库中用户名的可用性,而无需使用ajax
How to validate textbox in asp.net using c# for checking the availability of user name from database without using ajax
推荐答案
在按钮单击事件中尝试此代码.
Hi,
Try this code in button click event.
if (TextBox1.Text != null)
{
SqlConnection con = new SqlConnection("");
con.Open();
SqlDataAdapter da = new SqlDataAdapter("select * from UserTab where username='"+TextBox1 .Text +"'", con);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
Label1.Text = "UserName already Exists in database";
}
else
{
Label1.Text = "Proceed to future steps";
}
}
最好的
All the Best
您可以使用自定义验证器,例如..
You can use a custom validator something like..
<asp:TextBox ID="txtUsername" runat="server" />
<asp:CustomValidator ID="valUsernameExists" runat="server" ControlToValidate="txtUsername" ErrorMessage="Username already in use." Text="*" Display="Dynamic" OnServerValidate="valUsernameExists_ServerValidate" />
protected void valUsernameExists_ServerValidate(object source, ServerValidateEventArgs args) {
//code to check the username in database
}
您可以尝试使用此功能
Aspx页面
You try this for availability
Aspx page
<body>
<form id="form1" runat="server">
<div>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1"
ErrorMessage="*" Style="z-index: 100; left: 783px; position: absolute; top: 104px"></asp:RequiredFieldValidator>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="TextBox2"
ErrorMessage="*" Style="z-index: 101; left: 785px; position: absolute; top: 157px"></asp:RequiredFieldValidator>
<asp:Label ID="Label1" runat="server" Style="z-index: 102; left: 453px; position: absolute;
top: 97px" Text="Username:"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server" AutoPostBack="True" Style="z-index: 103;
left: 554px; position: absolute; top: 96px" TabIndex="1"></asp:TextBox>
<asp:Label ID="Label2" runat="server" Style="z-index: 104; left: 479px; position: absolute;
top: 156px" Text="Name:"></asp:Label>
<asp:TextBox ID="TextBox2" runat="server" Style="z-index: 105; left: 553px; position: absolute;
top: 155px" TabIndex="2"></asp:TextBox>
<asp:Label ID="Label3" runat="server" Style="z-index: 106; left: 842px; position: absolute;
top: 121px"></asp:Label>
<asp:Button ID="Button1" runat="server" Style="z-index: 107; left: 602px; position: absolute;
top: 219px" Text="Button" Width="54px" />
<div id="checkusername" runat="server" Visible="false">
<asp:Label ID="lblStatus" runat="server" style="z-index: 108; left: 763px; position: absolute; top: 75px"></asp:Label>
<asp:Image ID="imgstatus" runat="server" Width="20px" Height="20px" style="z-index: 109; left: 765px; position: absolute; top: 100px"/>
</div>
<asp:Image ID="Image1" runat="server" style="z-index: 111; left: 382px; position: absolute; top: 133px" ImageUrl="~/Images/LoadingImage.gif" Visible="False" />
</div>
</form>
</body>
</html>
背后的代码...
Code behind...
protected void Page_Load(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(TextBox1.Text))
{
SqlConnection con;
SqlCommand cmd;
string constr = ConfigurationManager.ConnectionStrings["SomeDataBase"].ToString();
con = new SqlConnection(constr);
cmd = new SqlCommand("Select * from Thi where Username=@Username", con);
con.Open();
cmd.Parameters.AddWithValue("@Username", TextBox1.Text);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
checkusername.Visible = true;
imgstatus.ImageUrl = "Images/NotAvailable.jpg";
Label3.Text = "UserName Already Taken";
if (Label3.Text == "UserName Already Taken")
{
TextBox1.Text = " ";
}
System.Threading.Thread.Sleep(2000);
}
else
{
checkusername.Visible = true;
imgstatus.ImageUrl = "Images/Icon_Available.gif";
Label3.Text = "UserName Available";
System.Threading.Thread.Sleep(1000);
con.Close();
}
}
else
{
checkusername.Visible = false;
}
}
希望对您有帮助...编码愉快.
I hope its help you... Happy coding.
这篇关于验证注册页面中的文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文