接触AJAX

 

1 None.gif < onClick ="getData(this,'A')" > 测试 </ a >


在应用页使用以下javaScript,用一个ID为DivContent的层显示结果

ContractedBlock.gif ExpandedBlockStart.gif 构建XML内容(从数据库读取记录)
 1None.gif        DataTable dt = new DataTable();
 2None.gif
 3None.gif        protected void Page_Load(object sender, EventArgs e)
 4ExpandedBlockStart.gifContractedBlock.gif        dot.gif{
 5InBlock.gif            string xml = string.Empty;
 6InBlock.gif            xml = "<?xml version='1.0' encoding='UTF-8' ?><xml><Author>";
 7InBlock.gif            if (this.Request["p"!= null)
 8ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 9InBlock.gif
10InBlock.gif
11InBlock.gif                string pinyin = Request["p"].ToString();
12InBlock.gif
13InBlock.gif
14InBlock.gif
15InBlock.gif
16InBlock.gif                dt = Kuqu.Applications.Resources.Resources.Resource_GetRecommendSingerOrderByPinYinTopNum(27, pinyin).Tables[0];
17InBlock.gif
18InBlock.gif                if (dt.Rows.Count > 0)
19ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
20InBlock.gif                    for (int i = 0; i < dt.Rows.Count; i++)
21ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
22InBlock.gif                        xml += @"<Table>";
23InBlock.gif                        xml += @"<AuthorName AuthorId='" + dt.Rows[i]["AuthorId"+ "'>" + dt.Rows[i]["AuthorName"+ "</AuthorName>";
24InBlock.gif                        //xml += @"<AuthorId>" + dt.Rows[i]["AuthorId"] + "</AuthorId>";
25InBlock.gif                        xml += @"</Table>";
26ExpandedSubBlockEnd.gif                    }

27ExpandedSubBlockEnd.gif                }

28InBlock.gif
29InBlock.gif
30ExpandedSubBlockEnd.gif            }

31InBlock.gif            xml += "</Author></xml>";
32InBlock.gif            Response.ContentType = "text/xml";
33InBlock.gif            Response.Write(xml);
34InBlock.gif            Response.End();
35ExpandedBlockEnd.gif        }

GETSingerByPinyin.aspx内容(构建XML内容)
ContractedBlock.gif ExpandedBlockStart.gif 在应用页调用JAVASCRIPT
 1None.gif<script type="text/javascript">
 2None.gifgetData(document.getElementById("spanA"),'A');
 3None.gifvar theObj;
 4None.gif        function getData(myObj,pinyin)
 5ExpandedBlockStart.gifContractedBlock.gif        dot.gif{          
 6ExpandedSubBlockStart.gifContractedSubBlock.gif            if(theObj!=null)dot.gif{theObj.style.backgroundColor="white";}
 7InBlock.gif            myObj.style.backgroundColor="lightblue";
 8InBlock.gif            theObj=myObj;
 9InBlock.gif            var divContent= document.getElementById("divContent");
10InBlock.gif
11InBlock.gif            divContent.innerHTML="";
12InBlock.gif            var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
13InBlock.gif
14InBlock.gif            // 异步调用 
15InBlock.gif            xmlhttp.onreadystatechange = function()
16ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
17InBlock.gif                if (xmlhttp.readyState == 4)    // 调用完毕
18ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
19InBlock.gif                    if (xmlhttp.status == 200)    // 加载成功
20ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
21InBlock.gif                        
22InBlock.gif                        var xmldoc = xmlhttp.responseXML;
23InBlock.gif                        var root = xmldoc.documentElement;
24InBlock.gif                         
25InBlock.gif                       //var objNodesname = xmldoc.getElementsByTagName("Author"); 
26InBlock.gif                        var objNodesid = xmldoc.getElementsByTagName("AuthorName");
27InBlock.gif 
28InBlock.gif                        var theNodename;
29InBlock.gif                        var theNodeid;
30InBlock.gif                        
31InBlock.gif                        for(var i=0;i<objNodesid.length;i++)
32ExpandedSubBlockStart.gifContractedSubBlock.gif                      dot.gif{
33InBlock.gif                          var theNode=objNodesid.item(i);
34InBlock.gif                          theNodename  =theNode.text;   
35InBlock.gif                          var theId= theNode.getAttribute("AuthorId");     
36InBlock.gif                                                       
37InBlock.gif                          divContent.innerHTML+='<A href="' + theId + '" style="margin:5px;">+ theNodename + '</A>&nbsp;&nbsp;';
38InBlock.gif                                        
39InBlock.gif                            
40ExpandedSubBlockEnd.gif                      }

41InBlock.gif
42ExpandedSubBlockEnd.gif                    }

43InBlock.gif                    else
44ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
45InBlock.gif                    alert("no");
46ExpandedSubBlockEnd.gif                    }

47ExpandedSubBlockEnd.gif                }

48ExpandedSubBlockEnd.gif            }
        
49InBlock.gif
50InBlock.gif            var url =  "GetSingerByPinyin.aspx?p=" + pinyin ;
51InBlock.gif             
52InBlock.gif            xmlhttp.open("get", url, true);
53InBlock.gif            xmlhttp.send();
54ExpandedBlockEnd.gif        }

55None.gif  </script>

转载于:https://www.cnblogs.com/cnzhouhai/archive/2007/07/30/835849.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值