2013-03-05 59 views
3

我想,当我点击网页(asp.net网站)按启动键启动进程现在我想设置进程启动标签文本。当过程结束时,我想将标签文本设置为“处理已完成”。如何在asp.net和C#中做到这一点。如何在网站的过程结束时显示消息?

在此先感谢。

+1

你使用更新面板或任何Ajax/jQuery? – jason 2013-03-05 11:53:01

回答

0

使用JavaScript做回调。并在每个阶段;启动,完成或错误;更新你的HTML标签。如果您使用jQuery AJAX查找一些示例,这应该相当简单。

jQuery AJAX POST example

0

在代码隐藏补充一点:

ScriptManager.RegisterStartupScript(this, GetType(), "Records Inserted Successfuly", "Showalert();", true); 

JAVASCRIPT在源代码中添加这个(CSS):

function Showalert() { 
      alert('Records inserted Successfully!'); 
     } 

,做使用的System.Web.UI增加;

OR

您可以将标签简单地添加到像这样的ASPX网页表单..

<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> 

和代码隐藏aspx.cs加..

Labelname.Text = "whatever msg you wanna display." 
+0

我编辑了我的答案,告诉我你是否仍然有任何问题..! – Arbaaz 2013-03-05 13:15:35

0

如果你不想使用javascript ...你可以做的是当按钮点击事件被触发时首先改变标签文本。

lblLabel.text="process started" 

和button_click最后一行的事件应该是这样的:

lblLable.text="process completed"; 
3

你可能要考虑使用ASP.NET SignalR。下面是它做什么摘要:

ASP.NET SignalR是ASP.NET开发人员的一个新的图书馆,它使 太简单了实时网络功能添加到您的应用程序 。什么是“实时网络”功能?这是让你的服务器端代码推送内容到连接 客户,因为它发生在实时的 能力。

以下是一个简单网页的例子,其中一个按钮开始于Notepad.exe。一旦这个过程开始时,页面上的标签显示process started。当进程退出(Notepad是封闭的),标签的更新process exited

因此,首先创建一个ASP.NET空web应用程序项目(我们将其命名为MyWebApplication)并获取Microsoft ASP.NET SignalR NuGet package。 Web表单添加到项目并将其命名为测试。将以下代码添加到测试。ASPX文件

<%@ Page Language="C#" AutoEventWireup="true" 
    CodeBehind="Test.aspx.cs" Inherits="MyWebApplication.Test" %> 
<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 
    <script src="http://code.jquery.com/jquery-1.8.2.min.js" 
     type="text/javascript"></script> 
    <script src="Scripts/jquery.signalR-1.0.1.js" type="text/javascript"></script> 
    <script src="/signalr/hubs" type="text/javascript"></script> 
    <script type="text/javascript"> 
     $(function() { 
      // Proxy created on the fly   
      var chat = $.connection.chat; 
      // Declare a function on the chat hub so the server can invoke it   
      chat.client.addMessage = function (message) { 
       $('#label').text(message); 
      }; 
      // Start the connection 
      $.connection.hub.start(); 
     }); 
    </script> 
</head> 
<body> 
    <form id="form1" runat="server"> 
     <asp:ScriptManager runat="server" /> 
     <div> 
      <asp:UpdatePanel runat="server"> 
       <ContentTemplate> 
        <asp:Button runat="server" Text="Start Notepad.exe" 
         ID="button" OnClick="button_Click" /> 
       </ContentTemplate> 
       <Triggers> 
        <asp:AsyncPostBackTrigger 
         ControlID="button" EventName="Click" /> 
       </Triggers> 
      </asp:UpdatePanel> 
      <span id="label"></span> 
     </div> 
    </form> 
</body> 
</html> 

添加一个新的类文件到您的项目并将其命名为Chat。在Chat.cs你将有:

using Microsoft.AspNet.SignalR; 

namespace MyWebApplication 
{ 
    public class Chat : Hub 
    { 
     public void Send(string message) 
     { 
      //Call the addMessage method on all clients  
      var c = GlobalHost.ConnectionManager.GetHubContext("Chat"); 
      c.Clients.All.addMessage(message); 
     } 
    } 
} 

添加以下到Test.aspx.cs文件:

using System; 
using System.Diagnostics; 
using Microsoft.AspNet.SignalR; 

namespace MyWebApplication 
{ 
    public partial class Test : System.Web.UI.Page 
    { 
     Chat chat = new Chat(); 

     protected void Page_Load(object sender, EventArgs e) 
     { 
     } 

     void MyProcess_Exited(object sender, EventArgs e) 
     { 
      chat.Send("process exited"); 
     } 

     protected void button_Click(object sender, EventArgs e) 
     { 
      Process MyProcess = new Process(); 
      MyProcess.StartInfo = new ProcessStartInfo("notepad.exe"); 
      MyProcess.EnableRaisingEvents = true; 
      MyProcess.Exited += MyProcess_Exited; 
      MyProcess.Start(); 
      chat.Send("process started"); 
     } 
    } 
} 

添加的Global.asax文件

using System; 
using System.Web.Routing; 

namespace MyWebApplication 
{ 
    public class Global : System.Web.HttpApplication 
    { 
     protected void Application_Start(object sender, EventArgs e) 
     { 
      RouteTable.Routes.MapHubs(); 
     } 
    } 
} 

我还没有涉及的一些东西:

  1. 标签在所有连接上更新。
  2. 我不验证过程是否已经运行(但这应该不是很难检查)。
相关问题