需求:当用户注册时,检查用户名是否已经被注册过了。如果注册过了,显示提示信息,否则不显示。不要刷新画面。
1.test.aspx文件中:
function checkName(access_url){
$.ajax({
type: "post",
url: access_url,
data: "name="+$('#<%= txtName.ClientID%>').val(),
error:function(result){
},
success: function(result) {
if(result=="True"){
$('#<%=lblNameMsg.ClientID%>').css('display','');
}
else{
$('#<%=lblNameMsg.ClientID%>').css('display','none');
}
}});
}
2.AjaxCheck.aspx.cs文件中:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bool blResult = false;
if (Request.Params["name"] != null)
{
blResult = CheckName();
Response.Clear();
Response.Write(!blResult);
}
}
}
private bool CheckName()
{
bool returnValue;
returnValue = true;
/*从数据库里面查询用户名是否存在*/
int UserID = loginUser.GetUserIDByLoginName(user);
// UserID已经存在
if (UserID > 0)
{
returnValue = false;
}
return returnValue;
}
3.test.aspx文件中
控件中添加属性onblur
<asp:TextBox ID="txtName" AutoCompleteType="disabled" οnblur="checkName('../AjaxCheck.aspx')" runat="server"></asp:TextBox>