zoukankan      html  css  js  c++  java
  • 非Ajax实现客户端的异步调用

    先来看 default.aspx页面:

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

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        
    <title>无标题页</title>
      
    <script language="javascript" type="text/javascript">
        
    function GetNumber()
        {
           UseCallback();
        }
        
        
    function GetRandomNumberFromServer(values,content)
        {
           document.getElementById(
    "TextBox1").value=values;
        }
      
    </script>
    </head>
    <body>
        
    <form id="form1" runat="server">
        
    <div>
            
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            
    <input id="Button1" type="button" value="button" onclick="GetNumber()" /></div>
        
    </form>
    </body>
    </html>

    接下来看deault.cs页面代码:

    using System;
    using System.Data;
    using System.Configuration;
    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 _Default : System.Web.UI.Page,System.Web.UI.ICallbackEventHandler
    {
        
    private string a = null;
        
    protected void Page_Load(object sender, EventArgs e)
        {
            
    string cbReference = Page.ClientScript.GetCallbackEventReference(this"arg""GetRandomNumberFromServer""context");
            
    string cbScript = "function UseCallback(arg,context)" + "{" + cbReference + ";" + "}";
            Page.ClientScript.RegisterClientScriptBlock(
    this.GetType(), "UseCallback", cbScript, true);
        }

        
    public string GetCallbackResult()
        {
            
    return a;
        }

        
    public void RaiseCallbackEvent(string enentArg)
        {
            Random rd 
    = new Random();
            a 
    = rd.Next().ToString();
        }
    }

    哈哈,就这么简单,点击标准html按钮就会自动获取后台自动生成的一个随机数,并显示到页面上,哈哈,是不是很强大~!!!

  • 相关阅读:
    FindWindowEx
    c# 基础知识
    propertychange 属性说明
    Python3-2020-测试开发-22- 异常
    Python3-2020-测试开发-21- 面向对象之封装,继承,多态
    Python3-2020-测试开发-20- Python中装饰器@property
    Python3-2020-测试开发-19- Python中私有属性和私有方法
    Python3-2020-测试开发-18- Python中方法没有重载
    Python3-2020-测试开发-17- 类编程
    Python3-2020-测试开发-16- 嵌套函数(内部函数 )&nonlacal声明外部函数的局部变量&LEGB规则
  • 原文地址:https://www.cnblogs.com/wantingqiang/p/1471781.html
Copyright © 2011-2022 走看看