惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Microsoft Security Blog
Microsoft Security Blog
WordPress大学
WordPress大学
Stack Overflow Blog
Stack Overflow Blog
D
DataBreaches.Net
罗磊的独立博客
博客园 - 司徒正美
Last Week in AI
Last Week in AI
The Cloudflare Blog
大猫的无限游戏
大猫的无限游戏
Microsoft Azure Blog
Microsoft Azure Blog
B
Blog RSS Feed
The GitHub Blog
The GitHub Blog
宝玉的分享
宝玉的分享
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
小众软件
小众软件
Jina AI
Jina AI
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Hugging Face - Blog
Hugging Face - Blog
B
Blog
博客园 - 【当耐特】
V
V2EX
Apple Machine Learning Research
Apple Machine Learning Research
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
酷 壳 – CoolShell
酷 壳 – CoolShell

博客园 - 小小

编程的首要原则是什么? AD .NET组件和COM组件之间的相互操作//(转) (转帖)提高SQL Server性能的几种技术 (转帖)从玩具到游戏 看另类项目激励机制 删除Flash9b.ocx和FlashUtil9b.exe 无限级无刷新菜单树 (转载)ASP.NET 2.0客户端回调的实现分析 (二) (转载)ASP.NET 2.0客户端回调的实现分析 (一) SQL 游标 3级JAVA菜单 CrystalReport错误消息和公式编译器警告 WinForm最简单两GridView同步滚动 (转) MOSS2007的自定义字段(转) 在XP中为打印机自定义纸张 数字人民币转大写 水晶报表基础 2005的TreeView,选中checkbox框页面不回发的解决方法 古怪的ConfigurationManager类
ASP.NET2.0实现页面无刷新CallBack_修正版
小小 · 2007-04-02 · via 博客园 - 小小

(转自:taito的专栏)

在看到众多网站转载的这篇文章时,发现调式时有错误,特发表了此修正版

后面的例子大家针对原版举一反三。

Asp.Net2.0的客户端回调是一种很让人激动的方法,他能够让我们控制要提交什么数据给服务器而不用提交整个页面,同时服务器也只返回你所需要的数据而不要发回整个页面。

  首先我们要说一个很重要的方法:GetCallbackEventRefernce.
  GetCallbackEventReference首先实现让客户端脚本有能力传递参数给服务器端的RaiseCallbackEvent方法,然后返回RaiseCallBackEvent方法的值给你在GetCallbackEventRefernce方法中注册的一个参数(其实也是一个你要在客户端写的脚本)。调用GetCallbackEventRefernce你必须从客户端脚本中传递给他两个参数,一个是要传递给RaiseCallbackEvent事件的值,一个是context.

  他的参数意义如下:

  第一个:实现了ICallbackEventHandler借口的页面或者服务器控件,写this代表但前页面。

  第二个:代表你从要从客户端传递给服务器RaiseCallbackEvent方法的值

  第三个:你要在客户端写的一个js函数,同时,服务器也会把计算得到的数据传递给这个函数做为这个函数的参数。

  第四个:context具体什么意思我也不太清楚GetCallbackEventRefernce发送到了客户、端的代码是这样的:

    WebForm_DoCallback('__Page',arg,ReceiveServerData,context,null,false)
  那么我们要怎么样做才能够从客户端调用他呢?看到了三中方法:

  第一种:在后台写个public string,在Page_Load中给他赋值为:=Page.ClientScript.GetCallbackEventReference(this, "message", "ShowServerTime", "context");注意在这里是Page.ClientScrip,因为他会返回个ClientScriptManager,ClientScriptManager管理所有的客户端脚本。然后在前台某个按钮的onclick事件里<%=那个public后台字符串%>.做个小实验代码如下:

  前台ServerTime.aspx:为了方便去掉好多没用的html 

<%@ page language="C#" CodeFile="ServerTime.aspx.cs" Inherits="ServerTime_aspx" %> 
<html> 
<head> 
<title>Server Time</title> 
<script language="javascript"> 

function GetServerTime() 

  var message 

= ''
  var context 
= ''
 
<%=sCallBackFunctionInvocation%> 

function ShowServerTime(timeMessage, context) { 
  alert(

'现在服务器上的时间是: ' + timeMessage); 

</script> 
</head> 
<body> 
<form id="MainForm" runat="server"> 
<input type="button" value="得到服务器端时间" onclick="GetServerTime();" /> 
</form> 
</body> 
</html> 

后台:

using System;
using System.Web.UI;public partial class ServerTime_aspx : Page,ICallbackEventHandler
{
  
//一定要实现ICallbackEventHandler接口
  public string sCallBackFunctionInvocation;void Page_Load(object sender, System.EventArgs e)
  {
   sCallBackFunctionInvocation 
= Page.ClientScript.GetCallbackEventReference(this"message""ShowServerTime""context");
  }

    String returnValue;

string ICallbackEventHandler.GetCallbackResult()
    {
        
return returnValue;
    }
void ICallbackEventHandler.RaiseCallbackEvent(string eventArgument)
    {
        returnValue 
= DateTime.Now.ToString();
    }
}

注意:

这里定义了一个字符串 String returnValue;

在GetCallbackResult 和 RaiseCallbackEvent  前都加上了 ICallbackEventHandler,好象是C#就要加上这个,VB不用。

一个 string  GetCallbackResult  和 void RaiseCallbackEvent   搞定!

--------------------------------------

再给大家一个例子:

<%@ Page Language="C#" AutoEventWireup="true" 
  CodeFile
="Exp2.aspx.cs" Inherits="Exp2" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 
  1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
  
<script type="text/javascript">
    function LookUpStock()
    {
        var lb 
= document.forms[0].ListBox1;
        var product 
= lb.options[lb.selectedIndex].text 
        CallServer(product, 
"");
    }
    
    function ReceiveServerData(rValue)
    {
        Results.innerText 
= rValue;
    }
  
</script>
</head>
<body>
  
<form id="form1" runat="server">
    
<div>
      
<asp:ListBox ID="ListBox1" Runat="server"></asp:ListBox>
      
<br />
      
<br />
      
<button onclick="LookUpStock()">Look Up Stock</button>
      
<br />
      
<br />
      Items 
in stock: <span ID="Results"></span>
      
<br />
    
</div>
  
</form>
</body>
</html>

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 Exp2 : System.Web.UI.Page, System.Web.UI.ICallbackEventHandler
{
    
protected System.Collections.Specialized.ListDictionary catalog;
    
protected void Page_Load(object sender, EventArgs e)
    {
        String cbReference 
=
            Page.ClientScript.GetCallbackEventReference(
this,
            
"arg""ReceiveServerData""context");
        String callbackScript;
        callbackScript 
= "function CallServer(arg, context)" +
            
"" + cbReference + "} ;";
        Page.ClientScript.RegisterClientScriptBlock(
this.GetType(),
            
"CallServer", callbackScript, true);

        catalog 

= new System.Collections.Specialized.ListDictionary();
        catalog.Add(
"monitor"12);
        catalog.Add(
"laptop"10);
        catalog.Add(
"keyboard"23);
        catalog.Add(
"mouse"17);

        ListBox1.DataSource 

= catalog;
        ListBox1.DataTextField 
= "key";
        ListBox1.DataBind();
    }
string earg = "";//#region ICallbackEventHandler 成员

    
public string GetCallbackResult()
    {
        String returnValue;
        
if (catalog[earg] == null)
        {
            returnValue 
= "-1";
        }
        
else
        {
            returnValue 
= catalog[earg].ToString();
        }
        
return returnValue;
    }
public void RaiseCallbackEvent(string eventArgument)
    {
        
//throw new Exception("The method or operation is not implemented.");
        earg = eventArgument;
    }
//#endregion

}

Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1032999