我在網上Down了一個程序,想學寫無刷新的網頁增量查詢,(以前是VS2002或VS2003)結果改過來(改成VS2005)既不報錯,又沒反應,請高手幫忙!看一下什麼原因
WebForm1.aspx的源碼
<%@ Page language="c#" CodeFile="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="ajaxtest.WebForm1" %>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1">
<title>Untitled Page</title>
<script type="text/javascript">
var xmlHttp;
function createXMLHttpRequest()
{
if (window.ActiveXObject)
{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else if (window.XMLHttpRequest)
{
xmlHttp = new XMLHttpRequest();
}
}
function startRequest()
{
//debugger;
var ProvinceID=document.getElementById("DropDownList1");
createXMLHttpRequest();
xmlHttp.onreadystatechange = handleStateChange;
xmlHttp.open("GET", "?ProvinceID="+ProvinceID.value, true);
xmlHttp.send(null);
}
function handleStateChange()
{
if(xmlHttp.readyState == 4) //0(未初始化);1(正在装载);2 (装载完毕);3 (交互中);4 (完成)
{
if(xmlHttp.status == 200) //200(OK);404(not found)
{
document.getElementById("gridiv").innerHTML=xmlHttp.responseText;
}
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList>
</div>
<div id="gridiv"></div>
</form>
</body>
</html>WebForm1.aspx的源碼using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;namespace ajaxtest
{
public partial class WebForm1 : System.Web.UI.Page
{
//protected System.Web.UI.WebControls.DropDownList DropDownList1; private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if (!Page.IsPostBack)
{
this.DropDownList1.Attributes.Add("onchange", "return startRequest();");
ListProvince();
if (ProvinceID != "")
{
GetCityByProvinceID(ProvinceID);
}
} } private string ProvinceID
{
get
{
if (Request["ProvinceID"] != null && Request["ProvinceID"].ToString() != "")
{
return Request["ProvinceID"];
}
else
{
return "";
}
}
} private void GetCityByProvinceID(string ProvinceID)
{
string connStr = "server=192.168.169.132;database=test;uid=sa;pwd=";
SqlConnection conn = new SqlConnection(connStr);
string sql = "select * from city where father='" + ProvinceID + "'";
SqlCommand cmd = new SqlCommand(sql, conn);
conn.Open();
SqlDataReader dr = cmd.ExecuteReader(); string s = @"<table cellspacing='0' cellpadding='4' border='0' id='GridView1' style='color:#333333;border-collapse:collapse;'>";
s += "<tr style='color:White;background-color:#990000;font-weight:bold;'>";
s += "<th scope='col'>流水号</th><th scope='col'>代号</th><th scope='col'>城市</th></tr>";
int m = 0;
while (dr.Read())
{
if (m % 2 == 0)
{
s += "<tr style='color:#333333;background-color:#FFFBD6;'>";
}
else
{
s += "<tr style='color:#333333;background-color:White;'>";
}
m++;
s += "<td>" + dr["id"] + "</td>";
s += "<td>" + dr["cityID"] + "</td>";
s += "<td>" + dr["city"] + "</td>";
s += "</tr>";
}
s += "</table>";
dr.Close();
conn.Close();
this.Response.Write(s);
this.Response.End();
}
private DataSet GetDataSet(string sql)
{
string constring = "server=192.168.169.132;database=test;uid=sa;pwd=";
SqlDataAdapter sda = new SqlDataAdapter(sql, constring);
DataSet ds = new DataSet();
sda.Fill(ds);
return ds;
}
private void ListProvince()
{
string sql = "select * from province";
DataSet ds = GetDataSet(sql);
DropDownList1.DataSource = ds;
DropDownList1.DataTextField = "province";
DropDownList1.DataValueField = "provinceID";
DropDownList1.DataBind();
}
}
}数据库脚本:if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[province]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[province]
GOif exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[city]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[city]
GOif exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[area]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[area]
GOCREATE TABLE [dbo].[province] (
[id] [int] NOT NULL ,
[provinceID] [nvarchar] (6) COLLATE Chinese_PRC_CI_AS NULL ,
[province] [nvarchar] (40) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]
GOCREATE TABLE [dbo].[city] (
[id] [int] NOT NULL ,
[cityID] [nvarchar] (6) COLLATE Chinese_PRC_CI_AS NULL ,
[city] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[father] [nvarchar] (6) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]
GOCREATE TABLE [dbo].[area] (
[id] [int] NOT NULL ,
[areaID] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[area] [nvarchar] (60) COLLATE Chinese_PRC_CI_AS NULL ,
[father] [nvarchar] (6) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]
GO
WebForm1.aspx的源碼
<%@ Page language="c#" CodeFile="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="ajaxtest.WebForm1" %>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1">
<title>Untitled Page</title>
<script type="text/javascript">
var xmlHttp;
function createXMLHttpRequest()
{
if (window.ActiveXObject)
{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else if (window.XMLHttpRequest)
{
xmlHttp = new XMLHttpRequest();
}
}
function startRequest()
{
//debugger;
var ProvinceID=document.getElementById("DropDownList1");
createXMLHttpRequest();
xmlHttp.onreadystatechange = handleStateChange;
xmlHttp.open("GET", "?ProvinceID="+ProvinceID.value, true);
xmlHttp.send(null);
}
function handleStateChange()
{
if(xmlHttp.readyState == 4) //0(未初始化);1(正在装载);2 (装载完毕);3 (交互中);4 (完成)
{
if(xmlHttp.status == 200) //200(OK);404(not found)
{
document.getElementById("gridiv").innerHTML=xmlHttp.responseText;
}
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList>
</div>
<div id="gridiv"></div>
</form>
</body>
</html>WebForm1.aspx的源碼using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;namespace ajaxtest
{
public partial class WebForm1 : System.Web.UI.Page
{
//protected System.Web.UI.WebControls.DropDownList DropDownList1; private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if (!Page.IsPostBack)
{
this.DropDownList1.Attributes.Add("onchange", "return startRequest();");
ListProvince();
if (ProvinceID != "")
{
GetCityByProvinceID(ProvinceID);
}
} } private string ProvinceID
{
get
{
if (Request["ProvinceID"] != null && Request["ProvinceID"].ToString() != "")
{
return Request["ProvinceID"];
}
else
{
return "";
}
}
} private void GetCityByProvinceID(string ProvinceID)
{
string connStr = "server=192.168.169.132;database=test;uid=sa;pwd=";
SqlConnection conn = new SqlConnection(connStr);
string sql = "select * from city where father='" + ProvinceID + "'";
SqlCommand cmd = new SqlCommand(sql, conn);
conn.Open();
SqlDataReader dr = cmd.ExecuteReader(); string s = @"<table cellspacing='0' cellpadding='4' border='0' id='GridView1' style='color:#333333;border-collapse:collapse;'>";
s += "<tr style='color:White;background-color:#990000;font-weight:bold;'>";
s += "<th scope='col'>流水号</th><th scope='col'>代号</th><th scope='col'>城市</th></tr>";
int m = 0;
while (dr.Read())
{
if (m % 2 == 0)
{
s += "<tr style='color:#333333;background-color:#FFFBD6;'>";
}
else
{
s += "<tr style='color:#333333;background-color:White;'>";
}
m++;
s += "<td>" + dr["id"] + "</td>";
s += "<td>" + dr["cityID"] + "</td>";
s += "<td>" + dr["city"] + "</td>";
s += "</tr>";
}
s += "</table>";
dr.Close();
conn.Close();
this.Response.Write(s);
this.Response.End();
}
private DataSet GetDataSet(string sql)
{
string constring = "server=192.168.169.132;database=test;uid=sa;pwd=";
SqlDataAdapter sda = new SqlDataAdapter(sql, constring);
DataSet ds = new DataSet();
sda.Fill(ds);
return ds;
}
private void ListProvince()
{
string sql = "select * from province";
DataSet ds = GetDataSet(sql);
DropDownList1.DataSource = ds;
DropDownList1.DataTextField = "province";
DropDownList1.DataValueField = "provinceID";
DropDownList1.DataBind();
}
}
}数据库脚本:if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[province]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[province]
GOif exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[city]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[city]
GOif exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[area]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[area]
GOCREATE TABLE [dbo].[province] (
[id] [int] NOT NULL ,
[provinceID] [nvarchar] (6) COLLATE Chinese_PRC_CI_AS NULL ,
[province] [nvarchar] (40) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]
GOCREATE TABLE [dbo].[city] (
[id] [int] NOT NULL ,
[cityID] [nvarchar] (6) COLLATE Chinese_PRC_CI_AS NULL ,
[city] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[father] [nvarchar] (6) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]
GOCREATE TABLE [dbo].[area] (
[id] [int] NOT NULL ,
[areaID] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[area] [nvarchar] (60) COLLATE Chinese_PRC_CI_AS NULL ,
[father] [nvarchar] (6) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]
GO
解决方案 »
- NetShop4.0里的有些代码不明白,麻烦大家看看
- 如何让获得的系统时间加上我想得到的时间?
- sql2005 的连接本机问题~求助~
- 在同一个虚拟目录下得两个应用程序,用户登陆一个程序后,在另一个程序中,如何得登陆人员得session["usercode"]
- Session传值问题,第一次访问页面的时候值没有按程序设定传到指定页面
- 上传后的文件.ASPX文件为什么不能加载类
- 一个简单的问题,谁帮看一下,如何通过值,运行二种结果
- 请问类似GOOGLE的搜索技术在ASP。NET中怎么实现
- 悲惨!公司要我去搞Sybase售后服务,我将离开心爱的编程工作。大家说说我该怎么办?
- string类的indexOf方法参数问题紧急求助!
- 求个正则表达式--只能输入数字1并且只能输入一次小数点.
- 手动管理用户状态的后台VB代码中Me代表什么呢??
改成这样试一下:
xmlHttp.open("GET", "WebForm1.aspx?ProvinceID="+ProvinceID.value, true);xmlhttp应该再加一个
new ActiveXObject('Msxml2.XMLHTTP')},
-----------------
protected void Page_Load(object sender, System.EventArgs e)
除了这点改动,别的都是lz代码
测试无问题
{
//debugger;
var ProvinceID=document.getElementById("DropDownList1");
createXMLHttpRequest();
xmlHttp.onreadystatechange = handleStateChange;
xmlHttp.open("GET", "?ProvinceID="+ProvinceID.value, true); xmlHttp.send(null);
}改:
----------------
function startRequest()
{
//debugger;
var ProvinceID=document.getElementById("<%=DropDownList1.ClientID%>").options[document.getElementById("<%=DropDownList1.ClientID%>").selectedIndex].value
;
createXMLHttpRequest();
xmlHttp.onreadystatechange = handleStateChange;
xmlHttp.open("GET", "?ProvinceID="+ProvinceID, true);
//"?ProvinceID="+ProvinceID.value 问号前面应该还有一个响应页面的url
xmlHttp.send(null);
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="WebForm2.aspx.cs" Inherits="ajaxtest.WebForm2" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
<script type="text/javascript">
var xmlHttp;
function createXMLHttpRequest()
{
if (window.ActiveXObject)
{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else if (window.XMLHttpRequest)
{
xmlHttp = new XMLHttpRequest();
}
} function startRequest()
{
//debugger;
var ProvinceID=document.getElementById("DropDownList1");
createXMLHttpRequest();
xmlHttp.onreadystatechange = handleStateChange;
xmlHttp.open("GET", "WebForm2.aspx?ProvinceID="+ProvinceID.value, true);
xmlHttp.send(null);
} function handleStateChange()
{
if(xmlHttp.readyState == 4) //0(未初始化);1(正在装载);2 (装载完毕);3 (交互中);4 (完成)
{
if(xmlHttp.status == 200) //200(OK);404(not found)
{
document.getElementById("gridiv").innerHTML=xmlHttp.responseText;
}
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList><div id="gridiv"></div></div>
</form>
</body>
</html>WebForm2.aspx.cs的源碼using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;namespace ajaxtest
{
public partial class WebForm2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
this.DropDownList1.Attributes.Add("onchange", "return startRequest();");
ListProvince();
if (ProvinceID != "")
{
GetCityByProvinceID(ProvinceID);
}
}
}
private string ProvinceID
{
get
{
if (Request["ProvinceID"] != null && Request["ProvinceID"].ToString() != "")
{
return Request["ProvinceID"];
}
else
{
return "";
}
}
} private void GetCityByProvinceID(string ProvinceID)
{
string connStr = "server=192.168.169.132;database=test;uid=sa;pwd=wz";
SqlConnection conn = new SqlConnection(connStr);
string sql = "select * from city where father='" + ProvinceID + "'";
SqlCommand cmd = new SqlCommand(sql, conn);
conn.Open();
SqlDataReader dr = cmd.ExecuteReader(); string s = @"<table cellspacing='0' cellpadding='4' border='0' id='GridView1' style='color:#333333;border-collapse:collapse;'>";
s += "<tr style='color:White;background-color:#990000;font-weight:bold;'>";
s += "<th scope='col'>流水号</th><th scope='col'>代号</th><th scope='col'>城市</th></tr>";
int m = 0;
while (dr.Read())
{
if (m % 2 == 0)
{
s += "<tr style='color:#333333;background-color:#FFFBD6;'>";
}
else
{
s += "<tr style='color:#333333;background-color:White;'>";
}
m++;
s += "<td>" + dr["id"] + "</td>";
s += "<td>" + dr["cityID"] + "</td>";
s += "<td>" + dr["city"] + "</td>";
s += "</tr>";
}
s += "</table>";
dr.Close();
conn.Close();
this.Response.Write(s);
this.Response.End();
}
private DataSet GetDataSet(string sql)
{
string constring = "server=192.168.169.132;database=test;uid=sa;pwd=wz";
SqlDataAdapter sda = new SqlDataAdapter(sql, constring);
DataSet ds = new DataSet();
sda.Fill(ds);
return ds;
}
private void ListProvince()
{
string sql = "select * from province";
DataSet ds = GetDataSet(sql);
DropDownList1.DataSource = ds;
DropDownList1.DataTextField = "province";
DropDownList1.DataValueField = "provinceID";
DropDownList1.DataBind();
}
}
}隻要在數據庫中加數據即可
我来晚了
其实对ajax来说,在ASP.net中还是使用成熟的框架比较好一些,多浏览器的兼容性上就不用多费周折