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

相关推荐
IT技术分享社区3 分钟前
在 Windows 上跑未改动的 Linux 程序:微软 LiteBox v0.1 把这件事变简单了
linux·运维·windows·microsoft·开源
宝桥南山1 小时前
.NET 11 - 尝试创建一下AGUIServer和AGUIClient(基于新版.NET SDK for AG-UI)
microsoft·ai·微软·c#·aigc·.net
jason.zeng@150220721 小时前
(九)多轮对话式新增维修记录实现方案
python·prompt·交互·llama
ji_shuke1 天前
使用 Canvas 和 mix-blend-mode 实现跟随鼠标的反色圆圈
microsoft·计算机外设·canvas
驱动小百科1 天前
Microsoft Visual C++ 2015安装失败如何修复 教你逐步处理
microsoft·vc++ 2015安装失败·visual c++ 2015·运行库安装失败·vc++ 2015安装错误
jason.zeng@15022071 天前
(五)多轮对话上下文,实现交互。
python·交互·llama
jason.zeng@15022071 天前
(十)分层架构的多文件工程
python·架构·prompt·交互·ai编程·llama
海盗12341 天前
微软技术日报 2026-10-08:Windows 给智能体立沙箱,Surface 换上 NVIDIA 芯
人工智能·windows·microsoft·机器人·aigc
墨天梦1 天前
B16_Material主题与可访问交互
android·kotlin·交互
鱼宵2 天前
Spring AI 初体验:配好 yml 就能聊,ChatClient 四步链式调用
人工智能·spring·microsoft·大模型·springai·chatclient