使用xmlHttp对象实现无刷新操作
html
Code
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
<script language="javascript" src="js/Demo.js" type="text/javascript"></script>
<script language="javascript" type="text/javascript">
var xmlHttp;
function GetData()
{
//debugger();
xmlHttp = GetXmlHttp();
if(xmlHttp !=null)
{
var item = document.getElementById("cID").value;
var url ="default.aspx?ID="+item;
xmlHttp.onreadystatechange = GetServerData;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}
else
{
window.alert("no support Ajax");
return;
}
}
function GetServerData()
{
if(xmlHttp.readyState == 4)
{
document.getElementById("txtHint").innerHTML = xmlHttp.responseText;
//window.alert();
}
}
</script>
</head>
<body>
<div id="txtHint"><b>Customer info will be listed here.</b></div>
<input type="text" id="cID" onchange="GetData();" />
<input id="Button1" type="button" value="button" onclick="GetData();" />
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
<script language="javascript" src="js/Demo.js" type="text/javascript"></script>
<script language="javascript" type="text/javascript">
var xmlHttp;
function GetData()
{
//debugger();
xmlHttp = GetXmlHttp();
if(xmlHttp !=null)
{
var item = document.getElementById("cID").value;
var url ="default.aspx?ID="+item;
xmlHttp.onreadystatechange = GetServerData;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}
else
{
window.alert("no support Ajax");
return;
}
}
function GetServerData()
{
if(xmlHttp.readyState == 4)
{
document.getElementById("txtHint").innerHTML = xmlHttp.responseText;
//window.alert();
}
}
</script>
</head>
<body>
<div id="txtHint"><b>Customer info will be listed here.</b></div>
<input type="text" id="cID" onchange="GetData();" />
<input id="Button1" type="button" value="button" onclick="GetData();" />
</body>
</html>
Code
function GetXmlHttp()
{
var xmlHttp=null;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch(e)
{
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}
function GetXmlHttp()
{
var xmlHttp=null;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch(e)
{
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}
后台
Code
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["ID"] != null)
{
bindData(Request.QueryString["ID"]);
}
}
public void bindData(string ID)
{
try
{
string connetionString = "server=192.168.1.6;database=vp_prod_080710;uid=VP;pwd=clochase";
SqlConnection con = new SqlConnection(connetionString);
string sql = "select top 10 * from tblcustomer where customerID like '%" + long.Parse(ID)+"%'";
SqlCommand cmd = new SqlCommand(sql, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count != 0)
{
Response.Write("<table border='1px;'>");
for (int i = 0; i < dt.Rows.Count; i++)
{
Response.Write(string.Format(@"<tr>
<td>{0}</td>
<td>{1}</td>
</tr>", dt.Rows[i]["customerID"], dt.Rows[i]["userName"]));
}
Response.Write("</table>");
Response.End();
}
else
{
}
}
catch
{
Response.Write(@"<xml><name>sssss</name></xml>");
Response.End();
}
}
}
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["ID"] != null)
{
bindData(Request.QueryString["ID"]);
}
}
public void bindData(string ID)
{
try
{
string connetionString = "server=192.168.1.6;database=vp_prod_080710;uid=VP;pwd=clochase";
SqlConnection con = new SqlConnection(connetionString);
string sql = "select top 10 * from tblcustomer where customerID like '%" + long.Parse(ID)+"%'";
SqlCommand cmd = new SqlCommand(sql, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count != 0)
{
Response.Write("<table border='1px;'>");
for (int i = 0; i < dt.Rows.Count; i++)
{
Response.Write(string.Format(@"<tr>
<td>{0}</td>
<td>{1}</td>
</tr>", dt.Rows[i]["customerID"], dt.Rows[i]["userName"]));
}
Response.Write("</table>");
Response.End();
}
else
{
}
}
catch
{
Response.Write(@"<xml><name>sssss</name></xml>");
Response.End();
}
}
}