<div id="aa">
<asp:TextBox id="txtuser" runat="server" Width="136px"></asp:TextBox>
</div>
<span id="errInfo">請輸入用戶名</span>
在Reg.aspx頁面中寫javascript
定義二個變量 、三個函數
var xmlhttp=false; //放 ActiveXObject的實例
var NewsInfo=""; //放 字符串
function GetXmlHttp()
{
if(window.ActiveXObject)
{
try
{
xmlhttp=new ActiveXObject("Msxml2.XMLHTTP.3.0");
}
catch(e)
{
try
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e)
{
NewsInfo="服務器忙...";
}
}
}
return xmlhttp;
}
function CallServer()
{
GetXmlHttp();
var struser=document.getElementById("txtuser").value;
if(struser=="" || struser==null)return;
var url="CallServer.aspx?user="+escape(struser);
xmlhttp.onreadystatechange=CallBack;
xmlhttp.open("post",url,true);
xmlhttp.send();
}
function CallBack()
{
if(xmlhttp.readyState==1)
{
NewsInfo="Loading...";
}
if(xmlhttp.readyState==4)
{
var msg=xmlhttp.reponseText;
var spanid=document.getElementById("errInfo");
spanid.innerHTML=msg;
}
}
2在CallServer.aspx頁面的page_load中寫如下代碼
SqlConnection conn=new SqlConnection("server=.;database=test;uid=aa;pwd=aa");
SqlCommand cmd=new SqlCommand();
private void Page_Load(object sender, System.EventArgs e)
{
if(!this.IsPostBack)
{
this.Bind();
}
}
private void Bind()
{
string struser=Request.QueryString["userna"].ToString();
string strSql="select count(*) from tt where username='"+struser+"'";
cmd.Connection=conn;
cmd.CommandText=strSql;
conn.Open();
int asd=int.Parse(cmd.ExecuteScalar().ToString());
if(asd>0)
{
Response.Write("用戶名重複,請重新選擇.");
Response.End();
}
else
{
Response.Write("用戶名可以使用.");
Response.End();
}
}
3在Reg.aspx頁面中找到層[id="aa"]在層中寫onmouseout="callserver();"
<div id="aa" onmouseout="callserver();">
<asp:TextBox id="txtuser" runat="server" Width="136px"></asp:TextBox>
</div>