使用Response.Write实现在页面的生命周期中前后台的交互

最近在做一个很大的查询,花时间很多,

用户会以为死掉了,就做了一个前后交互的,用于显示执行进度,在网上找了一下,这个比较合适。

主要是简单,大道至简

改进了一下:效果如下图

代码:

1,****************************************************

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

namespace WebApplication1

{

public partial class WebForm1 : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

}

protected void btnShowProgress_Click(object sender, EventArgs e)

{

int s_sum = 10; //总记录数,执行总次数

System.Text.StringBuilder sb = new System.Text.StringBuilder();

sb.Append("<html><head><script type='text/javascript' src='jscript.js'></script></head><body><form>");

//sb.Append("<table border = '1' width ='10%'><tr><td id ='area'>0</td></tr></table>");

sb.Append("<table border = '0' width ='10%'><tr><td id ='name'>进度:</td><td id ='area' align='right' style='color:red;'>0</td><td id ='area2' style='color:blue;'>" + "/ "+ s_sum.ToString() + "</td></tr></table>");

sb.Append("</body></form></html>");

HttpContext.Current.Response.Write(sb.ToString());

HttpContext.Current.Response.Flush();

for (int i = 1; i <=s_sum; i++)

{

System.Threading.Thread.Sleep(500); //模拟执行的复杂过程

setProcessValue(i.ToString()); //设置进度值

}

}

//执行进度值

private void setProcessValue(string percent) //percent参数类型用string,用int有时会出错

{

System.Text.StringBuilder sb = new System.Text.StringBuilder();

sb.Append("<script>setProcessBarValue('" + percent + "')</script>");

HttpContext.Current.Response.Write(sb.ToString());

HttpContext.Current.Response.Flush();

}

}

}

2,*************************

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

<title></title>

</head>

<body>

<form id="form1" runat="server">

<div>

<asp:Button ID ="btnShowProgress" Text ="Invoke" runat ="server" OnClick="btnShowProgress_Click" />

</div>

</form>

</body>

</html>

3,**********下面这个是javascript文件,文件名为:jscript.js,只包含下面这个函数***********

*********************放在根目录下***************************

function setProcessBarValue(per) {

var area = document.getElementById("area");

if (area) {

area.innerHTML = per;

}

}

上面有部分改动,记录下

原参考网址:https://www.cnblogs.com/isun/p/4160713.html

相关推荐
编码者卢布3 小时前
【Azure Service Fabric】如何在不删除应用的情况下“停止“某个服务,并在需要时快速恢复
microsoft·azure·fabric
王者鳜錸4 小时前
Android 接入 AIKit 轻量版语音合成实战
android·交互·多语种·离线合成·多种发音人
weixin_446260854 小时前
PhysiOmniDuplex‑Bench:面向真实物理环境具身交互智能体的评测基准
人工智能·交互
Leinwin5 小时前
技术视角解析 Microsoft 365 Copilot Standalone:无底座许可与跨平台集成方案
microsoft·copilot
炸薯条!16 小时前
Streamlit实战:从零实现AI智能伴侣(基本交互、会话记忆与流式输出)
交互
垆边人似月.1 天前
机器人交互测评(二):测试场景设计与Python自动化测试实战
python·机器人·交互
炸薯条!1 天前
Streamlit 实战:AI智能伴侣侧边栏设置与多会话管理
microsoft
垆边人似月.1 天前
机器人交互测评(一):核心概念、评价指标与测试体系详解
机器人·交互
总要学点什么2 天前
第128篇:镜头光晕与高光——把逆光太阳晕成会躲山的光斑
开发语言·javascript·数码相机·microsoft·材质·cesium
sean9082 天前
macOS Outlook(或者是Office系列?) App 打开时自动启动 Microsoft Autoupdate 处理
microsoft·macos·outlook·自动更新