html5中文学习网

您的位置: 首页 > 网络编程 > ASP.NET » 正文

使用ASP.NET 示事件日_.NET教程_编程技术

[ ] 已经帮助:人解决问题
asp.net

在 Windows 2000 (或 NT) 的事件日管理者可是最重要的息源,因所有的生的事件都在那 ─ 成功到重性的失。由於是如此的重要,那要是能透 web 使用,可不是更能突?

大家事件器不陌生,在篇文章我明如何使用 ASP.NET 和 .NET Framework SDK 能完美地仿效列出日。了者作,我先保留於呈的建置。

使用篇文章的原始在你的 Webserver 上必安 Microsoft .NET Framework SDK。 同我也假者 C# 程式有一定程度的。

暴力手段法

了能更迅速且又不是那的乾俐落手段,我可以好好利用去 ASP 的知生一系列事件。(即使是 table,然例不是要做 table)。程式的名也就是玩意的名: simple.aspx.

<% @Page Language="C#" %><% @Import Namespace="System.Diagnostics" %><%EventLog aLog = new EventLog();aLog.Log = "System";aLog.MachineName = ".";  // Local machinestring strImage = "";  // Icon for the eventResponse.Write("<p>There are  " + aLog.Entries.Count +          " entries in the System event log.</p>");   foreach (EventLogEntry entry in aLog.Entries) {  switch (entry.EntryType)  {    case EventLogEntryType.Warning:      strImage = "warning.png";      break;    case EventLogEntryType.Error:      strImage = "error.png";      break;    default:      strImage = "info.png";      break;  }  Response.Write("<img src=/"" + strImage + "/"> | ");  Response.Write(entry.TimeGenerated.ToString() + " | ");  Response.Write(entry.Source + " | ");  Response.Write(entry.EventID.ToString() + "<br>/r/n");}%>

事件日的可以在名空找到 System.Diagnostics,必它放在始的地方。打日本身就是很直接: 生新的 EventLog 物件, 指明 Log 和 MachineName ("." 是本地端的器)。然後我就取事件日。

我使用 foreach 圈完成工作。使呈不那的缺乏意,我在每一都放一正的案,列出的器一般的序相反: 去的列最先。

使用 DataGrid 更完美

在 ASP.NET 中有多的新,特是在料的展示,而且更棒的是料不一定要自料中。 DataGrid Web Control 也是如此,也就如其名一,料中生 table (grid)。唯一需求料源必是支援 ICollection 介面 ─ 也就是使用 EventLog 的集合 Entries。

以下的原始 (speccolsonly.aspx) 明了使用 DataGrid 是如此的:

<% @Page Language="C#" %><% @Import Namespace="System.Diagnostics" %><script language="C#" runat="server">void Page_Load(Object sender, EventArgs e) {  EventLog aLog = new EventLog();  aLog.Log = "System";  aLog.MachineName = ".";    LogGrid.DataSource = aLog.Entries;  LogGrid.DataBind();}</script>

DataGrid 控制 (接下的程式) 只包含格式化的指令,有其他。Grid 是藉由 Page_Load 事件填去,就打了事件日,然後分配 Entries () 作 DataGrid 的 DataSource 性。著呼叫 DataBind 料就了 table 中 ─ 但是我只用到位,如下像所示:

的限制完成工作都是在 DataGrid 本身的 (speccolsonly.aspx 包含完整程式):

<form runat="server"><asp:DataGrid id="LogGrid" runat="server"    BorderColor="black"    BorderWidth="1"    GridLines="Both"    CellPadding="3"    CellSpacing="0"    Font-Name="Verdana"    Font-Size="8pt"    HeaderStyle-BackColor="#aaaadd"    AutoGenerateColumns="false">    <Columns>      <asp:BoundColumn HeaderText="TOF" DataField="EntryType" />      <asp:BoundColumn HeaderText="Date/Time" DataField="TimeGenerated"/>      <asp:BoundColumn HeaderText="Source" DataField="Source"/>      <asp:BoundColumn HeaderText="Event ID" DataField="EventID"/>    </Columns></asp:DataGrid></form>

首要的步就是定 AutoGenerateColumns 性 false,一可避免自示所有性。在我藉可以指明我所要的位。

我使用了四位 (到料源),HeaderText 呈在最上一列,而在 DataField 中取性填入所予的位中。

例中我故意位的定用得一。有多的位型,而你始使用格式化把玩位,者倒是可以你 "狂似的" 好好玩一玩呢!你可以在 QuickStart tutorial 找到更多的例。

<

6qbHTML5中文学习网 - HTML5先行者学习网
6qbHTML5中文学习网 - HTML5先行者学习网
(责任编辑:)
推荐书籍
推荐资讯
关于HTML5先行者 - 联系我们 - 广告服务 - 友情链接 - 网站地图 - 版权声明 - 人才招聘 - 帮助