趕快檢查一下又沒有把代碼敲錯(不可能照著敲也出錯呀!)果然,沒有敲錯!代碼邏輯上也是完全沒有問題的,為什麼會報這麼一個錯?!
最後幾經周折,比較,發現在Northwind這個數據庫Orders表中,有的記錄中有這樣的內容México D.F. 注意到這個é了麼!就是這個字符的原因導致報錯,也不知道是哪國字符,索性把é改成e,終於天下太平了!
下面附上代碼,要是遇到這方面應用的時候,這些代碼還是比較有用的!
Webform1.ASPx的<HEAD>中:
<script language="Javascript">
var obj;
function GetDataViaAJax()
{
try
{
obj=new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e1)
{
try
{
obj=new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e2)
{
obj=null;
}
}
if(obj!=null)
{
obj.onreadystatechange=ProcessResponse;
obj.open("Get","./WebForm2.ASPx?customerid="+document.getElementById("Dropdownlist1").value,true);
obj.send(null);
}
return false;
}
function ProcessResponse()
{
if(obj.readyState==4)
{
if(obj.status==200)
{
var dstRoot=obj.responseXML.documentElement;
var ddlOrders=document.getElementById("ListBox1");
for(var i=ddlOrders.length-1;i>-1;i--)
{
ddlOrders.options[i]=null;
}
var orders=dstRoot.getElementsByTagName("OrderID");
var text;
var listItem;
for(var i=0;i<orders.length;i++)
{
text=(orders[i].textContent||orders[i].innerText||orders[i].text);
listItem=new Option(text,text);
ddlOrders.options[i]=listItem;
}
}
else
{
alert(obj.status);
alert("Error Retriving Data!");
}
}
}
</script>
<form>中:
<form id="Form1" method="post" runat="server">
<FONT face="宋體">
<P>
<asp:DropDownList id="DropDownList1" runat="server" Width="136px"></ASP:DropDownList></P>
<P>
<asp:ListBox id="ListBox1" runat="server" Width="136px"></ASP:ListBox></P>
</FONT>
</form>
CODEBEHIND:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'在此處放置初始化頁的用戶代碼
If Not IsPostBack Then
Dim dst As New DataSet
Dim conn As New SqlClIEnt.SqlConnection("server=localhost;user id=sa;passWord=ycm119;database=northwind;")
; Dim dad As New SqlClIEnt.SqlDataAdapter("Select customerid,companyname from customers", conn)
dad.Fill(dst)
DropDownList1.DataSource = dst
DropDownList1.DataTextFIEld = "companyname"
DropDownList1.DataValueFIEld = "customerid"
DropDownList1.DataBind()
End If
DropDownList1.Attributes.Add("onchange", "return GetDataViaAJax()")
End Sub
Webform2.ASPx中:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'在此處放置初始化頁的用戶代碼
If Not IsPostBack Then
Dim conn As New SqlClIEnt.SqlConnection("server=localhost;user id=sa;passWord=ycm119;database=northwind;")
Dim dad As New SqlClIEnt.SqlDataAdapter("Select * from orders where customerid='" & Request.QueryString("customerid") & "'", conn)
Dim dst As New DataSet
dad.Fill(dst, "customers")
Response.Clear()
Response.ContentType = "text/XML"
Response.Write(dst.GetXML)
Response.End()
End If
End Sub