Chinaunix首页 | 论坛 | 博客
  • 博客访问: 781320
  • 博文数量: 161
  • 博客积分: 10005
  • 博客等级: 中将
  • 技术积分: 1445
  • 用 户 组: 普通用户
  • 注册时间: 2006-12-04 15:08
文章分类

全部博文(161)

文章存档

2014年(1)

2013年(1)

2011年(2)

2010年(18)

2009年(26)

2008年(18)

2007年(66)

2006年(29)

我的朋友

分类:

2009-10-26 14:26:24

第一种方法:vs2008全面支持ajax,并封装了ajax的实现下面这种方法就是通过vs2008提供的控件实现的。

1、新建一个页面,加入Ajax Extensions控件组中的srciptmanager和updepanel控件,并在UpdatePanel中加入一个Button和一个Label。如下所示:


   

   
       
      
               Width="69px" onclick="Button1_Click" />
        
      

       

2、点击Button1,加入Click事件,

protected void Button1_Click(object sender, EventArgs e)
        {

            this.Label1.Text = System.DateTime.Now.ToString();
        }

3、运行,点击Button1,Label1显示当前系统时间,观察整个过程,页面是不是看起来没有刷新。


第二种方法:通过实现接口icallbackeventhandle实现类似ajax的无刷新页面

------ ClientCallback.aspx ------
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ClientCallback.aspx.cs" ­ Inherits="ClientCallback" %>


   
        Callback Test
       
   
       
         


           

         

   
-------- ClientCallbacp.aspx.cs  -----------
// ClientCallback.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;­
using System.Web.UI.HtmlControls;

public partial class ClientCallback : System.Web.UI.Page, System.Web.UI.ICallbackEventHandle­ r
{

    void Page_Load(object sender, EventArgs e)
    {
        ClientScriptManager cm = Page.ClientScript;
        String cbReference = cm.GetCallbackEventReference(this,­ "arg", "ReceiveServerData", "");
        String callbackScript = "function CallServer(arg, context) {" + cbReference + "; }";
        cm.RegisterClientScriptBlock(this.­ GetType(), "CallServer", callbackScript, true);
    }

    private string returnStr;
    //function called by client, executed on server
    public void RaiseCallbackEvent(String eventArgument)   
    {
        //do something with return argument
        returnStr = eventArgument.ToUpper();
        return;
    }
   
    //function that sends result?
    public string GetCallbackResult()
    {
        return returnStr;
    }
}
客户端用ReceiveServerData接收服务器返回的数据,

使用CallServer(argument, context)传递数据到服务器。

客户端ok了,接下来是服务器端,
实现ICallbackEventHandle­ 接口,
private string returnStr;
 public void RaiseCallbackEvent(String eventArgument)   
    {
        returnStr = eventArgument.ToUpper();
        return;
    }
   
public string GetCallbackResult()
    {
        return returnStr;
    }

最后,将客户端方法和服务器端的方法关联起来,在Page_Load中实现,
ClientScriptManager cm = Page.ClientScript;
 String cbReference = cm.GetCallbackEventReference(this,­ "arg", "ReceiveServerData", "");
String callbackScript = "function CallServer(arg, context) {" + cbReference + "; }";
 cm.RegisterClientScriptBlock(this.­ GetType(), "CallServer", callbackScript, true);

大功告成!

这种方法也能达到类似ajax的无刷新页面,而且实现起来十分简单。

阅读(1758) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~