【问题标题】:Show a Message Box after a Thread in ASP.net在 ASP.net 中的线程后显示消息框
【发布时间】:2012-07-03 18:57:59
【问题描述】:

我是 .Net 的新手。请帮助我。
我的 aspx 页面中有一个按钮和一个标签,我想在标签中显示文本,几秒钟后我想在按钮单击时显示消息框。 我的代码是 -

<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Label ID="lblFirst" runat="server" Text=""></asp:Label> 
<asp:Button ID="btnClick" runat="server" Text="MyThread" 
       onclick="btnClick_Click" />
</ContentTemplate>
</asp:UpdatePanel>

代码文件-

protected void btnClick_Click(object sender, EventArgs e)
    {
        lblFirst.Text = "Thread Start!";

        Thread threadObj = new Thread(new ThreadStart(myThreadMethod));
        threadObj.Start();
    }

    public void myThreadMethod()
    {
        //Server Code that take long time..
        Debug.Write("Thread Ending");
        Page.ClientScript.RegisterStartupScript(GetType(),"msgbox","alert('End');",true);

    }

我得到了标签的文本更改但未显示消息框。
是否可以在线程之后显示消息框?
我该怎么做?
感谢您的关注和回复。

【问题讨论】:

标签: c# asp.net multithreading message


【解决方案1】:

您可以使用System.Threading.Thread.Sleep(x);
将 x 替换为以毫秒为单位的时间。
之后你可以使用Button1.PerformClick();
其中 Button1 是按钮的名称。

【讨论】:

    【解决方案2】:

    @Nyein Nyein Chan Chan:

       Response.Write("<script language='javascript'>alert('Your message is here.')</script>");
    

    【讨论】:

    • 由此,我得到一个错误-“响应在此上下文中不可用。”
    【解决方案3】:

    使用后台工作人员,然后添加事件 BackGroundWorker 完成,在该事件中你添加你的消息框

            BackgroundWorker bgw = new BackgroundWorker();
    
        // setup the anonymous event, this fires when the thread starts
        bgw.DoWork += (sender, e) =>
        {
            // do work here
        };
    
        // event when the thread has finished
        bgw.RunWorkerCompleted += new RunWorkerCompletedEventHandler((sender, e) =>
        {
            // set yr messagebox here
        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多