日期:2014-05-20  浏览次数:20442 次

ajax问题,无法获取数据
我新建了一个查询页(Select.aspx)和一个获取查询结果的页(GetSelectData.aspx)
下面是select.aspx的js代码:
JScript code

var xmlhttp;
function GetData()
{
  var time=document.getElementById("<%=TimeTB.ClientID%>").value;
  var scity=document.getElementById("<%=SCityTB.ClientID%>").value;
  var dcity=document.getElementById("<%=DCityBT.ClientID%>").value;
  if(window.ActiveXObject)
  {
   xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");//IE浏览器
  }
  else if(window.XMLHttpRequest)//非IE浏览器
  {
   xmlhttp=new XMLHttpRequest();
  }
  else
  {
    alert("创建XMLHttpRequest对象失败!");
    return;
  }
  xmlhttp.Open("GET","GetSelectData.aspx?stime="+time+"&scity="+scity+"&dcity="+dcity,false);
  xmlhttp.onreadystatechange=handlfunc;
  xmlhttp.send(null);
}

function handlfunc()//获取数据后处理函数
{
   if(xmlhttp.readystate==4)
   {
      if(xmlhttp.status==200)
      {
      var data=bytes2BSTR(xmlhttp.ResponseBody);
      var start=data.indexOf("查找结果");
      var end=data.indexOf("结束");
      var finaldata=data.substring(start+8,end);
      document.write(xmlhttp.ResponseText);
      //document.getElementById('data_div').innerHTML=data;
      //document.getElementById("data_div").innerHTML=xmlhttp.ResponseText;
      }
   }
  



GetSelectData.aspx页面的后台代码:
C# code

 protected void Page_Load(object sender, EventArgs e)
    {
        string time = "2009-08-05",scity="南昌",dcity="景德镇";
        if (!IsPostBack)
        {
            try
            {
                time = Request.QueryString["stime"].ToString();
                scity = Request.QueryString["scity"].ToString();
                dcity = Request.QueryString["dcity"].ToString();
            }
            catch (Exception ex)
            {
                Response.Redirect("ErrorPage.htm");
            }
            LoadData(time, scity, dcity);
        }
        
    }
    public void LoadData(string time,string scity,string dcity)
    {
        SqlParameter[] sp ={DBAccess.MakeParam("@stime",SqlDbType.VarChar,time),
                            DBAccess.MakeParam("@scity",SqlDbType.VarChar,scity),
                            DBAccess.MakeParam("@spoint",SqlDbType.VarChar,""),
                            DBAccess.MakeParam("@dcity",SqlDbType.VarChar,dcity),
                            DBAccess.MakeParam("@dpoint",SqlDbType.VarChar,"")
                            };
        int flag = 0;
        DataView dv = DBAccess.GetData("qiche_SelectBusInfo", sp, out flag).Tables[0].DefaultView;
        if (flag == 11)
        {
            Response.Redirect("ErrorPage.htm");
        }
        this.DataList1.DataSource = dv;
        this.DataList1.DataBind(); 
    }


现在问题,当输入查询条件时,无查询到数据,得到是GetSelectData.aspx页面没绑定数据时的东西.
还有就当用responsebody时,出现乱码,如何解决。
小弟,刚开始弄这个ajax这个东东。有很多不懂,现在是一头雾水。

------解决方案--------------------
解决方法:
在GetSelectData.aspx页面的后台重写Render方法和VerifyRenderingInServerForm方法

C# code

 protected override void Render(HtmlTextWriter writer)
        {
            HtmlTextWriter gridWriter = new HtmlTextWriter(new System.IO.StringWriter());
            DataList1.RenderControl(gridWriter);
            //给ajax返回数据
            Response.Write(gridWriter.InnerWriter.ToString());
        }
        public override void VerifyRenderingInServerForm(System.Web.UI.Control control)
        {

        }

------解决方案--------------------