Web项目,在处理大数据量和复杂业务的情况下,用户在浏览器中点击一个按钮后,服务器需要处理几十秒甚至好几分钟,才能将处理结果反馈给用户,在这个漫长的等待过程中,用户面对毫无反应的浏览器会不知所措,即便提示用户正在处理,用户由于不知道服务器的处理状态,在长时间等待后,也会不耐烦或误以为操作失败,而去刷新或者关闭浏览器。
注意<iframe>的src属性的值是“Output.aspx”。再追加一个新的Web窗体,名为“Output.aspx”,通过请求它在<iframe>里来显示处理结果。
删除Output.aspx页面上的所有自动生成的html代码。在Page_Load事件里添加处理过程,处理那个传说中的大数据量的超复杂的业务运算。并且在这个处理过程中,实时地用Response.Write方法输出当前处理的状态信息,并且用Response.Flush强制回发给客户端,使用户得到及时的反馈。
1、在Default.aspx的Button的“Click”事件处理过程中,定义一个Session,存储一个标识和其他画面输入的数据,用于传递到Output.aspx页面,在Output.aspx页面上判断,只有该Session存在,并且标识正确的情况下,才使用Session保存的其他信息,来做那个传说中的大数据量的超复杂的业务运算;
<%@PageLanguage="C#"%>
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<scriptrunat="server">
privateconststringSessionKey="Default";
protectedvoidButton1_Click(objectsender,EventArgse)
{
Hashtableht=newHashtable();
ht.Add("post","true");
ht.Add("count",TextBox1.Text);
Session[SessionKey]=ht;
}
</script>
<htmlxmlns="http://www.w3.org/1999/xhtml">
<headrunat="server">
<title>Demo-HttpResponse.Flush方法</title>
</head>
<body>
<formid="form1"runat="server">
<asp:ScriptManagerID="ScriptManager1"runat="server"/>
<div>
<asp:LabelID="Label1"runat="server"Text="Label">实行次数</asp:Label>
<asp:TextBoxID="TextBox1"runat="server"></asp:TextBox>
<asp:ButtonID="Button1"runat="server"OnClick="Button1_Click"Text="实行"/>
<br/>
<iframesrc="Output.aspx"id="OutputFrame"allowtransparency="true"frameborder="1"
width="400px"height="200px"scrolling="auto"marginheight="0"></iframe>
</div>
</form>
</body>
</html>
Output.aspx
<%@PageLanguage="C#"%>
<scriptrunat="server">
privateconststringSessionKey="Default";
protectedvoidPage_Load(objectsender,EventArgse)
{
stringstartHTML="<!DOCTYPEHTMLPUBLIC\"-//W3C//DTDXHTML1.0Transitional//EN\"\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">"+Environment.NewLine
+"<htmlxmlns=\"http://www.w3.org/1999/xhtml\">"+Environment.NewLine
+"<head>"+Environment.NewLine
+"</head>"+Environment.NewLine
+"<body>"+Environment.NewLine;
startHTML+=newString('',1024)+Environment.NewLine;
Response.Write(startHTML);
Response.Flush();
if(Session[SessionKey]!=null)
{
Hashtableht=(Hashtable)Session[SessionKey];
Session[SessionKey]=null;
Output(ht);
}
stringendhTML=Environment.NewLine+"</body>"+Environment.NewLine
+"</html>";
Response.Write(endhTML);
Response.Flush();
}
privatevoidOutput(Hashtableht)
{
if(ht.ContainsKey("post")&&ht["post"].ToString()=="true")
{
stringjs="<scripttype=\"text/javascript\">window.scrollTo(0,document.body.scrollHeight);<"+"/script>";
intcount=0;
int.TryParse(ht["count"].ToString(),outcount);
Response.Write("开始<br/>");
Response.Flush();
for(inti=0;i<count;i++)
{
Response.Write(string.Format("正在处理第<spanstyle='color:blue'> {0} </span>次……",i+1));
Response.Write(js);
Response.Flush();
System.Threading.Thread.Sleep(1*1000);
Response.Write("<spanstyle='color:red'>完成</span><br/>");
Response.Flush();
}
Response.Write("结束<br/>");
Response.Write(js);
Response.Flush();
}
}
</script>
http://www.cnblogs.com/Philoo/archive/2009/09/10/1564266.html
优质内容筛选与推荐>>
1、UIButton详解2、asp.net mvc 3.0 知识点整理 ----- (2).Controller中几种Action返回类型对比3、如何为数据库服务器配置存储和内存(转载)4、【技术贴】小米利用Connectify共享无线网卡连接教程5、计算机基础知识
长按二维码向我转账
受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。