开发者

How to show Label1.Text for each item in a foreach statement?

I want to show each item Id that is doing now dynamically in a foreach statement.

But the following code only shows the last item Id in Label1.Text.

How to show Label1.Text for each item 开发者_高级运维in a foreach statement?

<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />

.

protected void Button1_Click(object sender, EventArgs e)
    {
        List<int> list = new List<int>()
        {
            1,2,3,4,5
        };

        foreach (var item in list)
        {
            Label1.Text = string.Format("I'm doing item {0} now.", item.ToString());
            Thread.Sleep(1 * 1000);
        }
    }


The server will process them all before sending the page back to the user.

To show this information dynamically, you need to use some kind of client callback the get interim updates after each step is complete.


foreach (var item in list)
{
  Label1.Text += string.Format("I'm doing item {0} now.", item.ToString());
  Thread.Sleep(1 * 1000);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜