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

Run_Table.asmx 

using  System;
using  System.Collections;
using  System.ComponentModel;
using  System.Data;
using  System.Diagnostics;
using  System.Web;
using  System.Web.Services;
using  System.Data .SqlClient ;

namespace  WebService_Demo
{
    
/// <summary>
    
/// Run_Table 的摘要说明。
    
/// </summary>

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


        
private System.Data.SqlClient.SqlConnection sqlConnection1;

        
#region 组件设计器生成的代码
        
        
//Web 服务设计器所必需的
        private IContainer components = null;
                
        
/// <summary>
        
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
        
/// 此方法的内容。
        
/// </summary>

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

        }


        
/// <summary>
        
/// 清理所有正在使用的资源。
        
/// </summary>

        protected override void Dispose( bool disposing )
        
{
            
if(disposing && components != null)
            
{
                components.Dispose();
            }

            
base.Dispose(disposing);        
        }

        
        
#endregion


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


          [WebMethod]
        
public string doSearch(string  id)
        
{
            
//SqlConnection cn=new SqlConnection(ConfigurationSettings.AppSettings["pubs"]);
            SqlDataAdapter da=new SqlDataAdapter("select ord_date,ord_num from sales where stor_id=@stor_id",sqlConnection1);
              
              
              
              
string[] s={"7067","7131","7896","8042"};

              Random Rand
=new Random ();
              
int i= Rand.Next(0,3);
                  
               
string temp=s[i];
                                
               
             
            da.SelectCommand.Parameters.Add(
new SqlParameter("@stor_id",temp));
            DataSet ds
=new DataSet();
            da.Fill(ds);
            
return ds.GetXml();
        }

    }

}

WebForm1.aspx

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

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

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

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

  
 web service 项目中

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

< webServices >
     
< protocols >
       
< add  name ="HttpPost"   />
       
< add  name ="HttpGet"   />
     
</ protocols >
</ webServices >

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


demo  地址

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


 

你可能感兴趣的:(web Service)