Avatar of trippy1976
trippy1976
Flag 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?
.NET ProgrammingC#MySQL Server

Avatar of undefined
Last Comment
trippy1976

8/22/2022 - Mon
gery128

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

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" />
Mlanda T

@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.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
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
Bob Learned

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
trippy1976

ASKER
Thank you!  That was exactly what I needed!