A. B.
A. B.

Reputation: 13

Changing label text on button click that also executes other methods

I am trying to update a label on my windows form with statistics from another methods execution that scrapes a webpage and creates a zip file of the links and creates a sitemap page. Preferably this button would run the scraping operations and report the statistics properly. Currently the scraping process is working fine but the label I want to update with statistics data is not changing on the button click. Here is what my code looks like now:

protected void btn_click(object sender, EventArgs e)
{
    //Run scrape work
    scrape_work(sender, e);
    //Run statistics work
    statistics(sender, e);
}

protected void scrape_work(object sender, EventArgs e)
{
    //Scraping work (works fine)
}

protected void statistics(object sender, EventArgs e)
{
    int count = 0;
    if (scriptBox.Text != null)
    {
        count += 1;
    }
    var extra = eventsBox.Text;
    var extraArray = extra.Split('\n');
    foreach (var item in extraArray)
    {
        count += 1;
    }
    //scrapeNumLbl is label I want to display text on
    scrapeNumLbl.Text = count.ToString();
}

Would I have to use threading for this process or is there some other way I can get this process to work? I have already tried this solution but was having the same issue where the code runs but the label does not update. Any help would be greatly appreciated, this minor thing has been bugging me for hours now.

Upvotes: 0

Views: 527

Answers (2)

A. B.
A. B.

Reputation: 13

I eventually solved this by writing the path to the zip file to a label button on the form rather than sending it right to download on the client's browser on button click. The issue was that the request was ending after the zip file was sent for download. To ensure that both methods ran at the proper time I moved the call to the scrape_work method to inside of of statistics

In order for the path to be clickable and the file to download properly I had to make the "label" in the form a LinkButton in the .aspx page

<asp:LinkButton ID="lblDownload" runat="server" CssClass="xclass" OnClick="lblDownload_Click"></asp:LinkButton>

And made the lblDownload_Click run like the following:

Response.Clear();
Response.BufferOutput = false;
Response.ContentType = "application/zip";
Response.AddHeader("content-disposition", "attachment; filename=zipFiles.zip");
string folder = Server.MapPath("~/zip");
string endPath = folder + "/zipFiles.zip";
Response.TransmitFile(endPath);
Response.End();

Running it this way the page reloads with the new labels properly written and the zip file available to download as well.

Upvotes: 1

markaaronky
markaaronky

Reputation: 1305

ASSUMING THIS CODE IS RUNNING SYNCHRONOUSLY (you aren't threading the scraping in a call I don't see), Are you sure you are reaching the code that sets the label text? I simplified your code as below (removing the iteration over the array and just setting the label text to a stringified integer) and am not having any trouble changing the text of a label.

namespace SimpleTest

{ public partial class Form1 : Form { public Form1() { InitializeComponent(); }

    private void button1_Click(object sender, EventArgs e)
    {
        scrape_work(sender, e);
        statistics(sender, e);
    }

    protected void scrape_work(object sender, EventArgs e)
    {
        //Scraping work (works fine)
    }

    protected void statistics(object sender, EventArgs e)
    {
        int count = 666;

        scrapeNumLbl.Text = count.ToString();
    }
}

}

Result: enter image description here

Upvotes: 0

Related Questions