使用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

相关推荐
William_cl1 小时前
【C# MVC 前置】异步编程 async/await:从 “卡界面” 到 “秒响应” 的 Action 优化指南(附微软官方避坑清单)
microsoft·c#·mvc
日日行不惧千万里9 小时前
绕过微软联网账号限制:用本地用户安装 Windows 11 的最新 3 种方法
windows·microsoft
KKKlucifer9 小时前
自动化漏洞利用技术颠覆传统:微软生态暴露的攻防新变局
运维·microsoft·自动化
Leinwin9 小时前
微软 365 迁移解决方案
microsoft
张晓~1833994812110 小时前
碰一碰发抖音源码技术搭建部署方案
线性代数·算法·microsoft·矩阵·html5
FreeBuf_11 小时前
微软数字防御报告:AI成为新型威胁,自动化漏洞利用技术颠覆传统
人工智能·microsoft·自动化
猫林老师18 小时前
HarmonyOS分布式硬件共享:调用手机摄像头的手表应用
华为·交互·harmonyos
yantuguiguziPGJ18 小时前
WPF 联合 Web 开发调试流程梳理(基于 Microsoft.Web.WebView2)
前端·microsoft·wpf
SmartSoftHelp开发辅助优化1 天前
C# WinForm 编程高手:程序,进程,线程。程序,窗体,UI,后台。是如何协调工作的?深度解析>SmartSoftHelp魔法精灵工作室
microsoft·ui·c#