0

I am working on a web application where I want to show a loading image (busy indicator) when my code create a xml file and download it I want to show the image in a div. I should use c# code only not update panel nor the jquery ajax technique. My code looks like:

protected void lb_DownloadXML_Click(object sender, EventArgs e)
    {
        this.imgLoading.Visible = true;
        //all my code
        this.imgLoading.Visible = false;
    }

my image is

<img src="Images/loading_big.gif" width="50" height="40" runat="server" id="imgLoading"
                                    visible="false" />

but its not working. Can anybody explain me how can I achieve this task.

thanks in advance.

Chuck Norris
  • 15,207
  • 15
  • 92
  • 123
tanweer
  • 83
  • 5
  • 18
  • 1
    The "this.imgLoading.Visible = true;" will only take effect on a postback, and in this case the postback will only occurr once all the code in the lb_DownloadXML onclick handler is finished. This is the reason you do not see the Image. – Keagan Ladds Jan 17 '12 at 05:31
  • why dont you go for placing a div containing gif image and show and hide that from server side using RegisterClientScriptBlock() method using jquery or may be basic javascript and for that too either you have to use jquery ajax post to server or Update panel – Devjosh Jan 17 '12 at 05:34
  • Yes it is possible, you can render contents to the page sequentially by setting the "Response.BufferOutput" property to false, writing directly to the Response.Output and Flushing the stream. IF you set the image to be visible and wrote the HTML to the Output stream it "should", depending on your browser, display before any other content, you could then hide the div/image using javascript when the document has completed loading. http://msdn.microsoft.com/en-us/library/33cy25ty(v=vs.100).aspx http://msdn.microsoft.com/en-us/library/ahsabzd6(v=vs.100).aspx – Lloyd Jan 17 '12 at 06:28

2 Answers2

1

To execute server side code from client machine, there is no other way other than UpdatePanel or Ajax. The client request should reach to the server to execute the request. And the way this happens is by PostBack or by Get request. PostBack will reload page, if you are not using UpdatePanel (I guess which you don't want) and second is GET, which again you don't want.

Update

According to @Lloyd

Yes it is possible, you can render contents to the page sequentially by setting the "Response.BufferOutput" property to false, writing directly to the Response.Output and Flushing the stream.

Community
  • 1
  • 1
Amar Palsapure
  • 9,590
  • 1
  • 27
  • 46
  • Hmmm in this case how client will trigger event on server? Plz explain. – Amar Palsapure Jan 17 '12 at 06:33
  • Read the response I wrote, the OP doesn't state he does not want to do a postback he simply states he wants to show an image in a div, which is easily done using the Response.Output stream. – Lloyd Jan 17 '12 at 06:44
  • In this case when user will click download xml, there will be postback, and the response to client will be buffered one... interesting. If it works I will be very glad. And what about the browser dependency you mentioned? Which browsers doesn't support it? – Amar Palsapure Jan 17 '12 at 06:51
  • All of the current browsers will work fine, how do you think most of the Airline booking websites handle the dynamic page redirects using the same techniques. – Lloyd Jan 17 '12 at 07:01
  • can you give me an example related to my requirement please? – tanweer Jan 17 '12 at 08:59
  • You can write javascript function which will display the image, when user clicks the download link, when the download is complete hide the image. Check [this](http://stackoverflow.com/questions/6990589/download-and-open-a-file-in-jquery) post and read the comments – Amar Palsapure Jan 17 '12 at 09:51
0

You should well understand that when you write any code on server side it is only reflected in the broswer after a successful postback. Over here if you write C# code to display an progress image then it would be displayed only after the postback is successful, that means after your XML file has been created and downloaded to the client end or after successful execution of lb_DownloadXML_Click().

So there is no way to achieve that using C# server side code, you have to rely on client side programming to achievev this.

Bibhu
  • 4,053
  • 4
  • 33
  • 63