利用ajax无刷新显示系统进程(B/S)!~

<%@ Page language="C#" MasterPageFile="~/MasterPage.master" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
  <ajax:ajaxpanel ID="Ajaxpanel1" runat="server">
  <fieldset>
    <legend>Windows Processes (list is updated every 10 seconds using Callback Timer)</legend>
    <i>This example demonstrates the use of the MagicAjax CallbackTimer. <br />For this example we've set the Callback timer in the Page_Load event (hint: click 'View Source' at the bottom of this page to see the complete source of this aspx file).</i><br /><br />
    <div style="overflow:auto;height:400px;width:325px">
    <asp:DataGrid AutoGenerateColumns="false" AllowSorting="True" OnSortCommand="grid_SortCommand" runat="server" ID="DataGrid1" BackColor="White" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px" CellPadding="5" GridLines="Horizontal" >
      <FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
      <SelectedItemStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
      <PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" Mode="NumericPages" />
      <AlternatingItemStyle BackColor="#F7F7F7" />
      <ItemStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
      <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
      <Columns>
        <asp:BoundColumn DataField="ProcessName" SortExpression="ProcessName" HeaderText="Image Name" />
        <asp:BoundColumn DataField="PhysicalMemory" SortExpression="PhysicalMemory" HeaderText="Mem Usage" DataFormatString="{0:###,###,###} K" />
        <asp:BoundColumn DataField="HandleCount" SortExpression="HandleCount" HeaderText="Handles" DataFormatString="{0:###,###}" />
      </Columns>
    </asp:DataGrid>
    </div>
  </fieldset>
  </ajax:ajaxpanel>
</asp:Content>

<script language="C#" runat="server">
  private void Page_Load(object sender, EventArgs e)
  {
    if (!IsPostBack)
    {
      // For automatic CallBack every 10 seconds.
      MagicAjax.AjaxCallHelper.SetAjaxCallTimerInterval(10000);
    }
    ShowProcesses();
  }

  private void ShowProcesses()
  {
    System.Diagnostics.Process[] processes = System.Diagnostics.Process.GetProcesses();

    System.Data.DataTable dt = new System.Data.DataTable("Processes");
    dt.Columns.Add("ProcessName");
    dt.Columns.Add("PhysicalMemory", typeof(Int64));
    dt.Columns.Add("HandleCount", typeof(Int32));

    foreach (System.Diagnostics.Process process in processes)
    {
      dt.Rows.Add(new object[] {process.ProcessName, process.WorkingSet64 /1000, process.HandleCount });
    }

    System.Data.DataView dv = dt.DefaultView;

    //sort data
    dv.Sort = SortExpression;

    //bind data
    DataGrid1.DataSource = dv;
    DataGrid1.DataBind();
  }

   /// <summary>
   /// SortExpression of the grid (stored in ViewState!)
   /// </summary>
   private string SortExpression
   {
     get
     {
       if (ViewState["_grid_SortExpression"] != null)
       {
         return (string)ViewState["_grid_SortExpression"];
       }
       else
       {
            return "PhysicalMemory desc"; //default
       }
     }
     set
     {
       ViewState["_grid_SortExpression"] = value;
     }
   }

   private void grid_SortCommand(object source, DataGridSortCommandEventArgs e)
   {
      string[] currentSort = SortExpression.Split(' ');
     
      if (currentSort[0] == e.SortExpression)
      {
        SortExpression = string.Format("{0} {1}", e.SortExpression, currentSort[1] == "desc" ? "asc" : "desc");
      }
      else
      {
        SortExpression = e.SortExpression + " asc";
      }

     //refill datagrid
    ShowProcesses();
   }
</script>

 

Lad.li

你可能感兴趣的:(Ajax)