coffeeak
coffeeak

Reputation: 3120

Label Text not changing

Here is the situation: I have a button and when I click it, it will execute some statements and I would like it to show some status messages.

 protected void Button1_Click(object sender, EventArgs e)
 {
    //executes some statements
    //shows some status message like "Done"
 }

I thought of using a label text to show the different status messages, but the label text only shows the last status message when the method completes. It does not show the in-between status messages....What am I doing wrong?

Upvotes: 1

Views: 6061

Answers (5)

celamai
celamai

Reputation: 1

try

Label1.Text="starting stuff";
this.refresh();
//do stuff
Label2.Text="finishing stuff";

this.refresh() causes the screen to repaint in the middle of a method, instead of at the end as it is by default.

Upvotes: 0

tariq
tariq

Reputation: 2258

You can also use a literal which will allow some formatting also

<asp:Literal ID="ltrlMessages" runat="server"></asp:Literal>

protected void Button1_Click(object sender, EventArgs e)
{
    ltrlMessages.Text = "status message one";
    //
    //executes some statements
    //
    ltrlMessages.Text += "</br> status message two";
    //
    //some other statements
    //
    ltrlMessages.Text += "</br> status message three and so on";

}

Upvotes: 1

Ruly
Ruly

Reputation: 360

Or try to do it like this:

Label1.Text = "First Status";
// Do something
Label1.Text += "\nSecond Status";
// Do something again
Label1.Text += "\nDone!!";

Upvotes: 1

roybalderama
roybalderama

Reputation: 1640

If you want to show all status for this. You can use other control (for example Listbox) that will handle your array/list of status that will be invoked in each executions.

Upvotes: 0

Lynn Crumbling
Lynn Crumbling

Reputation: 13367

ASP.Net doesn't work the way you're trying to get it to. The entire method will finish (updating the label to one and only one final value) and then render the page (with the final value of the label.)

I can think of two ways to do what you're looking for:

1) Write a bunch of Web Service methods and call into each with ajax. Update the label (div) on the client side after each ajax call.

2) Have the page resubmit as soon as the page renders (current operation completes). You'd still need to keep the state of the page to know where in the process (e.g. 2 of 4 operations) you currently are (increment a hidden form variable or a session variable, then use the value of that variable in page load to decide what to do if IsPostback.)

Upvotes: 1

Related Questions