请问如何使用ajax.dll组件 判断用户名是否存在?
下面是我的ajax的方法,不知可否实现,返回count>0提示用户名存在。问题是如何用javascrip判断这个count的值。[Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.Read)]
public int GetCount(string customerID)
{
SqlConnection conn = new SqlConnection("server=localhost;database=test;uid=sa;pwd=sasa");
SqlCommand cmd = new SqlCommand("select count(*) from Customer where CustomerID=@CustomerID", conn);
SqlParameter p1 = new SqlParameter("@CutomerID", SqlDbType.NVarChar, 100);
cmd.Parameters.Add(p1);
conn.Open();
int count = Convert.ToInt32(cmd.ExecuteScalar());
conn.Close();
return count;
}
下面是我的ajax的方法,不知可否实现,返回count>0提示用户名存在。问题是如何用javascrip判断这个count的值。[Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.Read)]
public int GetCount(string customerID)
{
SqlConnection conn = new SqlConnection("server=localhost;database=test;uid=sa;pwd=sasa");
SqlCommand cmd = new SqlCommand("select count(*) from Customer where CustomerID=@CustomerID", conn);
SqlParameter p1 = new SqlParameter("@CutomerID", SqlDbType.NVarChar, 100);
cmd.Parameters.Add(p1);
conn.Open();
int count = Convert.ToInt32(cmd.ExecuteScalar());
conn.Close();
return count;
}
函数体调用GetCount(customerID,GetCount_CallBack);//GetCount_CallBack()为回调函数,在GetCount_CallBack中用Response.value获取你的返回值。
后台方法返回的是bool类型,不过你照着做就可以了
function UserAdd()
{
var User=document.getElementById("user").value;
if(Default.IsExist(User)) //调用后台方法,如果返回true则表示该联系人已经存在
{
alert("该联系人已经存在!");
return false;
}
else
{
//没注册则...你自己的代码
}
[AjaxPro.AjaxMethod]
public bool IsExist(string linkman)
{
return MessageContext.Cuurent.IsExist(linkman);
}
关注,一直想得到这个效果的源代码
那你把这个改成bool型就可以了
public int GetCount(string customerID)
{
SqlConnection conn = new SqlConnection("server=localhost;database=test;uid=sa;pwd=sasa");
SqlCommand cmd = new SqlCommand("select count(*) from Customer where CustomerID=@CustomerID", conn);
SqlParameter p1 = new SqlParameter("@CutomerID", SqlDbType.NVarChar, 100);
cmd.Parameters.Add(p1);
conn.Open();
int count = Convert.ToInt32(cmd.ExecuteScalar());
conn.Close();
return count;
}
{
var first=document.getElementById("txtfirst");
AjaxMethod.GetCount(first.value,callback_GetCount);
}
function callback_GetCount(res)
{
var obj=document.getElementById("txtsecond");
obj.value=res.value;
}
我在button onclick事件中调用上面的函数,我输入一个用户名在另一个textbox中应该显示0或1,可是得到的却是null,怎么回事???
protected void Page_Load(object sender, EventArgs e)
{
AjaxPro.Utility.RegisterTypeForAjax(typeof(Default));//把这个page注册一下
}
[AjaxPro.AjaxMethod]
public bool IsExist(string linkman)
{
return MessageContext.Cuurent.IsExist(linkman);
}
前台调用
alert(Default.IsExist(document.getElementById("USER").value));
结果返回true或者false
曾经帮助过我,可当时就是没搞定
<script language="javascript" type="text/javascript">
function Exist()
{
var code = document.all("tbCode").value;
var result=CompanyManage_Login_Default.GetExist(code).value;
if(result<1)
{
alert("不存在此用户!");
document.all("tbCode").focus();
return false;
}
}
</script>
========================================
[Ajax.AjaxMethod()]
public int GetExist(int iCode)
{
Company_Login clMain = new Company_Login();
return clMain.Exist(iCode);
}