ScriptManager.RegisterStartupScript inside while loop only executed after breaking out of the loop

夙愿已清 提交于 2019-12-11 13:39:18

问题


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
           }

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!


回答1:


This code is "Server side" code:

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

            //doing something else
           }

While this is "client side" code:

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

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.

To explain the issue you are facing let's focused on the "Render" Event, if you check the documentation for this event:

protected override void Render(HtmlTextWriter output) {
    if ( (HasControls()) && (Controls[0] is LiteralControl) ) {
        output.Write("<H2>Your Message: " + ((LiteralControl) Controls[0]).Text + "</H2>");
    }
}

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.

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.

  • *There is a method Response.Flush() that can be used for partial rendering but I do not think it will fix your problem either and it is the only exception I know to some of my statements previously presented.


来源:https://stackoverflow.com/questions/20872019/scriptmanager-registerstartupscript-inside-while-loop-only-executed-after-breaki

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!