<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
<script language="JavaScript" type="text/javascript">
HTTPRequest = function ()
{
var xmlhttp=null;
try
{
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (_e)
{
try
{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (_E)
{ }
}
if (!xmlhttp && typeof XMLHttpRequest != 'undefined')
{
try
{
xmlhttp = new XMLHttpRequest();
}
catch (e)
{
xmlhttp = false;
} }
return xmlhttp;
}
function ask(url, fieldToFill, lookupField)
{
var http = new HTTPRequest();
http.open("GET", url, false);
http.onreadystatechange = function()
{
handleHttpResponse(http, fieldToFill, lookupField)
};
http.send(null);
}
function handleHttpResponse(http, fieldToFill, lookupField)
{
if (http.readyState == 4)
{
result = http.responseText;
var text;
text = result.substring(6, 0);
if(text !="沒有這個類型")
{
if ( -1 != result.search("null") )
{
lookupField.style.borderColor = "red";
fieldToFill.value = "";
}
else
{
lookupField.style.borderColor = "";
fieldToFill.value = result;
}
}
else
{
fieldToFill.value ="";
}
}
}
var codeField = null;
function codechanged()
{
codeField = document.getElementById("codecode");
var code = codeField.value;
//code.length == 2?updateBooklist(code):code.length == 5?updateBook(code):"";
if(code.length == 2)
{
updateBooklist(code);
}
else if(code.length == 5)
{
updateBook(code);
}
}
function updateBooklist(code)
{
var booklistField = document.getElementById("booklist");
ask("BookAjax.aspx?id="+code, booklistField, codeField);
}
function updateBook(code)
{
var bookField = document.getElementById("book");
ask("BookAjax.aspx?id="+code, bookField, codeField);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<table width="300">
<tr>
<td style="width: 90px">
编号:</td>
<td style="width: 187px">
<input id="codecode" type="text" οnkeyup="codechanged()" /></td>
</tr>
<tr>
<td style="width: 90px">
分类:</td>
<td style="width: 187px">
<input id="booklist" type="text" /></td>
</tr>
<tr>
<td style="width: 90px; height: 22px">
书名:</td>
<td style="width: 187px; height: 22px">
<input id="book" type="text" /></td>
</tr>
</table>
</div>
</form>
</body>
</html>
BookAjax.aspx:
protected void Page_Load(object sender, EventArgs e)
{
if (Request["id"].Length == 2)
{
SelectCity("select leiming from biao1 where bianhao ="+Request["id"]);
}
else if (Request["id"].Length == 5)
{
SelectCity("select shuming from biao2 where ListBianhao =" + Request["id"]);
}
}
public void SelectCity(string strSql)
{
SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
SqlCommand cmd = new SqlCommand(strSql);
cmd.Connection = con;
SqlDataAdapter da = new SqlDataAdapter();
DataSet ds = new DataSet();
da.SelectCommand = cmd;
con.Open();
da.Fill(ds);
if (ds.Tables[0].Rows.Count !=0)
{
Response.Write(ds.Tables[0].Rows[0][0]);
}
else
{
Response.Write("沒有這個類型");
}
con.Close();
}