2017-05-14 9 views
0

私はサーバ/クライアント側の違いを理解しています...しかし、最後に希望が消えてしまうので、私はここに来て質問しなければなりませんでした。実行時にasp.netの更新ラベル

私のアプリでは、ある時点で多くのユーザーのレポートを生成しています。そのレポートの生成時に、%completeというラベルがあります。 これまでのところ、私は...コード

最新のもの

クッキーを作成
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("~/Klijenti/Klijenti.aspx"); 
request.Headers.Add("ReportPercentage", Value.ToString()); 

の背中側に

var myCookiePerc = Response.Cookies["ReportPercentage"]; 

if (string.IsNullOrWhiteSpace(myCookiePerc.Value) || 
    string.IsNullOrEmpty(myCookiePerc.Value)) 
{ 
    myCookiePerc = new HttpCookie("ReportPercentage");    
} 

Response.Cookies.Remove("ReportPercentage"); 

myCookiePerc.Values["ReportPercentage"] = Value.ToString(); 
myCookiePerc.Expires = DateTime.UtcNow.AddMinutes(2); 
Response.Cookies.Add(myCookiePerc); 

そして、私はhere

から得た答えでクッキーを読んを複数のものを試してみました

そしてそれ以前には、

ClientScript.RegisterStartupScript(GetType(), "nyScript", "updateCompletePercent(" + percent + ", true); 
ScriptManager.RegisterStartupScript(GetType(), "nyScript", "updateCompletePercent(" + percent + ", true); 

しかし、これまでのところすべてが失敗してしまった...誰もが、コードビハインドからそのラベルを実行時にどのように更新することができますか? これを達成する方法は他にもありますか?私はちょうどこのレポートの作成中に、私のasp.netアプリで "プログレスバー"や "ラベルが1から100%に増加"のようなものを持ってほしい

+0

これを達成することはできません – hardkoded

+0

@kblok可能です。私の答えを見てください。 – CodingYoshi

+0

@コーディングYoshiもちろん、すべてが人生で可能ですが、コード全体を再実装する必要があります – hardkoded

答えて

1

あなたが望むことを達成するには、定期的にサーバーを呼び出す必要がありますajaxと進歩を求める。しかし、それは良い解決策ではありません。なぜなら、あなたはコールする頻度を知ることができないからです。代わりに、より良い解決策、あなたがサーバーに "やあ、この仕事をして、進捗状況を私に報告する"と伝えることができれば理想的です。そして、サーバーがブラウザにメッセージを送信する必要があるこのような状況では、あなたの親友はSignalRです。

このタスクを達成するには、以下の手順に従ってください:

  1. SignalR NuGet Package
  2. をダウンロードStartupクラスのConfigureAuthメソッドにこのコード行を追加します。

    app.MapSignalR(); 
    
  3. ウェブを追加メソッドをコードの背後に置いて、作業を行います。クライアントにメッセージを送信するために、このクラスを追加

    // We must have this class in order for SignalR to communicate between server and client. 
    // We don't have to define any method here because in this example we want to send data directly from server. 
    // In a chat application it will take a message from a client and send it to other client(s) etc... 
    public class ProgressHub : Hub 
    { 
    
    } 
    
  4. public class Messenger 
    { 
        public static void SendProgress(string progressMessage, int progressCount, int totalItems) 
        { 
         // In order to invoke SignalR, let's get the context 
         var hubContext = GlobalHost.ConnectionManager.GetHubContext<ProgressHub>(); 
    
         // Calculate % 
         var percentage = (progressCount * 100)/totalItems; 
    
         // Send to client 
         hubContext.Clients.All.AddProgress(progressMessage, percentage + "%"); 
        } 
    } 
    
  5. using Microsoft.AspNet.SignalR;をインポートすることを確認します(このクラスを追加

    public partial class _Default : Page 
    { 
        // We are mimicking some work here 
        [WebMethod] 
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)] 
        public static string LongRunningProcess() 
        { 
         int itemsCount = 100; 
    
         for (int i = 0; i <= itemsCount; i++) 
         { 
          Thread.Sleep(500); 
          Messenger.SendProgress("Process in progress...", i, itemsCount); 
         } 
    
         return "Completed"; 
        } 
    } 
    
  6. :この例では、あなたは、このメソッドを使用することができます

  7. サーバーからのメッセージを聞くには、JavaScriptが必要です。

    ここで
    $(function() { 
    
        // Reference the auto-generated proxy for the hub. 
        var progress = $.connection.progressHub; 
        console.log(progress); 
    
        // Create a function that the hub can call back to display messages. 
        progress.client.addProgress = function (message, percentage) { 
    
         // we got a message from the server so update the label 
         $('#<%=Label1.ClientID%>').html(percentage); 
        }; 
    
        //Before doing anything with our hub we must start it 
        $.connection.hub.start(); 
    }); 
    

ページの完全なコードです。

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Asp._Default" %> 

    <asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server"> 

    <!-- This is what will do the magic --> 
    <script src="Scripts/jquery.signalR-2.2.2.min.js"></script> 
    <script src="signalr/hubs"></script> 

    <!-- We will udpate this label. --> 
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> 

    <!-- We will start the process when this button is clicked. --> 
    <button onclick="StartProcess()" type="button">Start the process</button> 

    <script> 
     // Let's call the server using jQuery AJAX 
     function StartProcess() { 
      $.ajax({ 
       type: "POST", 
       url: "Default.aspx/LongRunningProcess", 
       contentType: "application/json; charset=utf-8", 
       dataType: "json", 
       success: function (data) { 
        alert(data.d); 
       } 
      }); 
     } 

     $(function() { 

      // Reference the auto-generated proxy for the hub. 
      var progress = $.connection.progressHub; 
      console.log(progress); 

      // Create a function that the hub can call back to display messages. 
      progress.client.addProgress = function (message, percentage) { 

       // we got a message from the server so update the label 
       $('#<%=Label1.ClientID%>').html(percentage); 
      }; 

      //Before doing anything with our hub we must start it 
      $.connection.hub.start(); 
     }); 

    </script> 
    </asp:Content> 

あなたはすべての手順に従っていることを確認します。手順のいずれかを省略すると、動作しません。コードを貼り付けてコピーするだけではありません(テストされていますが)、それを理解しようとしているので、それを維持する必要がある場合は、何が起こっているのかを知ってください。

+0

本当にありがとうございました – Veljko89

関連する問題