ASP.NET:在文本框中动态显示结果

时间:2016-06-02 10:16:16

标签: c# asp.net textbox

我正在尝试创建一个ping.exe程序的网络表单,它看起来像这样。我想将结果动态地插入到textbox2中,就像ping命令将显示的那样。我是怎么做到的? (当我按下开始按钮时,它会等待几秒钟,然后立即打印所有结果!)

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 个答案:

答案 0 :(得分:1)

这可以通过ajax和jQuery来实现。这是一个例子:

代码背后:

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>