實現功能:文字框失去焦點時,帶出其他相關資訊。
HTML:
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>未命名頁面</title>
<script type ="text/javascript" language ="javascript" >...
var XmlHttp=false;
function AjaxSet()
...{
if (window.XMLHttpRequest)
...{
XmlHttp = new XMLHttpRequest;
}
else
...{
try
...{
XmlHttp= new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e)
...{
XmlHttp= new ActiveXObject("Msxml2.XMLHTTP");
}
}
}
function AjaxSendServer()
...{
AjaxSet();
var UserID=document.getElementById("txtUserID").value;
var url="DAL.aspx?id="+UserID;
XmlHttp.onreadystatechange=updatepage;
XmlHttp.open("GET",url,true);
XmlHttp.send(null);
}
function updatepage()
...{
if(XmlHttp.readyState==4)
...{
if(XmlHttp.status==200)
...{
var reponse=XmlHttp.responseText.split(',');
document.getElementById("txtName").value=reponse[0];
document.getElementById("txtDepart").value=reponse[1];
XmlHttp=false;
}
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type ="text" id="txtUserID" runat="server" onfocusout ="AjaxSendServer()" />
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
<asp:TextBox ID="txtDepart" runat="server"></asp:TextBox>
<asp:GridView ID="gvTest" runat="server" Width="483px" ></asp:GridView>
</div>
</form>
</body>
</html>
其中資料處理URL: DAL.aspx
protected void Page_Load(object sender, EventArgs e)
...{
if (Request.QueryString[0].ToString() == "F5400171")
...{
Service.WebService cDAL = new WebService();
string sql = "select username,departno from userinfor where userid='"+Request.QueryString[0].ToString()+"'";
string s =cDAL.GetStr(sql);
// string s = "jiaxin,MIS";
Response.Write(s);
Response.End();
}
}
其中的資料訪問採用webservice實現,這裡不在累述~