webservice 实现动态刷新

    FeaturedService.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
None.gif
namespace  WebService
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// FeaturedService 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

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

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{
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 GetFeatured() 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
string strSelect;
InBlock.gif                SqlConnection conPubs;
InBlock.gif                SqlDataAdapter dadPubs;
InBlock.gif                DataSet dstTitles;
InBlock.gif                DataRow drowTitle;
InBlock.gif                Random objRanNum;
InBlock.gif                
int intRanNum;
InBlock.gif        
InBlock.gif                strSelect 
= "SELECT Title, isNull( Notes, '' ) Notes From Titles";
InBlock.gif                conPubs 
= new SqlConnection( "server=(local);database=pubs;uid=sa;pwd=" );
InBlock.gif                dadPubs 
= new SqlDataAdapter( strSelect, conPubs );
InBlock.gif                dstTitles 
= new DataSet();
InBlock.gif                dadPubs.Fill( dstTitles, 
"Titles" );
InBlock.gif                objRanNum 
= new Random();
InBlock.gif                intRanNum 
= objRanNum.Next( 0, dstTitles.Tables["Titles"].Rows.Count );
InBlock.gif                drowTitle 
= dstTitles.Tables["Titles"].Rows[intRanNum];
InBlock.gif                
return drowTitle["Title"+ " - " + drowTitle["Notes"];
ExpandedSubBlockEnd.gif            }

InBlock.gif        
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

调用页面

 
ExpandedBlockStart.gif ContractedBlock.gif <% dot.gif @ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="WebService.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      
InBlock.gif    
InBlock.gif    
var intCallID = 0;
InBlock.gif
InBlock.gif
function Init()
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif  GetNewFeatured();
InBlock.gif  setInterval( 
"GetNewFeatured()"5000 ) 
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
InBlock.gif
function GetNewFeatured()
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif   service.useService(
"FeaturedService.asmx?WSDL","FeaturedService");
InBlock.gif  intCallID 
= service.FeaturedService.callService( "GetFeatured" );
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
InBlock.gif
function Service_Result()
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif  
if (event.result.error) 
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif  divFeatured.innerText 
= event.result.errorDetail.string;
ExpandedSubBlockEnd.gif  }

InBlock.gif  
else
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif  divFeatured.innerText 
= event.result.value;
ExpandedSubBlockEnd.gif  }

ExpandedSubBlockEnd.gif}

InBlock.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            
< table  width ="600" >
None.gif                
< tr >
None.gif                    
< td  valign ="top" >
None.gif                        
< h2 > Welcome to this Web Site! </ h2 >
None.gif                        Browse this Web site to get great deals on the latest books!
None.gif                    
</ td >
None.gif                    
< td >
None.gif                        
< div  id ="service"   style ="behavior:url(webservice.htc)"   onresult ="Service_Result()" ></ div >
None.gif                        
< div  id ="divFeatured"  style ="BORDER-RIGHT:darkgreen 1px solid;PADDING-RIGHT:10px;BORDER-TOP:darkgreen 1px solid;PADDING-LEFT:10px;PADDING-BOTTOM:10px;BORDER-LEFT:darkgreen 1px solid;WIDTH:200px;PADDING-TOP:10px;BORDER-BOTTOM:darkgreen 1px solid" >
None.gif                        
</ div >
None.gif                    
</ td >
None.gif                
</ tr >
None.gif            
</ table >
None.gif        
</ form >
None.gif    
</ body >
None.gif
</ HTML >
None.gif
 


实现效果

  webservice.gif

还有就是要注意那个webservice.htc文件.....
一定要放在程序目录下...








 

转载于:https://www.cnblogs.com/gwazy/archive/2005/05/10/152205.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值