javascript解析dom(2)
目的:
通过上个帖子生成的 供应商下拉列表选择显示该供应商的产品
在DataAccess添加该方法
通过Supplierid得到products
public static DataSet getProductBySupplier(int supplierID) {
Con.Open();
SqlCommand com = new SqlCommand("select * from products where supplierid=@supplierID", Con);
SqlParameter param = new SqlParameter("@supplierID", SqlDbType.Int);
param.Value = supplierID;
com.Parameters.Add(param);
SqlDataAdapter sda = new SqlDataAdapter(com);
DataSet ds = new DataSet();
sda.Fill(ds, "products");
con.Close();
return ds;
}
一般处理文件里面加上该方法
public static DataSet getProductBySupplier(int supplierID) {
Con.Open();
SqlCommand com = new SqlCommand("select * from products where supplierid=@supplierID", Con);
SqlParameter param = new SqlParameter("@supplierID", SqlDbType.Int);
param.Value = supplierID;
com.Parameters.Add(param);
SqlDataAdapter sda = new SqlDataAdapter(com);
DataSet ds = new DataSet();
sda.Fill(ds, "products");
con.Close();
return ds;
}
把底层的datable转为XML返回
private void Xml_GetProductBySupplierID(int supplierID,HttpContext context) {
DataSet ds = DataAccess.getProductBySupplier(supplierID);
XmlTextWriter write = new XmlTextWriter(context.Response.OutputStream, System.Text.Encoding.UTF8);
write.Formatting = Formatting.Indented;
write.Indentation = 4;
write.IndentChar = ' ';
ds.WriteXml(write);
write.Flush();
context.Response.End();
write.Close();
}
前台
private void Xml_GetProductBySupplierID(int supplierID,HttpContext context) {
DataSet ds = DataAccess.getProductBySupplier(supplierID);
XmlTextWriter write = new XmlTextWriter(context.Response.OutputStream, System.Text.Encoding.UTF8);
write.Formatting = Formatting.Indented;
write.Indentation = 4;
write.IndentChar = ' ';
ds.WriteXml(write);
write.Flush();
context.Response.End();
write.Close();
}
HTML
<div>
<input type="button" value="获得供应商" onclick="getSupplier()" />
<select id="supplier" onchange="changeSelect(this)">
<option value="-1">请选择提供商</option>
</select>
//下面这个div用来生成table
<div style="border: 1px solid #FF0000" id="divTable"></div>
</div>
javascript
<div>
<input type="button" value="获得供应商" onclick="getSupplier()" />
<select id="supplier" onchange="changeSelect(this)">
<option value="-1">请选择提供商</option>
</select>
//下面这个div用来生成table
<div style="border: 1px solid #FF0000" id="divTable"></div>
</div>
j脚本
function getProducts(supplierid){
request=new XMLHttpRequest();
request.open("POST","Handler.ashx?supplierID="+supplierid,true);
request.onreadystatechange=setProducts;
request.send(null);
}
function changeSelect(option){
document.getElementById("divTable").innerHTML="";
var select=document.getElementById("selectPageCount");
getProducts(option.value);
}
function setProducts(){
if(request.readyState==4){
if(request.status==200){
var xml=request.responseText;
var doc=new ActiveXObject("Msxml2.DOMDocument");
doc.loadXML(xml);
var products=doc.documentElement.selectNodes("products");
if(products.length>0){
var oTable=document.createElement("table");
var oTbody=document.createElement("tbody");
oTable.appendChild(oTbody);
oTbody.insertRow(0);
for(var j=0; j<products[0].childNodes.length; j++){
oTbody.rows[0].insertCell(j);
oTbody.rows[0].cells[j].appendChild(document.createTextNode(products[0].childNodes[j].nodeName));
}
for(var i=0;i<products.length; i++){
oTbody.insertRow(i+1);
for(var j=0; j<products[i].childNodes.length; j++){
oTbody.rows[i+1].insertCell(j);
oTbody.rows[i+1].cells[j].appendChild(document.createTextNode(products[i].childNodes[j].text));
}
}
document.getElementById("divTable").appendChild(oTable);
}
else{
document.getElementById("divTable").innerHTML="<b>没有该供应商的产品</b>";
}
}
}
}
function getProducts(supplierid){
request=new XMLHttpRequest();
request.open("POST","Handler.ashx?supplierID="+supplierid,true);
request.onreadystatechange=setProducts;
request.send(null);
}
function changeSelect(option){
document.getElementById("divTable").innerHTML="";
var select=document.getElementById("selectPageCount");
getProducts(option.value);
}
function setProducts(){
if(request.readyState==4){
if(request.status==200){
var xml=request.responseText;
var doc=new ActiveXObject("Msxml2.DOMDocument");
doc.loadXML(xml);
var products=doc.documentElement.selectNodes("products");
if(products.length>0){
var oTable=document.createElement("table");
var oTbody=document.createElement("tbody");
oTable.appendChild(oTbody);
oTbody.insertRow(0);
for(var j=0; j<products[0].childNodes.length; j++){
oTbody.rows[0].insertCell(j);
oTbody.rows[0].cells[j].appendChild(document.createTextNode(products[0].childNodes[j].nodeName));
}
for(var i=0;i<products.length; i++){
oTbody.insertRow(i+1);
for(var j=0; j<products[i].childNodes.length; j++){
oTbody.rows[i+1].insertCell(j);
oTbody.rows[i+1].cells[j].appendChild(document.createTextNode(products[i].childNodes[j].text));
}
}
document.getElementById("divTable").appendChild(oTable);
}
else{
document.getElementById("divTable").innerHTML="<b>没有该供应商的产品</b>";
}
}
}
}
本人在长沙, 有工作可以加我QQ4658276