当前位置:K88软件开发文章中心编程语言.NETASP.NET → 文章内容

ASP.NET 管理状态

减小字体 增大字体 作者:佚名  来源:网上搜集  发布时间:2019-1-23 13:58:00

dtd"><html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title> Untitled Page </title> </head> <body> <form id="form1" runat="server"> <div>       <table style="width: 568px; height: 103px"> <tr> <td style="width: 209px"> <asp:Label ID="lblstr" runat="server" Text="Enter a String" style="width:94px"> </asp:Label> </td> <td style="width: 317px"> <asp:TextBox ID="txtstr" runat="server" style="width:227px"> </asp:TextBox> </td> </tr> <tr> <td style="width: 209px"> </td> <td style="width: 317px"> </td> </tr> <tr> <td style="width: 209px"> <asp:Button ID="btnnrm" runat="server" Text="No action button" style="width:128px" /> </td> <td style="width: 317px"> <asp:Button ID="btnstr" runat="server" Text="Submit the String" /> </td> </tr> <tr> <td style="width: 209px"> </td> <td style="width: 317px"> </td> </tr> <tr> <td style="width: 209px"> <asp:Label ID="lblsession" runat="server" style="width:231px" > </asp:Label> </td> <td style="width: 317px"> </td> </tr> <tr> <td style="width: 209px"> <asp:Label ID="lblshstr" runat="server"> </asp:Label> </td> <td style="width: 317px"> </td> </tr> </table> </div> </form> </body></html>在设计视图中应有如下显示:后台代码如下:public partial class _Default : System.Web.UI.Page { String mystr; protected void Page_Load(object sender, EventArgs e) { this.lblshstr.Text = this.mystr; this.lblsession.Text = (String)this.Session["str"]; } protected void btnstr_Click(object sender, EventArgs e) { this.mystr = this.txtstr.Text; this.Session["str"] = this.txtstr.Text; this.lblshstr.Text = this.mystr; this.lblsession.Text = (String)this.Session["str"]; }}执行文件并观察其如何运行:应用程序状态ASP.NET 应用程序是在 Web 服务器上所有网页,代码和单个虚拟目录的其他文件的集合。当信息被存储在应用程序状态,它可以供所有用户使用。为了提供应用程序状态的使用,ASP.NET 从 HttpApplicationState 类中为每个应用程序创建一个应用程序状态对象,并将该对象存储在服务器内存中。该对象是由类文件 global.asax 表示。应用程序状态主要被用于存储计数器,其他统计数据及税率,折扣率等所有应用程序数据,并存储用户访问网站的路径。HttpApplicationState 类具有以下属性:属性描述Item(name)具有指定名称的应用程序项的值,是 HttpApplicationState 的默认属性。Count应用程序状态集合中项的数量。HttpApplicationState 类具有以下方法:方法描述Add(name, value)添加新的项目到应用程序状态集合。Clear移除应用程序状态集合中的所有项。Remove(name)移除应用程序状态集合中的指定项。RemoveAll移除一个 HttpApplicationState 集合中所有对象。RemoveAt移除从由索引找到的集合中的一个 HttpApplicationState 对象。Lock()锁定应用程序状态集合以便只有当前用户可以访问。Unlock()解锁应用程序状态集合以便所有用户可以访问。应用程序状态的数据通常是由为事件编写的处理程序维护:应用程序开启应用程序结束应用程序错误会话开始会话结束以下代码片段展示了用于存储应用程序状态信息的基本语法:Void Application_Start(object sender, EventArgs e){ Application["startMessage"] = "The application has started.";}Void Application_End(object sender, EventArgs e){ Application["endtMessage"] = "The application has ended.";}

上一页  [1] [2] 


ASP.NET 管理状态