ajax 笔记-- 写了一个不用刷新就能实现--用户名验证的例子

 现在用ajaxpro写了一个检测用户是否已经存在的例子 

///判断用户是否已经存在
function userNameIsRepeat()
{
    
var userName = document.getElementById("txtuserName").value;
    
if(userName == "")
    
{
        alert(
"请输入用户名");
        document.getElementById(
"txtuserName").focus(); //调用焦点
        return false;
    }

    
else
    
{
        
var userIsRepeat = Register.userNameIsRepeat(userName); //这个调用Register页aspx.cs里的,通过返回值进行的
        if( userIsRepeat.value == true)
        
{
            alert(
"对不起此用户已经存在");
        }

        
else
        
{
            alert(
"恭喜您,此用户还没有被注册");
        }

    }

}


在你的Register面当中加入<input type="text" id="txtusername"/><div id="userNameIsRepeat();">检测用户</div>

下面是Register.aspx.cs里的代码.

   //在Register page_load里加入下面代码
    protected void Page_Load(object sender, EventArgs e)
    
{
        AjaxPro.Utility.RegisterTypeForAjax(
typeof(Register)); //跟具自己的写这个.
    }

    
/// <summary>
    
/// 判断用记是不是已经存在了
    
/// </summary>
    
/// <param name="userName">用户名</param>
    
/// <returns></returns>

    [AjaxPro.AjaxMethod]
    
public static bool userNameIsRepeat(string userName)
    
{
        
string executeString = "select count(*) from users where userName = '" + userName + "'";
        
return TrueOrFalse(executeString);
    }


    
/// <summary>
    
/// 返回是真(true)还是假(false)
    
/// </summary>
    
/// <param name="executeString">执行语句,或存储过程名称</param>
    
/// <returns></returns>

    private static bool TrueOrFalse(string executeString)
    
{
        
int count = int.Parse(GetCommand.ExecuteScalar(executeString)); //返回值
        if (count >= 1)
        
{
            
return true;
        }

        
else
        
{
            
return false;
        }

    }
原文地址:https://www.cnblogs.com/xiaotuni/p/2365804.html