Link to home
Start Free TrialLog in
Avatar of trippy1976
trippy1976Flag for United States of America

asked on

WebClient().DownloadString() blocks and I can't give user feedback

First time out using WebClient().DownloadString() to get data from a web page.

It works, but I have a label I want to set to something like "Loading Information..." before the download happens.  This is because it takes a few moments for the call to complete.  In this time, it looks like the app has hung to the user.

Despite setting the label prior to making this call, it does not show until AFTER the call completes.

this.status_label.Text = "Loading information...";
this.status_label.BackColor = Color.Green;
this.status_label.ForeColor = Color.White;             
var response = new WebClient().DownloadString("http://whatever.com");

Open in new window


This is the code.  Simple enough, but the status_label text and color only change after the downloadstring call completes.

Can I put up some kind of hourglass or notice or progress bar so the user knows something is working?
Avatar of gery128
gery128
Flag of India image

I would suggest fire up some client script (Javascript) on button click to change Label's value and style.
like using script like this:

        function ChangeLabel() {
            var lblProgress = document.getElementById('status_label');
            lblProgress.value = 'Loading information';
            lblProgress.style.backColor = 'Green';
            lblProgress.style.color = 'White';
       
        }

Then call this ChangeLabel function on clientclick of the button which raises the event of getting web response.

<asp:Button runat="server" OnClientClick="ChangeLable();" ID="btnGetWeb" Text="Test" onclick="btnGetWeb_Click" />
@Gery I agree with you. Setting the label text in server side code will not achieve the desired behaviour since the vakue will only show when the postback is all complete.

Adding to that... what do you want to do with the string you are getting from teh page? It is possible that you could do it all in javascript and avoid the postback completely.
Avatar of trippy1976

ASKER

This is a form application, no server side / client side going on here.  No Javascript involved at all, sorry if that was not clear.

It's a form application using the DownloadString() function to grab a remote web page.  

It does not update the label or show that it's working in any way once I call the DownloadString() function.  It just appears to "hang" until the response comes in.  It's like the entire application blocks (and may be) awaiting the response from the server.
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you!  That was exactly what I needed!