w0051977
w0051977

Reputation: 15797

Response.write and ASP.NET controls

Please see the code below:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        Response.Write("<table><tr><td>some text</td></tr></table>")
        Response.Flush()
        Threading.Thread.Sleep(3000)
        Response.Write("<table><tr><td>some text</td></tr></table>")
        Response.Flush()
        Threading.Thread.Sleep(3000)
        Response.Write("<table><tr><td>some text</td></tr></table>")
        Response.Flush()
        Threading.Thread.Sleep(3000)
        Response.Write("<table><tr><td>some text</td></tr></table>")
        Response.Flush()
        Threading.Thread.Sleep(3000)
    End Sub

The code works as I would expect i.e. a HTML table is generated and written to the screen, then after three seconds another table is generated and added to the screen etc.

I have a webpage that dynamically adds x number of tables (http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.table%28v=vs.110%29.aspx) to a webpage. However, Response.Flush does not seem to work. I have created the simple code below to explain the problem:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        Dim Table1 As New Table
        Dim r As New TableRow()
        Dim c As New TableCell()
        c.Text = "Test 1"
        r.Cells.Add(c)
        Table1.Rows.Add(r)
        Response.Flush()
        Threading.Thread.Sleep(3000)

        Dim Table2 As New Table
        Dim r2 As New TableRow()
        Dim c2 As New TableCell()
        c.Text = "Test 2"
        r.Cells.Add(c)
        Table1.Rows.Add(r)
    End Sub

Is it possible to flush table1 to the client, then wait a few seconds then flush table2 or do you have to write a string to response.write?

Upvotes: 0

Views: 4676

Answers (1)

Guffa
Guffa

Reputation: 700262

To use Response.Flush in that way you need to output all the HTML code yourself using Response.Write.

You can't use web controls as they are not rendered until the page is complete. The web controls only exist as objects until the Page_Render stage where the entire page is rendered as HTML code and then sent to the browser.

Upvotes: 3

Related Questions