default.aspx.cs

来自「Asynchrounous pages with Ajax to refresh」· CS 代码 · 共 63 行

CS
63
字号
using System;
using System.Data;
using System.Web;
using System.Web.Services;
using System.Web.UI;

public partial class _Default : Page 
{
protected void Page_Load(object sender, EventArgs e)
{
  // If no data has been cached yet, generate
  //  test data for purposes of demonstration.
  //
  // In actual use, this should cache data
  //  from your database or other live source.
  if (Cache["Headlines"] == null)
  {
    // Create a DataTable.
    DataTable dt = new DataTable("Headlines");

    // Add schema for the article example.
    dt.Columns.Add("Date", typeof(DateTime));
    dt.Columns.Add("Title", typeof(string));

    // Populate the test data.
    dt.Rows.Add(new object[] {DateTime.Now, 
       "CSS style as AJAX progress indicator"});
    dt.Rows.Add(new object[] {DateTime.Now.AddDays(-1.25), 
       "AJAX, file downloads, and IFRAMEs"});
    dt.Rows.Add(new object[] {DateTime.Now.AddDays(-2), 
       "Easily refresh an UpdatePanel, using JavaScript" });

    // Cache the initialized DataTable.
    Cache["Headlines"] = dt;
  }
}

[WebMethod]
public static long GetLatestHeadlineTick()
{
  // Retrieve the cached DataTable.
  DataTable dt = (DataTable)HttpContext.Current.Cache["Headlines"];

  // Sort by date and find the latest article.
  DataRow row = dt.Select("", "Date DESC")[0];

  // Return that article's timestamp, in ticks.
  return ((DateTime)row["Date"]).Ticks;
}

protected void up1_Load(object sender, EventArgs e)
{
  // Retrieve the cached DataTable.
  DataTable dt = (DataTable)Cache["Headlines"];

  // Set the hidden field's value to the
  //  latest headline's "tick".
  LatestDisplayTick.Value = GetLatestHeadlineTick().ToString();

  Headlines.DataSource = dt;
  Headlines.DataBind();
}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?