我在網上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

解决方案 »

  1.   

    function handleStateChange(),在这个函数里设alert断点调试下
      

  2.   

    就是什麼反應也沒有,沒報錯呀,大家可在自己的項目中測下,我在JAVASCRIPT中加了好幾個ALERT測
      

  3.   

    xmlHttp.open("GET", "?ProvinceID="+ProvinceID.value, true);
    改成这样试一下:
    xmlHttp.open("GET", "WebForm1.aspx?ProvinceID="+ProvinceID.value, true);xmlhttp应该再加一个
     new ActiveXObject('Msxml2.XMLHTTP')},
      

  4.   

    或者用绝对连接后台不用判断PostBack 返回一个值
      

  5.   

    我查了一下,ListProvince();根本沒執行,page_Load好像也沒有,真怪
      

  6.   

    private void Page_Load(object sender, System.EventArgs e)
    -----------------
    protected void Page_Load(object sender, System.EventArgs e)
    除了这点改动,别的都是lz代码
    测试无问题
      

  7.   

    如有必要,我開貼加分呀,我是第一次用XMLHTTP,希望大家能拷到自己的IIS中測下,記住是VS2005(我從VS2003修改成VS2005的),幫我一下,我有個項目急需,明天就要了呀
      

  8.   

    public partial class WebForm1 : System.Web.UI.Page,這裡本來沒有partial,報錯,我就加了
      

  9.   

    function startRequest()
    {
    //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);
    }
      

  10.   

    發現整個WebForm1.aspx.cs根本不運行似的,設了斷點一點反應都沒有.
      

  11.   

    給出修改後的原碼:(WebForm2.aspx)
    <%@ 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();
            }
           }
        }隻要在數據庫中加數據即可
      

  12.   

    哈哈
    我来晚了
    其实对ajax来说,在ASP.net中还是使用成熟的框架比较好一些,多浏览器的兼容性上就不用多费周折
      

  13.   

    我改成了TextBox後,如果刪空時,會在document.getElementById("gridiv").innerHTML=xmlHttp.responseText;這一句會出錯