首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 编程语言 > .NET > C#.NET > ASP.NET客户端回调实现 (C#) 示例
【标  题】:ASP.NET客户端回调实现 (C#) 示例
【关键字】:ASP.NET,C#
【来  源】:http://blog.csdn.net/fanweiwei/archive/2006/10/10/1328218.aspx

ASP.NET客户端回调实现 (C#) 示例

ASP.NET 
客户端回调实现 (C#) 示例 

 

演示实现客户端回调的 ASP.NET 网页。有关更多信息,请参见在 ASP.NET 网页中不经过回发而实现客户端回调

示例

说明

下面的代码示例分为两部分。示例的第一部分演示一个 ASP.NET 网页(.aspx 页)。第二部分演示相应的代码隐藏文件(.aspx.cs 文件)。

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 
  1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html  >
<head 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>

// ClientCallback.aspx.cs code-behind page
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.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();
    }


    
public String RaiseCallbackEvent(String eventArgument)
    
{
        String returnValue;
        
if (catalog[eventArgument] == null)
        
{
            returnValue 
= "-1";
        }

        
else
        
{
            returnValue 
= catalog[eventArgument].ToString();
        }

        
return returnValue;
    }

}

该网页模拟一个数据库查找,以确定一系列产品(显示器、键盘等)的供货数量或库存数量。为了简化此代码示例,数据库由包含少量物品的词典列表来表示。对于表中的每件物品,键就是物品名称(如显示器),值就是物品的库存数。但是在成品应用程序中,将使用数据库。

当运行此页时,ListBox 控件被绑定到哈希表,这样,ListBox 控件便可以显示产品列表。此页还包含一个 button 元素(并非 Button Web 服务器控件),其 onclick 事件被绑定到一个名为 LookUpStock 的客户端函数。当用户单击按钮时,该按钮便会执行 LookUpStock 函数,此函数从列表框中获取当前所选内容,然后通过调用 CallServer 函数来执行客户端回调。

代码隐藏页通过 RegisterClientScriptBlock 方法向该页添加客户端脚本。添加到该页的脚本包括一个称为 CallServer 的函数,此函数用于获取将从 GetCallbackEventReference 方法回发到服务器的方法的名称。

客户端回调调用 RaiseCallbackEvent 方法,该方法会返回传递给它的产品的可用库存。请注意,在客户端脚本与服务器代码之间发送的参数只能是字符串。

Security note安全注意

使用此功能时,存在潜在的安全威胁。由于不对回调参数进行验证,因此存在一定的不安全因素。每次使用参数之前,都应对参数的内容进行检查。有关详细信息,请参见脚本侵入概述

请参见

net2.0客户端回调实现 (C#) 示例:【上一篇】
引发 Session 丢失的几种原因:【下一篇】
【相关文章】
  • net2.0客户端回调实现 (C#) 示例
  • ASP.NET 2.0 正式版中callback的一些变化+使用示例
  • Asp.Net2.0无刷新客户端回调
  • C#实现钩子
  • 将屏幕区域保存到文件中(C#.net)
  • 将 JavaScript 与 ASP.NET 2.0 结合使用的简便方法
  • C#学习小结
  • 《Effective C#》Item 18:Dispose函数的标准实现模式
  • 《Effective C#》Part II:第二部分总结
  • 初学java总结一下它与其它一些语言(c#,delphi)的差别.
  • 【随机文章】
  • Solaris硬盘分区学习笔记
  • GCC —— 一切从这里开始
  • Linux进程的软中断通信
  • DB2管理员认证一
  • 如何规划 Linux 主机
  • USANA产品 > 身体好转反应
  • QQ2003IIIbeta4新功能应用(2)
  • Linux服务器配置指南
  • 关于super指针指向的父类非静态变量
  • MITSUBISHI 三菱IPM功率模块DIP-IPM双列直插PS21564-P PS21564-SP MITSUBISHI 三菱IPM功率模块DIP-IPM双列直...
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 软讯网络 All Rigths Reserved.