2016-06-02 4 views
1

ping.exe 프로그램의 웹 양식을 만들려고하는데 다음과 같은 모양입니다. ping 명령과 같이 동적으로 결과를 textbox2에 삽입하고 싶습니다. 내가 어떻게 그럴 수 있니? (내가 시작 버튼을 누르면 그것은 몇 초를 기다렸다가 한 번에 모든 결과를 출력합니다!)ASP.NET : 결과를 동적으로 텍스트 상자에 표시합니다.

enter image description here

using System; 
using System.Net.NetworkInformation; 
using System.Threading; 
using System.Text; 

public partial class ping : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
    } 

    //------------------------------------------------------------------------------- 
    protected void Button1_Click(object sender, EventArgs e) 
    { 

     string ip = TextBox1.Text; 
     string nn = DropDownList1.SelectedItem.Text.ToString(); 
     int n = int.Parse(nn); 

     Ping PingSender = new Ping(); 
     PingOptions PingOpt = new PingOptions(); 
     StringBuilder sb = new StringBuilder(); 

     for (int i = 0; i <= n; i++) 
     { 
      PingReply reply = PingSender.Send(ip); 
      var ttl = reply.Options.Ttl; 
      var rt = reply.RoundtripTime; 

      sb.Append(Environment.NewLine + reply.Address + "\t" + ttl + "\t" + rt); 
      TextBox2.Text = sb.ToString(); 

      Thread.Sleep(1000); 
     }     
    } 
} 

답변

1

이것은 아약스을 달성 할 수 jQuery.Here의 예 :

코드 뒤에 :

public partial class PingThings : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 

    } 

    [WebMethod] 
    public static string GetPing(string ipAddress) 
    { 
     Ping PingSender = new Ping(); 
     PingOptions PingOpt = new PingOptions(); 
     StringBuilder sb = new StringBuilder(); 

     PingReply reply = PingSender.Send(ipAddress); 
     var ttl = reply.Options.Ttl; 
     var rt = reply.RoundtripTime; 

     sb.Append(Environment.NewLine + reply.Address + "\t" + ttl + "\t" + rt); 

     return sb.ToString(); 
    } 
} 

.ASPX :

<head runat="server"> 
    <title></title> 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.3/jquery.min.js"></script> 
    <script type="text/javascript"> 
     $(function() { 

      var timer; 
      var times = 0; 
      var maxTimes = 0; 

      $("#btnOK").click(function() { 

       $("#results").empty(); 
       times = 0; 
       maxTimes = $("#" + '<%=DropDownList1.ClientID%>').val(); 
       timer = setInterval(doAjax, 5000); 

      }); 

      function doAjax() { 

       var ipAddress = $("#ipAddress").val(); 

       $.ajax({ 
        type: "POST", 
        url: 'PingThings.aspx/GetPing', 
        data: '{ipAddress:"' + ipAddress + '"}', 
        contentType: "application/json; charset=utf-8", 
        dataType: "json", 
        success: function (response) { 

         $("#results").append("<div>" + response.d + "</div>"); 

         times++; 

         if(times >= maxTimes) 
         { 
          clearTimeout(timer); 
         } 


        }, 
        failure: function (response) { 
         alert(response.d); 
        } 
       }); 
      } 
     }); 
    </script> 
</head> 
<body> 
    <form id="form1" runat="server"> 
     <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true"></asp:ScriptManager> 
     IP&nbsp;<input type="text" id="ipAddress" /> 
     <asp:DropDownList ID="DropDownList1" runat="server" Width="300"> 
      <asp:ListItem>1</asp:ListItem> 
      <asp:ListItem>2</asp:ListItem> 
      <asp:ListItem>3</asp:ListItem> 
      <asp:ListItem>4</asp:ListItem> 
      <asp:ListItem>5</asp:ListItem> 
     </asp:DropDownList> 
     <input type="button" id="btnOK" value="OK" /> 
     <div id="results"></div> 
    </form> 
</body> 
관련 문제