看谁能解决?????????

Eagle_ice Eagle_ice 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
问题是这样的,我尽量描述清楚,请大家看的仔细些,小弟先谢过了...
Register.aspx页面

<head runat="server">
<title>无标题页</title>
<script type="text/javascript">
var xmlHttp=false;
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e)
{
try
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e2)
{
xml=false;
}
}
if(!xmlHttp)
{
xml=new XMLHttpRequest();
}
function checkUser(userName)
{
var url="DisposeEvent.aspx?Name="+userName;
xmlHttp.open("GET",url,true);
xmlHttp.onreadystatechange=backInfo;
xmlHttp.send(null);
}
function backInfo()
{
if(xmlHttp.readyState==4)
{
if(xmlHttp.status==200)
{
alert("false");
if(xmlHttp.responseText=="true")
{
document.getElementById('message').innerHTML="对不起,此用户已存在!";
}
else
{
document.getElementById('message').innerHTML="恭喜您,此用户可注册!";
}
}
}
}
</script>
</head>
<table style="width: 500px; height: 164px">
<tr>
<td style="width: 80px">
用户名:</td>
<td style="width: 100px">
<input id="userName" type="text" onchange="checkUser(document.getElementById('userName').value)"/></td>
<td style="width: 100px">
<div id="message"></div>
</td>
</tr>
<tr>
<td style="width: 80px">
密码:</td>
<td style="width: 100px">
<input id="userPwd" type="text" /></td>
<td style="width: 100px">
</td>
</tr>
<tr>
<td colspan="3">
<input id="RegBtn" type="button" value="注册" /></td>
</tr>
</table>

处理页面:(已引入using System.Data.SqlClient)
DisposeEvent.asp.cs

public partial class DisposeEvent : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
User user = new User();
if (user.LoadUserData(Request.QueryString["Name"].ToString()))
{
Response.Write("true");
Response.End();
}
else
{
Response.Write("false");
Response.End();
}
}
}

User类中的检查用户是否存在的方法

public bool LoadUserData(string userName)
{
int Result = 0;
bool exist = false;
SqlConnection con = DB.createDB();
SqlParameter pUserName = new SqlParameter("@UserName", SqlDbType.VarChar, 100);
pUserName.Value = userName;
string SqlText = string.Format("select count(*) from Users where UserName={0}", pUserName);
SqlCommand sqlcmd = new SqlCommand(SqlText,con);
sqlcmd.Parameters.Add(pUserName);
try
{
con.Open();
Result = Convert.ToInt32(sqlcmd.ExecuteScalar());
if (Result > 0)
{
exist = true;
}
}
catch(SqlException ex)
{
throw new Exception(ex.Message,ex);
}
finally
{
con.Close();
}
return exist;
}

数据库类DB.cs

public class DB
{
public DB()
{
}
public static SqlConnection createDB()
{
string sqlcon = "server=(local);database=practice;uid=sa;pwd=;";
SqlConnection con = new SqlConnection(sqlcon);
return con;
}
}

我想实现的就是用ajax方法检查用户是否已注册,可是当我输入数据库中已存在的用户名时,还是提示“恭喜您,此用户可注册!”就是说在处理页面DisposeEvent.asp.cs中的返回值始终是false,进而造成了在调用回调函数时responseText始终是false,我设置断点跟踪,看到LoadUserData()方法中的Result始终是0,所以exist也就始终为false,即根本没执行数据库操作,但是我原先不用ajax就能用啊,怎么一到ajax就不行了呢?导致数据库操作没执行,可能是Request.QueryString["Name"].ToString()没接到值,但它是处理页面,且我只用.cs页面,不用设计页面,怎么判断啊?
...全文
评论 14
收藏 已收藏
分享
返回顶部
Eagle_ice
Ajax 52,778 成员 25,334 内容