<p>在编程练习中,用得最多的语句应该是控制台输出语句,如Java中的System.out.println(),C中的printf(),C#中的Console.WriteLine()等等。</p>
在Web编程中,我们没有控制台,不过也很想在练习过程中,像在控制台输出一些信息一样,在网页上输出一些信息,即把网页当成是控制台。
在ASP.NET中,我的方法是,给Page对象一个字符串如Information来存储信息,然后,输出在网页上。这样只需要在网页的某个部分,写上<%= this.Information %>。
当然,还缺一个方法,就是向Information中填充内容的方法,我的实现如下:
C# 代码:
public partial class HRStaff_AttendanceReport : System.Web.UI.Page { protected string Information = ""; protected void Page_Load(object sender, EventArgs e) { this.PrintLine("自定义模仿控制台输出测试"); this.PrintLine("http://www.myfootprints.cn"); } protected void PrintLine(string s) { if (this.Information.Length <= 0) { this.Information = "<pre>"; } else { this.Information = this.Information.Replace("</pre>", "<br />"); } this.Information += s + "</pre>"; }protected void PrintLine() { this.PrintLine(""); }
}
有了PrintLine()方法,就可以在后台程序中,像Java中使用System.out.println()一样去使用this.PrintLine()了。
最后,建议将<%= this.Information %>写在母版页中,并将PrintLine()方法写在母版页的后台代码里。然后,对于想要调用PrintLine()方法的子页里,加上<%@ MasterType VirtualPath="Master的URL" %>。这样,就可以做到全局应用此方法了。