3

Webページ(asp.netサイト)の開始ボタンをクリックしたときにプロセスを開始したいので、ラベルテキストをプロセス開始に設定したいと思います。プロセスが終了したときにラベルテキストを「プロセス完了」に設定したい。asp.net と C# でこれを行う方法。

前もって感謝します。

4

4 に答える 4

3

ASP.NET SignalRの使用を検討することをお勧めします。これが何をするかの要約です:

ASP.NET SignalR は、ASP.NET 開発者向けの新しいライブラリで、リアルタイム Web 機能をアプリケーションに非常に簡単に追加できます。「リアルタイム Web」機能とは何ですか? これは、サーバー側のコードで、コンテンツが発生したときに、接続されているクライアントにリアルタイムでコンテンツをプッシュする機能です。

以下は、 を開始するボタンを含む単純な Web ページの例ですNotepad.exe。プロセスが開始されると、ページのラベルに が表示されますprocess started。プロセスが終了する (Notepadが閉じられる) と、ラベルは に更新されprocess exitedます。

したがって、最初に ASP.NET の空の Web アプリケーション プロジェクトを作成し (名前をMyWebApplicationにします)、Microsoft ASP.NET SignalR NuGet パッケージを取得します。プロジェクトに Web フォームを追加し、Testという名前を付けます。次のコードをTest.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>

プロジェクトに新しいクラス ファイルを追加し、名前を付けますChatChat.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. プロセスがすでに実行されているかどうかは確認していません (ただし、確認するのはそれほど難しくありません)。
于 2013-03-05T14:34:24.767 に答える
0

javascriptを使用してコールバックを実行します。そして、各ステージで。開始、完了、またはエラー。HTMLのラベルを更新します。jQuery AJAXでいくつかのサンプルを探す場合、これはかなり単純なはずです。

jQueryAJAXPOSTの例

于 2013-03-05T11:44:59.400 に答える
0

JavaScript を使用したくない場合...できることは、ボタン クリック イベントが発生したときに最初にラベル テキストを変更することです。

lblLabel.text="process started"

button_click イベントの最後の行は次のようになります。

lblLable.text="process completed";
于 2013-03-05T13:06:23.670 に答える
0

これを CodeBehind に追加します。

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

JAVASCRIPT は、ソース コード (aspx) でこれを追加します。

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

System.Web.UI; を使用して追加します。

また

aspx..でこのように Web フォームにラベルを追加するだけです。

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

コードビハインド aspx.cs に ..

Labelname.Text = "whatever msg you wanna display."
于 2013-03-05T12:53:50.403 に答える