首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WebProject C#中的列表框和睡眠线程

WebProject C#中的列表框和睡眠线程
EN

Stack Overflow用户
提问于 2015-05-26 13:37:07
回答 1查看 140关注 0票数 1

我有一个新的网络项目,它有一个ListBox和一个按钮到Default.aspx:

代码语言:javascript
复制
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

        <br />
        <br />
        <asp:ListBox ID="ListBox1" runat="server" Width="174px"></asp:ListBox>
        <br />
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
        <br />
        <br />
        <br />
        <br />

    </div>
    </form>
</body>
</html>

以及Default.aspx.cs下面的代码:

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ProvaSequenza
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            ListBox1.Items.Add("Start");
            ListBox1.DataBind();
            ListBox1.Items.Add("sleep");
            System.Threading.Thread.Sleep(10000);
            ListBox1.Items.Add("The End.");

        }
    }
}

当我运行这个项目时,我没有得到我所期望的:我期望列表框中添加“开始”,在“睡眠”之后加上“结束”。相反,我先得到了“睡眠”,然后加入了“开始”和“结束”.为什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-05-26 13:40:07

您需要理解您的C#代码在服务器上执行。服务器端代码生成html、javascript和css。浏览器下载所有这些,并显示该内容并执行javascript。因此,使用Thread.Sleep的代码在服务器上执行,同时为页面生成html。

如果您想在用户机器上执行某些代码,则必须在浏览器中使用Javascript。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30460188

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档