zoukankan      html  css  js  c++  java
  • asp.net异步处理

    程序代码 程序代码
    一.页面异步调用

    前台

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

    <!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>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label><br />
            <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label><br />
            <asp:TextBox ID="TextBox1" runat="server" ReadOnly="true" columns="80" rows="25" TextMode="MultiLine"></asp:TextBox><br />
            <br />
            &nbsp;</div>
        </form>
    </body>
    </html>
    后台:

    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.Net.WebRequest myRequest = null; //获取网络资源
        protected void Page_Load(object sender, EventArgs e)
        {
            BeginEventHandler bh = new BeginEventHandler(this.BeginGetAsyncData);
            EndEventHandler eh = new EndEventHandler(this.EndGetAsyncData);
            AddOnPreRenderCompleteAsync(bh, eh);
            string address = "http://www.hao123.com";
            myRequest = System.Net.WebRequest.Create(address);
        }

        IAsyncResult BeginGetAsyncData(Object src, EventArgs args, AsyncCallback cb, Object state)
        {
            Trace.Warn("BeginGetAsyncData is Called");
            Label1.Text = "BeginGetAsyncData: thread #" + System.Threading.Thread.CurrentThread.GetHashCode();
            return myRequest.BeginGetResponse(cb, state); //异步请求
        }

        void EndGetAsyncData(IAsyncResult ar)
        {
            Trace.Warn("EndGetAsyncData is Called");
            Label2.Text = "EndGetAsyncData: thread #" + System.Threading.Thread.CurrentThread.GetHashCode();

            System.Net.WebResponse myResponse = myRequest.EndGetResponse(ar);   //获取请求结果
            TextBox1.Text = new System.IO.StreamReader(myResponse.GetResponseStream()).ReadToEnd();
            myResponse.Close();
        }
    }

    程序代码 程序代码
    对比--页面同步调用

    前台

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

    <!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>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" /><br />
            <br />
            <asp:TextBox ID="TextBox1" runat="server" TextMode="MultiLine" Height="521px" Width="566px"></asp:TextBox>&nbsp;</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 Default2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            System.Net.WebRequest wr = System.Net.WebRequest.Create("http://www.baidu.com");
            System.Net.WebResponse wb2 = wr.GetResponse();
            TextBox1.Text = new System.IO.StreamReader(wb2.GetResponseStream()).ReadToEnd();
          
        }
    }
  • 相关阅读:
    AS/400开发经验点滴(三)如何使用分布式关系数据库
    AS/400开发经验点滴(五)通用日志管理工具
    AS/400开发经验点滴(二)一个批量修改文件属性的工具
    FTP执行AS400命令
    ORA12514 TNS 监听程序当前无法识别连接描述符中请求服务 的解决方法
    Centos 查看系统硬件信息
    [转]Oracle的DBMS_METADATA包
    java读写删.text,.xml文件内容
    oracle 是user_tables里面可以查找到一个表,而用DESC或者insert语句插入时就会报不存在视图。
    Oracle监听服务lsnrctl参数及查询状态详解
  • 原文地址:https://www.cnblogs.com/sontin/p/1929809.html
Copyright © 2011-2022 走看看