web service 实现无刷新返回一个表

Run_Table.asmx 

None.gif using  System;
None.gif
using  System.Collections;
None.gif
using  System.ComponentModel;
None.gif
using  System.Data;
None.gif
using  System.Diagnostics;
None.gif
using  System.Web;
None.gif
using  System.Web.Services;
None.gif
using  System.Data .SqlClient ;
None.gif
None.gif
namespace  WebService_Demo
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// Run_Table 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class Run_Table : System.Web.Services.WebService
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public Run_Table()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//CODEGEN: 该调用是 ASP.NET Web 服务设计器所必需的
InBlock.gif
            InitializeComponent();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private System.Data.SqlClient.SqlConnection sqlConnection1;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
组件设计器生成的代码#region 组件设计器生成的代码
InBlock.gif        
InBlock.gif        
//Web 服务设计器所必需的
InBlock.gif
        private IContainer components = null;
InBlock.gif                
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
InBlock.gif        
/// 此方法的内容。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private void InitializeComponent()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
InBlock.gif            
// 
InBlock.gif            
// sqlConnection1
InBlock.gif            
// 
InBlock.gif
            this.sqlConnection1.ConnectionString = "workstation id=GW;packet size=4096;user id=sa;data source=GW;persist security inf" +
InBlock.gif                
"o=True;initial catalog=pubs;password=";
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 清理所有正在使用的资源。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        protected override void Dispose( bool disposing )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if(disposing && components != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                components.Dispose();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
base.Dispose(disposing);        
ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
InBlock.gif        
// WEB 服务示例
InBlock.gif        
// HelloWorld() 示例服务返回字符串 Hello World
InBlock.gif        
// 若要生成,请取消注释下列行,然后保存并生成项目
InBlock.gif        
// 若要测试此 Web 服务,请按 F5 键
InBlock.gif

InBlock.gif
InBlock.gif          [WebMethod]
InBlock.gif        
public string doSearch(string  id)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//SqlConnection cn=new SqlConnection(ConfigurationSettings.AppSettings["pubs"]);
InBlock.gif
            SqlDataAdapter da=new SqlDataAdapter("select ord_date,ord_num from sales where stor_id=@stor_id",sqlConnection1);
InBlock.gif              
InBlock.gif              
InBlock.gif              
ExpandedSubBlockStart.gifContractedSubBlock.gif              
string[] s=dot.gif{"7067","7131","7896","8042"};
InBlock.gif
InBlock.gif              Random Rand
=new Random ();
InBlock.gif              
int i= Rand.Next(0,3);
InBlock.gif                  
InBlock.gif               
string temp=s[i];
InBlock.gif                                
InBlock.gif               
InBlock.gif             
InBlock.gif            da.SelectCommand.Parameters.Add(
new SqlParameter("@stor_id",temp));
InBlock.gif            DataSet ds
=new DataSet();
InBlock.gif            da.Fill(ds);
InBlock.gif            
return ds.GetXml();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

WebForm1.aspx

ExpandedBlockStart.gif ContractedBlock.gif <% dot.gif @ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="WebService_Demo.WebForm1"  %>
None.gif
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"  >
None.gif
< HTML >
None.gif    
< HEAD >
None.gif        
< title > WebForm1 </ title >
None.gif        
< meta  name ="GENERATOR"  Content ="Microsoft Visual Studio .NET 7.1" >
None.gif        
< meta  name ="CODE_LANGUAGE"  Content ="C#" >
None.gif        
< meta  name ="vs_defaultClientScript"  content ="JavaScript" >
None.gif        
< meta  name ="vs_targetSchema"  content ="http://schemas.microsoft.com/intellisense/ie5" >
ExpandedBlockStart.gifContractedBlock.gif        
< script > dot.gif  
InBlock.gif        
function Init()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            get_data()
InBlock.gif          setInterval(
"get_data()",5000);
ExpandedSubBlockEnd.gif        }

InBlock.gif    
function get_data()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      div1.innerHtml 
="";
InBlock.gif            docSubmit 
= new ActiveXObject("MSXML2.DOMDocument");
InBlock.gif            docSubmit.async 
= false;
InBlock.gif            
InBlock.gif             s
="7131";
InBlock.gif            docSubmit.load(
"http://localhost/WebService_Demo/Run_Table.asmx/doSearch?id="+s);
InBlock.gif            
InBlock.gif            docSubmit.loadXML(docSubmit.xml.replace(
/&lt;/g,"<").replace(/&gt;/g,">"));
InBlock.gif            
InBlock.gif            
//
InBlock.gif
            var s;
InBlock.gif            s
="<table borderColor=\"blue \" cellSpacing=\"1\" cellPadding=\"1\" border=\"1\"  > <tr><td>标题1</td><td>标题2</td></tr>";
InBlock.gif            nodeList
=docSubmit.documentElement.getElementsByTagName("Table");
InBlock.gif            
for (i=0;i<nodeList.length;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif            
InBlock.gif             s
=s+"<tr><td>"+nodeList(i).selectSingleNode("ord_num").text+"</td>"+"<td>" +nodeList(i).selectSingleNode("ord_date").text +"</td></tr>"
InBlock.gif            
ExpandedSubBlockEnd.gif            }

InBlock.gif            s
+"</table>"
InBlock.gif            div1.innerHTML
=s;
InBlock.gif            div1.style.visibility
="visible";
ExpandedSubBlockEnd.gif            }

ExpandedBlockEnd.gif        
</ script >
None.gif    
</ HEAD >
None.gif    
< body  MS_POSITIONING ="GridLayout"  onload ="Init()" >
None.gif        
< form  id ="Form1"  method ="post"  runat ="server" >
None.gif            
< FONT  face ="宋体" ></ FONT >
None.gif            
< div  id ="div1" >< FONT  face ="宋体" >   </ FONT >
None.gif            
</ div >
None.gif        
</ form >
None.gif    
</ body >
None.gif
</ HTML >
None.gif

  
 web service 项目中

 还有就是一定要在 web.config 中
    <system.web>
   中加上

None.gif < webServices >
None.gif     
< protocols >
None.gif       
< add  name ="HttpPost"   />
None.gif       
< add  name ="HttpGet"   />
None.gif     
</ protocols >
None.gif
</ webServices >

否则一直提示出错..
一定要加在你的  web service 项目中,其它地方就不用了......


demo  地址

http://files.cnblogs.com/gwazy/WebService_Demo.rar


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值