ASP.NET 文本框失去焦点事件验证用户是否已经存在
2012-10-10 22:44
555 查看
新建一个网站,在web.config中添加代码:
<connectionStrings>
<add name="SqlConn" connectionString="server=.;database=Test;uid=sa;pwd=123"/>
</connectionStrings>
然后Default.aspx代码:
<body>
<form id="form1" runat="server">
<div>
用户名:
<asp:TextBox ID="txtUserName" runat="server" ontextchanged="txtUserName_TextChanged" AutoPostBack="true" ></asp:TextBox>
<asp:Label ID="lblMessage" runat="server" Text="用户名不能为空!" Font-Size="Small" ForeColor="Red"></asp:Label>
<br />
密码:
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:Label ID="Label2" runat="server" Text="密码长度6,由字符、数字、组成" Font-Size="Small" ForeColor="Red"></asp:Label>
</div>
</form>
</body>
接着是Default.aspx.cs
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void txtUserName_TextChanged(object sender, EventArgs e)
{
bool result = CheckUserName(txtUserName.Text.Trim());
if (result == true)
{
lblMessage.Text = "恭喜您,此用户名可以使用!";
}
if (result == false)
{
lblMessage.Text = "该用户名已存在,请更换用户名!";
}
}
public static bool CheckUserName(string username)
{
string connectionString = ConfigurationManager.ConnectionStrings["SqlConn"].ConnectionString;
SqlConnection conn = new SqlConnection(connectionString);
conn.Open();
string sql = "select * from T_User where UserName = '" + username +"'";
SqlCommand cmd = new SqlCommand(sql, conn);
try
{
int count = Convert.ToInt32(cmd.ExecuteScalar());
conn.Close();
if (count > 0)
{
return false;
}
else
{
return true;
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return false;
}
}
}
最后上图:
<connectionStrings>
<add name="SqlConn" connectionString="server=.;database=Test;uid=sa;pwd=123"/>
</connectionStrings>
然后Default.aspx代码:
<body>
<form id="form1" runat="server">
<div>
用户名:
<asp:TextBox ID="txtUserName" runat="server" ontextchanged="txtUserName_TextChanged" AutoPostBack="true" ></asp:TextBox>
<asp:Label ID="lblMessage" runat="server" Text="用户名不能为空!" Font-Size="Small" ForeColor="Red"></asp:Label>
<br />
密码:
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:Label ID="Label2" runat="server" Text="密码长度6,由字符、数字、组成" Font-Size="Small" ForeColor="Red"></asp:Label>
</div>
</form>
</body>
接着是Default.aspx.cs
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void txtUserName_TextChanged(object sender, EventArgs e)
{
bool result = CheckUserName(txtUserName.Text.Trim());
if (result == true)
{
lblMessage.Text = "恭喜您,此用户名可以使用!";
}
if (result == false)
{
lblMessage.Text = "该用户名已存在,请更换用户名!";
}
}
public static bool CheckUserName(string username)
{
string connectionString = ConfigurationManager.ConnectionStrings["SqlConn"].ConnectionString;
SqlConnection conn = new SqlConnection(connectionString);
conn.Open();
string sql = "select * from T_User where UserName = '" + username +"'";
SqlCommand cmd = new SqlCommand(sql, conn);
try
{
int count = Convert.ToInt32(cmd.ExecuteScalar());
conn.Close();
if (count > 0)
{
return false;
}
else
{
return true;
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return false;
}
}
}
最后上图:
相关文章推荐
- Asp.Net文本框失去焦点检查用户名是否存在
- asp.net JQuery Ajax 结合 WebService 自动匹配 用户是否存在验证
- asp.net无刷新验证用户名是否已经存在
- Asp.net 2.0 Ajax 验证用户是否存在
- asp.net验证用户名是否已经存在
- asp.net验证用户名是否已经存在
- asp.net +ajax 验证用户是否存在
- 验证标题是否存在(TextBox控件失去焦点验证)
- asp.net AJAX 验证用户名是否存在 -Jquery(转)
- 关于ASP.net TextBox控件的失去焦点后触发其它事件
- asp.net ajax验证用户名是否存在
- asp.net AJAX 验证用户名是否存在 -Jquery
- 关于ASP.net TextBox控件的失去焦点后触发其它事件
- asp.net AJAX 验证用户名是否存在 -Jquery
- AJAX无刷验证用户是否已经存在
- asp.net AJAX 验证用户名是否存在 -Jquery
- ASP.NET判断TextBox是否获得焦点事件
- ASP.NET 应用程序级 验证用户是否登录 一般处理程序
- asp.net ajax+jquery 验证用户名是否存在
- 如何判断注册用户是否已经存在(membership验证)