当前位置导航:炫浪网>>网络学院>>网页制作>>ASP教程

ASP.NET编程实例ABC(3)

      
  
  
  在 ASP.NET中显示事件日志记录
  在Windows 2000 或 NT中,事件日志对于管理员来说几乎是最重要的信息来源,因为所有发生的事件都在那里进行记录,无论是成功的操作,还是灾难性的失败。既然如此,你是否想过让这些信息在Web上呈现出来? 是的,这是很有意义的事情。
  我们都应该很熟悉下面的事件阅读器了,下面就示范如何使用AP.NET和.NET Framework SDK来美观、清晰地模仿其条目列表。我还给读者留下一个练习题,为一个条目的完整细节构造一个页面。
  
  
  较粗笨的方法
  如果必须又快又粗略地完成任务,那么完全可以利用ASP语言的相关技术来生成一个事件列表(甚至可以用表格,但是这个例子没有那样做)。程序的名字是 simple.aspx,代码如下:
  <% @Page Language="C#" %>
  <% @Import Namespace="System.Diagnostics" %>
  <%
  EventLog aLog = new EventLog();
  aLog.Log = "System";
  aLog.MachineName = "."; // Lokale Maschine
  string strImage = ""; // Icon für das Event
   
  Response.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 也是如此,正如其名称所示,它可以创建一个来自数据的表格或者栅格。唯一的前提是数据源要支持Icollection接口,而EventLog (事件日志)的Entries Collection(条目集合 )正是满足这一要求的。
  以下的文件datagrid.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>
  <body bgcolor="#ffffff">
   
  <h3>System Event Log</h3>
   
  <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"
  />
  </form>
   
  </body>
  </html>
  DataGrid 控件 除了包含格式化指令外,什么也没有。Grid通过Page_Load 事件来填充,它打开事件日志,然后将DataGrid的DataSource属性指定为Entries(条目)。接着调用 DataBind方法,所有的数据就被填入到表格中。
  数据量确实不小,因为 EventLogEntry 类有许多属性,而我们只想要一个简洁的概括。下一部分就将对此进行限定。
  限定DataGrid中的字段
  接着的目的是要显示某些特定的字段。在讨论代码前,我们先快速看一下执行后的结果:
  
  
  从原则上说,这个结果与前面的例子非常相似,唯一的不同就是显示的栏数。这种限定是靠 DataGrid 标记本身进行的,文件speccolsonly.aspx 包含了全部实现代码:
  <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">
  <property name="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"/>
  </property>
  </asp:DataGrid>
  第一个重要步骤是将AutoGenerateColumns 属性设置为假,这样就可以防止显示所有属性。接着指定想要显示的栏目,在这里,我们指定了4个栏目。HeaderText显示在顶部行中,DataField给出填充这个栏目所要读取的属性。
  在DataGrid中分页
  在结束之前,我们还要使用 DataGrid 的另一个功能,也就是 数据库程序员的老相识-"分页"处理。DataGrid的优势在于处理分页几乎不需要代码。还是请先看一下执行后的结果:
  
  
  请看paging.aspx文件的全部源代码:
  <% @Page Language="C#" %>
  <% @Assembly Name="System.Diagnostics" %>
  <% @Import Namespace="System.Diagnostics" %>
  <script language="C#" runat="server">
  void Page_Load(Object sender, EventArgs e)
  {
  BindGrid();
  }
  void LogGrid_Page(Object sender, DataGridPageChangedEventArgs e)
  {
  BindGrid();
  }
  void BindGrid()
  {
  EventLog aLog = new EventLog();
  aLog.Log = "System";
  aLog.MachineName = ".";
   
  LogGrid.DataSource = aLog.Entries;
  LogGrid.DataBind();
  }
  </script>
  <body bgcolor="#ffffff">
  <h3>System Event Log</h3>
  <form runat="server">
  <asp:DataGrid id="LogGrid" runat="server"
  AllowPaging="True"
  PageSize="10"
  PagerStyle-Mode="NumericPages"
  PagerStyle-HorizontalAlign="Right"
  PagerStyle-NextPageText="Next"
  PagerStyle-PrevPageText="Prev"
  OnPageIndexChanged="LogGrid_Page"
  BorderColor="black"
  BorderWidth="1"
  GridLines="Both"
  CellPadding="3"
  CellSpacing="0"
  Font-Name="Verdana"
  Font-Size="8pt"
  HeaderStyle-BackColor="#aaaadd"
  AutoGenerateColumns="false">
  <property name="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"/>
  </property>
  </asp:DataGrid>
  </form>
  </body>
  </html>
  我们可以看到,第一个变化在 DataGrid控件中:
  AllowPaging="True"
  PageSize="10"
  PagerStyle-Mode="NumericPages"
  PagerStyle-HorizontalAlign="Right"
  PagerStyle-NextPageText="Next"
  PagerStyle-PrevPageText="Prev"
  OnPageIndexChanged="LogGrid_Page"
  其中的两个最重要的属性是第一个和最后一个:AllowPaging和 OnPageIndexChanged。第一个激活分页功能,第二个指明当页面变化时所引起事件对应的方法。剩下的属性都是装饰性的。
  当我们使用的是一个数据集合而不是数据库时,可以通过将数据重新捆绑在栅格上而使处理工作变得很容易。
  小 结
  以上介绍了在ASP.NET中如何阅读事件日志,但这不是主要目的,我们的意图是要说明DataGrid的使用是多么得广泛,这已经超出了数据库编程的主要应用领域。
  结 语
  以上通过3个实例的实现思路、原理以及具体代码示范了ASP.NET的实际应用。这3个例子都非常典型并且实用,我希望从这里起步,开始你的ASP.NET之神奇、强大的编程旅程。
  

 
相关内容
赞助商链接