• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

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

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
 演示实现客户端回调的 ASP.NET 网页。有关更多信息,请参见在 ASP.NET 网页中不经过回发而以编程方式实现客户端回调 [ http://msdn.microsoft.com/zh-cn/library/ms178208.aspx ]
 示例

说明

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

代码

C#
复制代码
<%@ 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 id="Head1" runat="server">
  <title>Client Callback Example</title>
  <script type="text/ecmascript">
    function LookUpStock()
    {
        var lb = document.getElementById("ListBox1");
        var product = lb.options[lb.selectedIndex].text;
        CallServer(product, "");
    }

    function ReceiveServerData(rValue)
    {   
        document.getElementById("ResultsSpan").innerHTML = rValue;

    }
  </script>
</head>
<body>
  <form id="form1" runat="server">
    <div>
      <asp:ListBox ID="ListBox1" Runat="server"></asp:ListBox>
      <br />
      <br />
      <button type="Button" onclick="LookUpStock()">Look Up Stock</button>
      <br />
      <br />
      Items in stock: <span id="ResultsSpan" runat="server"></span>
      <br />
    </div>
  </form>
</body>
</html>
C#
复制代码
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 String returnValue;
    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 void RaiseCallbackEvent(String eventArgument)
    {
        if (catalog[eventArgument] == null)
        {
            returnValue = "-1";
        }
        else
        {
            returnValue = catalog[eventArgument].ToString();
        }
    }
    public String GetCallbackResult()
    {
        return returnValue;
    }
}

注释

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

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

代码隐藏页通过 RegisterClientScriptBlock [ http://msdn.microsoft.com/zh-cn/library/system.web.ui.clientscriptmanager.registerclientscriptblock.aspx ] 方法向该页添加客户端脚本。添加到该页的脚本包括一个称为 CallServer 的函数,此函数用于获取将从 GetCallbackEventReference [ http://msdn.microsoft.com/zh-cn/library/system.web.ui.clientscriptmanager.getcallbackeventreference.aspx ] 方法回发到服务器的方法的名称。

客户端回调会调用 RaiseCallbackEvent [ http://msdn.microsoft.com/zh-cn/library/system.web.ui.icallbackeventhandler.raisecallbackevent.aspx ] 方法,以确定传递给它的产品的可用库存。GetCallbackResult [ http://msdn.microsoft.com/zh-cn/library/system.web.ui.icallbackeventhandler.getcallbackresult.aspx ] 方法将返回该值。请注意,在客户端脚本与服务器代码之间发送的参数只能是字符串。若要传入或接收多个值,可以分别在输入字符串或返回字符串中将这些值串连起来。

安全说明:

使用此功能时,存在潜在的安全威胁。由于不对回调参数进行验证,因此存在一定的不安全因素。每次使用参数之前,都应对参数的内容进行检查。有关详细信息,请参见脚本侵入概述 [ http://msdn.microsoft.com/zh-cn/library/w1sw53ds.aspx ]

 请参见

任务

如何:在 ASP.NET 网页中实现回调 [ http://msdn.microsoft.com/zh-cn/library/ms366518.aspx ]

概念

在 ASP.NET 网页中不经过回发而以编程方式实现客户端回调 [ http://msdn.microsoft.com/zh-cn/library/ms178208.aspx ]
具有验证实现的客户端回调示例 [ http://msdn.microsoft.com/zh-cn/library/ms366515.aspx ]
标记: 
 
社区内容

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#委托事件发布时间:2022-07-10
下一篇:
使用Rider写一个C#的Hello World程序发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap