asp.netvb.netinfinite-looptextwriter

ASP.NET - Infinite loop when using server.execute


When using server.execute to capture the html of the current page the following code gets stuck in an infinite loop.

On Debug

Server.Execute causes the button click event to fire each time. But why?

It appears that preserveForm = true is maintaining the click event within the form but I need this to maintain my querystrings.

Protected Sub btn_Click(sender As Object, e As EventArgs) Handles btn.Click
  test()
End Sub

Protected Sub test()
  Dim writer As TextWriter = New StringWriter()
  Server.Execute("ThisPage", writer, True)
End Sub

I am aware I can do some kind or variable flag to check for the return of the loop but I want to know:


Solution

  • As per the MSDN page for the HttpServerUtility.Execute Method, the preserveFrom parameter of this method has the following defined behaviour:

    preserveForm

    Type: System.Boolean

    true to preserve the QueryString and Form collections; false to clear the QueryString and Form collections.

    The Form collection will include state data for the Controls on the page. As such, this could lead to an infinite loop if Execute() is performed as part of an event.