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

相关推荐
AxureMost1 小时前
Axure常用交互功能案例-免费
交互·axure·photoshop
大熊背7 小时前
图像处理专业书籍以及网络资源总结
人工智能·算法·microsoft
Leinwin15 小时前
微软发布新一代存储优化型虚拟机:Azure Laosv4、Lasv4 和 Lsv4 系列
microsoft·azure
大美B端工场-B端系统美颜师19 小时前
多模态数据融合预警:从IoT传感器到卫星监测的可视化方案升级
物联网·交互·数据预警
小彭努力中20 小时前
141.在 Vue 3 中使用 OpenLayers Link 交互:把地图中心点 / 缩放级别 / 旋转角度实时写进 URL,并同步解析显示
前端·javascript·vue.js·交互
蹦蹦跳跳真可爱58920 小时前
Python----大模型(使用api接口调用大模型)
人工智能·python·microsoft·语言模型
Triv202520 小时前
ECU开发工具链1.10版:更强大的测量、校准与数据分析体验.
microsoft·数据分析·汽车电子开发·校准流程自动化·高速信号采集·测试台架集成·实时数据监控
梓贤Vigo21 小时前
【Axure高保真原型】动态打字输入效果
交互·产品经理·axure·原型
爱奥尼欧1 天前
【Linux 系统】基础IO——Linux中对文件的理解
linux·服务器·microsoft
梓贤Vigo1 天前
【Axure视频教程】大小图轮播
交互·产品经理·axure·原型·中继器