2、代码隐藏模式 用于显示的代码放在.aspx文件中(页面文件),用于逻辑处理的代码放在aspx.cs的文件中(代码隐藏文件)。以上述的例子为例,代码隐藏模式为:
<%@ Page Language="C#" CodeFile="SamplePage.aspx.cs" Inherits="SamplePage" AutoEventWireup="true" %> <html> <head runat="server"> <title>隐藏代码模式</title> </head> <body> <from id="form1" runat="server"> <div> <asp:Label id="Label1" runat="server" Text="Label"/><br /> <asp:Button id="Button1" runat="server" onclick="Button1_Click" Text="Button"/> </div> </from> </body> </html>上述是.aspx文件,该模式下是没有script块的,并且在最开头的@Page指令中,需要引用aspx.cs的外部文件和Inherits属性,下面是aspx.cs文件。
using System; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class SamplePage : System.Web.UI.Page { protected void Button1_Click(object sender,EventArgs e) { Label1.Text = "Clicked at"+DateTime.Now.ToString(); } }在较为复杂的网页设计中,更推荐第二种模式,将需要运行的代码统一放在aspx文件中,便于管理。
ASP.Net包含一系列类,用来封装上下文信息,并将这些类的实例作为内部对象提供,下面是一系列类的简介。
Response 可以动态的响应客户端请求,并且可以将动态生成的响应结果返回给客户端浏览器。可以向客户端输出数据、跳转网页等。 主要属性: 1、Buffer:获取或设置是否缓冲输出。 2、ContentType:获取或设置输出流的HTTP MIME类型。 3、Cookies:获取响应Cookie集合。 4、Expires:获取或设置在浏览器上缓存的页过期之前前的分钟数。 5、ExpiresAbsolute:获取或设置从缓存中移除缓存信息的绝对日期和时间。 6、Headers:获取响应头的集合。 7、Status:设置返回到客户端的Status 栏 主要的方法: 1、void BinaryWrite (byte[] buffer):将一个二进制字符串写人HTTP输出流。 2、void Clear():清除缓冲区流中的所有内容输出。 3、void Close():关闭到客户端的套接字连接。 4、void End():将当前所有缓冲的输出发送到客户端,停止该页的执行。 5、void Flush():向客户端发送当前所有缓冲的输出。 6、void Redirect(String url):将客户端重定向到新的URL。 7、void SetCookie ( HttpCookie cookie ):更新Cookie集合中的-一个现有Cookie。 8、void Write ( Strings):将文本写人HTTP响应输出流。 9、void WriteFile ( string filename ):将指定的文件直接写人HTTP响应输出流。