3

I want to start a process when i clicked the start button on webpage (asp.net site) now i want to set the label text to process started. and i want to set the label text to "Process Completed " when the process is ended. how to do this in asp.net and C#.

Thanks in advance.

narenderk
  • 31
  • 3

4 Answers4

3

You might want to consider using ASP.NET SignalR. Here's a summary of what it does:

ASP.NET SignalR is a new library for ASP.NET developers that makes it incredibly simple to add real-time web functionality to your applications. What is "real-time web" functionality? It's the ability to have your server-side code push content to the connected clients as it happens, in real-time.

The following is an example of simple web page with a button which starts Notepad.exe. Once the process is started, a label on the page shows process started. When the process exits (Notepad is closed), the label's updates to process exited.

So, first create an ASP.NET empty web application project (let's name it MyWebApplication) and get the Microsoft ASP.NET SignalR NuGet package. Add a web form to the project and name it Test. Add the following code to the Test.aspx file:

<%@ 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>

Add a new class file to your project and name it Chat. In Chat.cs you will have:

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);
        }
    }
}

Add the following to the Test.aspx.cs file:

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");
        }
    }
}

Add the Global.asax file:

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();
        }
    }
}

Some things I haven't covered:

  1. The label is updated on all connections.
  2. I'm not verifying if the process is already running or not (but that shouldn't be very difficult to check).
Alex Filipovici
  • 31,789
  • 6
  • 54
  • 78
0

Use javascript to do the callback. And on each stage; Initiated, Completed or Error; update a label in your html. This should be fairly simple if you look for some samples with jQuery AJAX.

jQuery AJAX POST example

Community
  • 1
  • 1
Yahya
  • 3,386
  • 3
  • 22
  • 40
0

Add this in CodeBehind:

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

JAVASCRIPT add this in source code (aspx):

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

And do add using System.Web.UI;

OR

You can simply add a Label to the webform like this in aspx..

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

and in codebehind aspx.cs add ..

Labelname.Text = "whatever msg you wanna display."
SamuraiJack
  • 5,131
  • 15
  • 89
  • 195
0

If you dont want to use javascript...what you can do is to change label text first when button click event is fired.

lblLabel.text="process started"

and last line in button_click event should be like:

lblLable.text="process completed";
user1181942
  • 1,587
  • 7
  • 35
  • 45