Stringbuilder not written to HttpContext.Current.Response.Write in some conditions
<p>I am looping through two input controls of type file with this function.</p>

<pre><code>protected void btnUpload_Click(object sender, EventArgs e)
{
    HttpFileCollection hfc = Request.Files;
    int numFiles = hfc.Count;
    for (int i = 0; i &lt; hfc.Count; i++)
    {
        HttpPostedFile hpf = hfc[i];
        string FilePath = hpf.FileName;
        CreateProgress(FilePath, i + 1, numFiles);
        Thread.Sleep(2000);
    }
}
</code></pre>

<p>At this stage I am doing nothing with the files, I just want to prove that after uploading each file I can write something to the browser to let the user know the files are being uploaded. The function CreateProgress is this:</p>

<pre><code>public void CreateProgress(string FilePath, int i, int numFiles)

{
    StringBuilder sbProgress = new StringBuilder();
    string strTableId = "mainTable" + i;
    sbProgress.Append("&lt;html&gt;&lt;head&gt;&lt;link rel=\"StyleSheet\" href=\"stylesheet/StyleSheet1.css\" type=\"text/css\" /&gt;&lt;/head&gt;&lt;body&gt;");
    sbProgress.Append("&lt;table id=\"" + strTableId + "\" border=\"0\"&gt;");
    sbProgress.Append("&lt;tr&gt;"); 
    sbProgress.Append("&lt;td&gt;");
    sbProgress.Append("&lt;p class=\"filey\"&gt;Uploading " + FilePath + " ..... file " + i.ToString() + " of " + numFiles.ToString() + "&lt;/p&gt;");
    sbProgress.Append("&lt;/td&gt;");
    sbProgress.Append("&lt;td style=\"display:none\"&gt;");
    sbProgress.Append("&lt;/td&gt;");
    sbProgress.Append("&lt;/tr&gt;");
    sbProgress.Append("&lt;/table&gt;");
    sbProgress.Append("&lt;/body&gt;&lt;/html&gt;");
    HttpContext.Current.Response.Write(sbProgress.ToString());
    HttpContext.Current.Response.Flush();
}
</code></pre>

<p>The code as above works perfectly every time. I click to upload the files and on the screen it shows 'Uploading SomeFile.txt ... file 1 of 2' for 2 seconds and then 'Uploading SomeOtherFile.txt ... file 2 of 2' for 2 seconds.</p>

<p>But, you'll see in the code building the StringBuilder that I am constructing a table with one row and two cells. I don't need or use the second cell in the row but if I delete it by commenting out three lines like this ...</p>

<pre><code>    public void CreateProgress(string FilePath, int i, int numFiles)

{
    StringBuilder sbProgress = new StringBuilder();
    string strTableId = "mainTable" + i;
    sbProgress.Append("&lt;html&gt;&lt;head&gt;&lt;link rel=\"StyleSheet\" href=\"stylesheet/StyleSheet1.css\" type=\"text/css\" /&gt;&lt;/head&gt;&lt;body&gt;");
    sbProgress.Append("&lt;table id=\"" + strTableId + "\" border=\"0\"&gt;");
    sbProgress.Append("&lt;tr&gt;"); 
    sbProgress.Append("&lt;td&gt;");
    sbProgress.Append("&lt;p class=\"filey\"&gt;Uploading " + FilePath + " ..... file " + i.ToString() + " of " + numFiles.ToString() + "&lt;/p&gt;");
    //sbProgress.Append("&lt;/td&gt;");
    //sbProgress.Append("&lt;td style=\"display:none\"&gt;");
    //sbProgress.Append("&lt;/td&gt;");
    sbProgress.Append("&lt;/tr&gt;");
    sbProgress.Append("&lt;/table&gt;");
    sbProgress.Append("&lt;/body&gt;&lt;/html&gt;");
    HttpContext.Current.Response.Write(sbProgress.ToString());
    HttpContext.Current.Response.Flush();
}
</code></pre>

<p>... then, when the Upload button is clicked - nothing happens at first, then the second message shows on the screen (Uploading file 2 of 2). Why on earth does commenting out three lines so that the second cell in the row is not created cause it to stop working properly?</p>

<p>In fact, if I leave the 3 lines in and remove the 'style="display:none"' from the td tag - it does not work properly. Put the style="display:none" back in and it works fine.</p>

<p>In actual fact I don't want to really create a table at all. I have tried removing the lot and simply doing this:</p>

<pre><code>    StringBuilder sbProgress = new StringBuilder();
    sbProgress.Append("&lt;html&gt;&lt;head&gt;&lt;link rel=\"StyleSheet\" href=\"stylesheet/StyleSheet1.css\" type=\"text/css\" /&gt;&lt;/head&gt;&lt;body&gt;");
    sbProgress.Append("&lt;p class=\"filey\"&gt;Uploading " + FilePath + " ..... file " + i.ToString() + " of " + numFiles.ToString() + "&lt;/p&gt;");
    sbProgress.Append("&lt;/body&gt;&lt;/html&gt;");
    HttpContext.Current.Response.Write(sbProgress.ToString());
    HttpContext.Current.Response.Flush();
}
</code></pre>

<p>but this does not work either. The first message 'Uploading file 1 of 2' does not appear, but the second one ('Uploading file 2 of 2') does appear.</p>

<p>Any ideas? I'm thinking of becoming a taxi driver.</p>
