while循环内部的ScriptManager.RegisterStartupScript仅在脱离循环后才执行 [英] ScriptManager.RegisterStartupScript inside while loop only executed after breaking out of the loop

查看:133
本文介绍了while循环内部的ScriptManager.RegisterStartupScript仅在脱离循环后才执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码后面有一个while循环,我想监视进度,因此,每次进入循环时,我希望页面绘制一些东西(一个点),当循环中断时,我会看到整个进度( scatterplot)我的代码如下:

I have a while loop in my codebehind and I want to monitor the progress, so each time I go into the loop, I wish my page draws something (a point) and when the loop breaks I would see the entire progress (scatterplot) My code is like the following:

          while (iter <= Common.maxIter)
          {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "myFunc" + iter.ToString(), "Display()", true);

            //doing something else
           }

函数Display()每次绘制一个点.我的问题是,仅当我退出while循环后,所有条形才会同时绘制,但在循环过程中没有绘制任何条形... :(我不确定哪里出错了,如果我使用断点,我可以看到每次都执行此代码,只是未在页面上显示.

The function Display() draws one point each time. My problem is, only after I break out of the while loop, all of the bars get drawn at the same time, but none of them is drawn during the loop...:( I am not sure where went wrong, if I use break point and I can see this code gets executed everytime, just not shown on my page.

任何人都知道正确的方法是什么?非常感谢!

Any one knows what should be the correct way of doing this? Many thanks!

推荐答案

此代码为服务器端"代码:

This code is "Server side" code:

while (iter <= Common.maxIter)
          {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "myFunc" + iter.ToString(), "Display()", true);

            //doing something else
           }

这是客户端"代码:

"myFunc" + iter.ToString(), "Display()"...

ASP.NET的工作方式是通过在服务器端"执行一系列事件,这也称为页面生命周期".您可能已经知道其中一些事件,只是为您提供这些事件的一些示例:PreInit,Init,PreLoad,Load,PreRender,Render和UnLoad.

The way ASP.NET works is by executing a series of events on the "server side" this is also known as "Page Life Cycle". You may already know some of those events, just to give you some examples of these events: PreInit, Init, PreLoad, Load, PreRender, Render and UnLoad.

我想说最常用的是页面加载事件,我想您的代码示例也放置在该方法中.

I would say that the most often used is the Page Load Event, I guess your code sample was place inside that method as well.

如果要查看

您会注意到,该事件使用的是" HtmlTextWriter ",它基本上是一个Stream.我之所以强调它,是因为您在页面生命周期内对控件或页面执行的每项操作/修改都将在页面生命周期结束时写入到此流中,该流将作为html发送回用户. .

You will notice that this event uses an "HtmlTextWriter" which is basically a Stream. The reason why I highlighted it is because every action/modification you perform to the controls or the page during the page life cycle will be written into this stream at the end of the page life cycle this stream will be send back to the user as html.

因此,回到您的问题,您正在做的是在HTML流中添加"javascript"并将其发送给用户.因此,当页面生命周期结束时,它将立即将所有信息*发送回用户.

So, going back to your problem, what you are doing is adding "javascript" into the HTML Stream that will be send to the user. So when the page life cycle concludes it will sending back all at once* to the user.

  • *有一种方法Response.Flush()可用于部分渲染,但我认为它也不能解决您的问题,这是我之前提到的某些语句中唯一的例外情况.

这篇关于while循环内部的ScriptManager.RegisterStartupScript仅在脱离循环后才执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆