页面输出缓存作为最简单的缓存形式,输出缓存只是在内存中保留为响应请求而发送的 HTML 的副本。其后再有请求时将提供缓存的输出,直到缓存到期,这样,性能有可能得到很大的提高(取决于需要多少开销来创建原始页面输出 - 发送缓存的输出总是很快,并且比较稳定)。
设置页面输出缓存可以使用以下两种方式:一种是使用@ OutputCache指令,另一种是使用页面输出缓存API。@ OutputCache指令曾经在ASP.NET 1.x中出现过,并在ASP.NET 2.0中得到了继承和增强。页面输出缓存API主要是指HttpCachePolicy类。
@ OutputCache 以声明的方式控制 ASP.NET 页或页中包含的用户控件的输出缓存策略。
语法如下
<% @ OutputCache Duration = " #ofseconds "
Location = " Any | Client | Downstream | Server | None |
......
用Asp.Net上传文件,首用到的是FileUpload控件
<asp:FileUpload ID="FileUpload1" runat="server" /><br />
<asp:Button ID="btn" runat="server" Text="确定" OnClick="btn_Click"/>
C#代码
protected void btn_Click(object sender, EventArgs e)
{
//判断是否选择了文件
if (this.FileUpload1.HasFile)
{
try{
//获取文件
HttpPostedFile filename = this.FileUpload1.PostedFile;
//判断文件大小,字节为单位
if (filename.ContentLength>0)
{
FileUpload1.SaveAs(@"D:\" + FileUpload1.FileName);
s_alert("上传成功");
}
}
catch (Exception ex)
{
s_alert ("上传失败,出现错误");
}
}
else
{
s_alert("你还没有选择文件");
}
}
public static void s_alert(string str)
{
Page pa ......
对“添加”、“提交”、“保存”、“更新”等按钮需要对数据库进行写操作的按钮,一定要在页面初始化时加载脚本,防止多次重复点击,例如:
protected void Page_Load(object sender, EventArgs e)
{
//.net 2.0以上
Button1.Attributes.Add("onclick", "this.disabled=true;" + this.ClientScript.GetPostBackEventReference(Button1, ""));
}
测试页面:
为了测试,我们可以建立一个页面,加入一个btnAdd按钮
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="AddAndEditUser.aspx.cs" Inherits="AddUser" %><html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>添加和编辑页面示例</title>
</head>
<body>
<form id="frmMain" runat="server">
<asp:Button ID="btnAdd" runat="server" CssClass="INPUT-BUTTON-Save" OnClick="btnAdd_Click">
......
方法一:
HtmlProxy.cs:
using System.Text;
using System.IO;
/// <summary>
/// HtmlProxy 的摘要说明
/// </summary>
public class HtmlProxy
{
public HtmlProxy()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public static bool ChangeFile(int id)
{
string filename = HttpContext.Current.Server.MapPath("Post_" + id + ".html");
//尝试读取已有文件
Stream st = GetFileStream(filename);
//如果文件存在并且读取成功
if (st != null) //文件存在
& ......
上次我们说到的编译成的dll文件与cs文件在同一目录中,而不会放到虚拟目录的bin目录中,如何做才能够把cs
文件编译成dll且自动放到虚拟目录的bin文件夹中呢?
开始-------程序-------Microsoft Visual Studio.NET 2003-------Visual Studio.NET工具,点击其中的“Visual Studio.NET2003命令提示”,就会进入Microsoft Visual Studio.NET 2003命令提示窗口,然后我们用dos命令(cd)进入要编译成dll的cs文件所在的目录,然后输入命令:
csc /out: bin\index.dll /t:library index.cs
回车,就会在bin目录下生成与cs文件同名的dll文件
但是如果这个cs文件引用了bin目录下的另外一个dll文件如comman.dll,则应该这样输入命令:
csc /out: bin\index.dll /r: bin\comman.dll /t:library index.cs
给出我自己的一个编译实例:
首先进入Microsoft Visual Studio.NET 2003命令提示窗口,在命令行下切换到c盘下(我把MouseGridView.cs放在C盘下面)
执行
C:\>csc /out:MouseOver.dll /t:library MouseGridView.cs
在安装了Microsoft.NET Framework的操作系统上,我们可以在Windows所在目录下找到Microsoft.NET目录。在这个目录下面提供了C#的编译器,CSC.EXE
......
一、(单值绑定)在页面的后台代码中定义公有变量,如下:
public string gongYou = "声明的公有成员";
①然后在页面的源中调用,如下:
<asp:Label ID="lblMgs" runat="server" Text="<%#gongYou >"></asp:Label>
②当然最后要记得绑定数据:
protected void Page_Load(object sender, EventArgs e)
{
Page.DataBind();
}
注:<%# %>作用是在前台显示代码中嵌入访问后台数据的表达式来完成数据绑定。
二、列表控件的数据绑定
介绍一下DropDownList,CheckBox,BulletedList;
一般来说,实现IEnumerable,IListSource,IDataSource和IHierarchicalDataSource的类都可以作为数据源。
例:页面代码:
<div>
<asp:DropDownList ID="ddlSelect" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddlSelect_SelectedIndexChanged"
Width="158px">
</asp:DropDownList>
<asp:TextBox ID="txtDDL" runat= ......