码农奇迹
专注、专业、无畏的前行

里最重要的是:要把用户名:后面那个Textbox的AutoPostBack设置为True;.

第一步:

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//用户名文本框获得焦点 
ScriptManager1.SetFocus(txtUser);

}

第二步:当你输入用户名时:看看数据库里是否存在这个用户。

定义一个方法:

 

private bool CheckUser()
{

SqlConnection tt 
= new SqlConnection(ConfigurationManager.ConnectionStrings["liuyanbanConnectionString"].ToString());
 tt.Open();
 SqlCommand aa 
= new SqlCommand("select count(*) from yonghubiao where userName='" + this.txtUser .Text + "'", tt);
 
int w = Convert.ToInt32(aa.ExecuteScalar());
 
if (w == 0

return true

else 
return false
}

 

当TextBox的值变换时。执行以下

protected void txtUser_TextChanged(object sender, EventArgs e)
{
if (CheckUser())
{
lblInfo.Text = "用户名可以使用";
btnSave.Enabled = true;
//如果用ASP.NET AJAX时,使文本框获得焦点的方法(经试验txtPassword.Focus()方法或者ScriptManager.RegisterStartup()方法都无效)
ScriptManager1.SetFocus(txtPassword);
}
else
{
lblInfo.Text = "用户名已存在";
//ScriptManager1.SetFocus(txtUser);
//用户名存在则选中文本框内用户名方便修改
ScriptManager.RegisterStartupScript(this.UpdatePanel1, UpdatePanel1.GetType(), "", "document.getElementById('txtUser').select();", true);
}

}

第三步:点击注册按钮。

protected void btnSave_Click(object sender, EventArgs e)
{
if (!CheckUser())
{
lblInfo.Text = "用户名已存在";
//ScriptManager1.SetFocus(txtUser);
//用户名存在则选中文本框内用户名方便修改
ScriptManager.RegisterStartupScript(this.UpdatePanel1, UpdatePanel1.GetType(), "", "document.getElementById('txtUser').select();", true);
return;
}
lblInfo.Text = "注册成功";
}

方法二:

<script language="javascript" type="text/javascript">
function checkcommun() {
PageMethods.CheckCommunCode(document.getElementById("txtChargeCode").value, OnComplete);


}
function OnComplete(result) {
if (result != true) {
alert('该小区编码己经存在,请重新填写');
document.getElementById("txtChargeCode").value = '';
document.getElementById("txtChargeCode").focus();
}
}

</script>

<asp:TextBox ID="txtChargeCode" runat="server" MaxLength="10"
onblur="CheckLentgh(this) checkcommun()" ></asp:TextBox>

/// <summary>
/// 收费标准代码检测
/// </summary>
/// <param name="communcode">收费标准代码</param>
/// <returns></returns>
[System.Web.Services.WebMethod]
public static bool CheckCommunCode(string ChargeCode)
{

BLL.Pub_ChargeStand bll = new BeidouWY.BLL.Pub_ChargeStand();
DataSet ds = bll.GetList("ChargeCode='" + ChargeCode.Trim() + "'");
if (ds.Tables[0].Rows.Count != 0)
{
return false;
}
else
{
return true;
}


}

来自: http://hi.baidu.com/hahaw666666/blog/item/78d9d12b4be206f7e7cd40d0.html
 
posted on 2011-04-15 11:08  @编程浪子@  阅读(621)  评论(0编辑  收藏  举报