index.html
<script type="text/javascript">
var xmlHttp;
//创建XMLHttpRequest对象
//目的:兼容浏览器
function createXMLHttpRequest() {
if (window.ActiveXObject) {
xmlHttp = new ActiveXObject("Microsoft.XMLHttp");
}
else if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
}
}
function StringBuilder() {
var userName = document.getElementById("User").value;
return encodeURI(encodeURI(userName)); //两次编码解决中文乱码问题
}
//当状态改变的时候
function StateChange() {
if (xmlHttp.readyState == 4 && xmlHttp.Status == 200) {
var responseDiv = document.getElementById("back");
responseDiv.innerHTML = decodeURI(xmlHttp.responseText); //解码
}
}
//发送
function SendToPage() {
createXMLHttpRequest();
var queryString = "Page_1.aspx?U=";
queryString += StringBuilder();
xmlHttp.open("GET", queryString);
xmlHttp.onreadystatechange = StateChange;
xmlHttp.send(null);
}
<body>
用户名:<input type="text" name="UserName" id="User" />
<input type="button" name="btn" value="Show" onclick="SendToPage();" /><br />
<div id="back">
</div>
</body>
IsExists.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["U"] != null)
{
Session["User"] = Request.QueryString["U"].ToString();
Response.Write(Check());
Response.End();
}
}
protected string Check()
{
string sql = string.Empty;
string res = string.Empty;
if (Session["User"] != null)
{
sql = "select count(*) from T_Person where C_Name='" + Session["User"].ToString() + "'";
}
else
{
res = "error";
return res;
}
if ((int)new SQLHelper().ExecuteScalar(sql) == 0)
{
res = "不存在该用户名!";
}
else
{
res = "已存在该用户名!";
}
return res;
}
</script>