首页 > 代码库 > Application对象、ViewState对象、分页展示--2017年1月4日
Application对象、ViewState对象、分页展示--2017年1月4日
Application对象
存储 Application 变量 Application["application名称"] = "application的值";
取回 Application 变量 string str = Application["application名称"];
常用的属性和方法
| All | 返回全部的Application对象变量到一个对象数组 |
AllKeys | 返回全部的Application对象变量到一个字符串数组 | |
Count | 取得Application中对象变量的数量 | |
Item | Application变量名称传回的内容值 | |
Add | 新增一个Application变量值 | |
Clear | 清空全部Application变量值 | |
Get | 变量名传回的变量值 | |
Set | 更新Application变量值 | |
Lock | 锁定所有Application变量值 | |
UnLock | 解除锁定Application变量 |
Application对象特性:
存储的物理位置:服务器内存。
存储的类型限制:任意类型。
状态使用的范围:整个应用程序。
存储的大小限制:任意大小。
生命周期:应用程序开始的时候创建(准确来说是用户第一次请求某URL的时候创建),应用程序结束的时候销毁。
安全与性能:数据总是存储在服务端,安全性比较高,但不易存储过多数据。
优缺点与注意事项:检索数据速度快,但缺乏自我管理机制,数据不会自动释放。
ViewState:
用于记录页面的一些Value,
GET:URL?表单元素Name=表单元素Value&表单元素Name=表单元素Value
webform中微软已经给我们做好了这个对象,自带此功能。
分页显示
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Index.aspx.cs" Inherits="Index" %><!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> <link rel="stylesheet" href=http://www.mamicode.com/"css/goupiao.css" type="text/css" /> <link rel="stylesheet" href=http://www.mamicode.com/"css/bootstrap.min.css" /> <link rel="stylesheet" href=http://www.mamicode.com/"css/font-awesome.css" /></head><body> <div class="content-bg"></div> <div class="bg-overlay"></div> <div class="main-posts" style="top: 38px;"> <div class="container" style="padding: 0px; box-shadow: 1px 2px 3px #000; background-color: white;"> <div style="height: 200px; background-color: white; text-align: center; line-height: 180px; font-size: 60px; background-color: white; background-image: url(images/1.png); background-size: 70%; background-repeat: no-repeat; background-position: center"> </div> <div style="height: 20px; text-align: right;">Low爆了的字</div> </div> </div> <div class="main-posts" style="top: 38px;"> <div class="container"> <form id="form1" runat="server"> <div> <div style="height: 60px; text-align: center; position: relative"> <div style="position: relative; color: white; float: left; height: 50px; line-height: 50px;"> <asp:Button ID="luru1" runat="server" Text="添加员工" Style="display: none;" /> <asp:Button ID="luru2" runat="server" Text="上架物品" Style="display: none;" /> <div runat="server" class="luru luru1" onclick="onc1()"> 添加员工 </div> <div runat="server" class="luru luru2" onclick="onc2()"> 上架物品 </div> <div runat="server" class="zhuang" style="background-color: orange;margin-left:120px;"> </div> <div runat="server" class="zhuangg"> 过期 </div> <div runat="server" class="zhuang" style="background-color: red;"> </div> <div runat="server" class="zhuangg"> 缺货 </div> <div runat="server" class="zhuang"> </div> <div runat="server" class="zhuangg"> 正常 </div> </div> <div style="position: relative; color: white; float: right; width: 300px; height: 50px; line-height: 50px;"> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> <asp:LinkButton ID="LinkButton1" runat="server">退出登陆</asp:LinkButton> </div> </div> <asp:Repeater ID="Repeater1" runat="server"> <HeaderTemplate> <table id="tit"> <tr> <td>编号</td> <td>货物名称</td> <td>销售价格</td> <td>进货价格</td> <td>库存数量</td> <td>进货日期</td> <td>保质期</td> <td>操作</td> </tr> </HeaderTemplate> <ItemTemplate> <tr class="ttt" style="<%#Eval("Warnings") %><%#Eval("EDatestyle") %>"> <td><%#Eval("Ids") %></td> <td><%#Eval("Name") %></td> <td><%#Eval("Price") %></td> <td><%#Eval("Bid") %></td> <td><%#Eval("Stock") %></td> <td><%#Eval("PDatestr") %></td> <td><%#Eval("EDatestr") %></td> <td> <div class="choosetwo"> <a class="fa fa-shopping-cart aa" href=http://www.mamicode.com/"ADDnum.aspx?UserName=<%= Request["UserName"] %>&Ids=<%#Eval("Ids") %>">进货</a> <a class="fa fa-gears aa" href=http://www.mamicode.com/"Updata.aspx?UserName=<%= Request["UserName"] %>&Ids=<%#Eval("Ids") %>">修改</a> <a class="fa fa-level-down aa" href=http://www.mamicode.com/"Delete.aspx?UserName=<%= Request["UserName"] %>&Ids=<%#Eval("Ids") %>">下架</a> </div> </td> </tr> </ItemTemplate> <%-- <AlternatingItemTemplate> <tr style="<%#Eval("Warnings") %><%#Eval("EDatestyle") %> font-size:15px;font-weight:bold;"> <td><%#Eval("Ids") %></td> <td><%#Eval("Name") %></td> <td><%#Eval("Price") %></td> <td><%#Eval("Bid") %></td> <td><%#Eval("Stock") %></td> <td><%#Eval("PDatestr") %></td> <td><%#Eval("EDatestr") %></td> <td> <div class="choosetwo"> <a class="fa fa-shopping-cart aa" onclick="xcz(2)">进货</a> <a class="fa fa-gears aa" onclick="xcz(2)">修改</a> <a class="fa fa-level-down aa" onclick="xcz(2)">下架</a> </div> </td> </tr> </AlternatingItemTemplate>--%> <FooterTemplate> </table> </FooterTemplate> </asp:Repeater> </div> <div style="color:#FFF">当前第【<asp:Label ID="Label_NowPage" runat="server" Text="Label"></asp:Label>】页 共【<asp:Label ID="Label_MaxPage" runat="server" Text="Label"></asp:Label>】页 <asp:LinkButton ID="btn_first" runat="server">首页</asp:LinkButton> <asp:LinkButton ID="btn_prev" runat="server">上一页</asp:LinkButton> <asp:LinkButton ID="btn_next" runat="server">下一页</asp:LinkButton> <asp:LinkButton ID="btn_last" runat="server">末页</asp:LinkButton> <asp:DropDownList ID="DropDownList1" AutoPostBack="true" runat="server"></asp:DropDownList> <asp:Button ID="Button1" runat="server" Text="跳转" /> </div> </form> </div> </div></body></html><script type="text/javascript"> function onc1() { document.getElementById(‘luru1‘).click(); } function onc2() { document.getElementById(‘luru2‘).click(); }</script>
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;public partial class Index : System.Web.UI.Page{ int Count = 5; //每页显示条数 public int MaxPageNumber() { List<Commodity> clist = new CommodityData().Select(); double end2 = Math.Ceiling(clist.Count / (Count * 1.0)); int end = Convert.ToInt32(end2); return end; } protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { Label_NowPage.Text = "1"; Label_MaxPage.Text = MaxPageNumber().ToString(); Repeater1.DataSource = new CommodityData().Select(Count, 1); Repeater1.DataBind(); pandin(); int max = MaxPageNumber(); DropDownList1.Items.Clear(); for (int i = 1; i <= max; i++) { DropDownList1.Items.Add(new ListItem(i.ToString(), i.ToString())); } } #region 地址栏传值:取值 if (true) { //string un = Request["UserName"];//地址栏传值:取值 //if (!String.IsNullOrEmpty(un)) //{ // Users u = new UsersData().select(un); // Label1.Text = "ssssssssssssssssss"; // if (u != null) // { // Label1.Text = u.PName + ",欢迎回来!"; // } // else // { // Response.Redirect("Login.aspx"); // } //} //else //{ // Response.Redirect("Login.aspx"); //} } #endregion #region Session全局传值:取值 if (true) { //if (Session["UserName"] != null) //{ // string un = Session["UserName"].ToString();//Session全局传值 // Users u = new UsersData().select(un); // if (u != null) // { // Label1.Text = u.PName + ",欢迎回来!"; // } // else // { // Response.Redirect("Login.aspx"); // } //} //else //{ // Response.Redirect("Login.aspx"); //} } #endregion #region cookie全局传值:取值 if (true) { if (Request.Cookies["UserName"] != null) { string un = Request.Cookies["UserName"].Value;//Session全局传值 Users u = new UsersData().select(un); if (u != null) { Label1.Text = u.PName + ",欢迎回来!"; } else { Response.Redirect("Login.aspx"); } } else { Response.Redirect("Login.aspx"); } } #endregion //Repeater1.DataSource = new CommodityData().Select(); //Repeater1.DataBind(); LinkButton1.Click += LinkButton1_Click; luru2.Click += luru2_Click; DropDownList1.SelectedIndexChanged += Button1_Click; btn_first.Click += btn_first_Click;//首页按钮 btn_prev.Click += btn_prev_Click; btn_next.Click += btn_next_Click; btn_last.Click += btn_last_Click; Button1.Click += Button1_Click; } void Button1_Click(object sender, EventArgs e) { int nextPage = Convert.ToInt32(DropDownList1.SelectedValue); Repeater1.DataSource = new CommodityData().Select(Count, nextPage); Repeater1.DataBind(); Label_NowPage.Text = nextPage.ToString(); pandin(); } void btn_first_Click(object sender, EventArgs e) { Repeater1.DataSource = new CommodityData().Select(Count, 1); Repeater1.DataBind(); Label_NowPage.Text = "1"; pandin(); } void btn_prev_Click(object sender, EventArgs e) { int nextPage = Convert.ToInt32(Label_NowPage.Text) - 1; Repeater1.DataSource = new CommodityData().Select(Count, nextPage); Repeater1.DataBind(); Label_NowPage.Text = nextPage.ToString(); pandin(); } void btn_next_Click(object sender, EventArgs e) { int nextPage = Convert.ToInt32(Label_NowPage.Text) + 1; Repeater1.DataSource = new CommodityData().Select(Count, nextPage); Repeater1.DataBind(); Label_NowPage.Text = nextPage.ToString(); pandin(); } void btn_last_Click(object sender, EventArgs e) { int hehe = MaxPageNumber(); Repeater1.DataSource = new CommodityData().Select(Count, hehe); Repeater1.DataBind(); Label_NowPage.Text = hehe.ToString(); pandin(); } void luru2_Click(object sender, EventArgs e) { //string un = Request["UserName"];//地址栏传值:取值 //string un = Session["UserName"].ToString();//Session全局传值 string un = Request.Cookies["UserName"].Value;//cookie全局传值 Response.Redirect("ADDComm.aspx?UserName=" + un); } void pandin() { int NowPage = Convert.ToInt32(Label_NowPage.Text); int MaxPage=Convert.ToInt32(Label_MaxPage.Text); if (MaxPage==1) { DropDownList1.Enabled = false; btn_first.Enabled = false; btn_prev.Enabled = false; btn_next.Enabled = false; btn_last.Enabled = false; Button1.Enabled = false; } else if (NowPage >= MaxPage) { DropDownList1.Enabled = true; btn_first.Enabled = true; btn_prev.Enabled = true; btn_next.Enabled = false; btn_last.Enabled = false; Button1.Enabled = true; } else if (NowPage <= 1) { DropDownList1.Enabled = true; btn_first.Enabled = false; btn_prev.Enabled = false; btn_next.Enabled = true; btn_last.Enabled = true; Button1.Enabled = true; }else { DropDownList1.Enabled = true; btn_first.Enabled = true; btn_prev.Enabled = true; btn_next.Enabled = true; btn_last.Enabled = true; Button1.Enabled = true; } } void LinkButton1_Click(object sender, EventArgs e) { //Session.Contents.Remove("UserName"); Response.Redirect("Login.aspx"); }}
using System;using System.Collections.Generic;using System.Data.SqlClient;using System.Linq;using System.Web;/// <summary>/// CommodityData 的摘要说明/// </summary>public class CommodityData{ SqlConnection conn = null; SqlCommand cmd = null; public CommodityData() { conn = new SqlConnection("server=.;database=WareHouse;user=sa;pwd=0928"); cmd = conn.CreateCommand(); } /// <summary> /// 查找所有数据 /// </summary> /// <returns></returns> public List<Commodity> Select() { List<Commodity> list = new List<Commodity>(); cmd.CommandText = "select *from Commodity"; conn.Open(); SqlDataReader dr = cmd.ExecuteReader(); if (dr.HasRows) { while (dr.Read()) { Commodity u = new Commodity(); u.Ids = (int)dr[0]; u.Name = (string)dr[1]; u.Price = (decimal)dr[2]; u.Bid = (decimal)dr[3]; u.Stock = (int)dr[4]; u.Warning = (int)dr[5]; u.PDate = (DateTime)dr[6]; u.EDate = (int)dr[7]; list.Add(u); } } conn.Close(); return list; } /// <summary> /// 查找某几条数据 /// </summary> /// <returns></returns> public List<Commodity> Select(int count, int NowPage) { List<Commodity> list = new List<Commodity>(); cmd.CommandText = "select top " + count + " *from Commodity where ids not in(select top " + ((NowPage - 1) * count) + " Ids from Commodity)"; conn.Open(); SqlDataReader dr = cmd.ExecuteReader(); if (dr.HasRows) { while (dr.Read()) { Commodity u = new Commodity(); u.Ids = (int)dr[0]; u.Name = (string)dr[1]; u.Price = (decimal)dr[2]; u.Bid = (decimal)dr[3]; u.Stock = (int)dr[4]; u.Warning = (int)dr[5]; u.PDate = (DateTime)dr[6]; u.EDate = (int)dr[7]; list.Add(u); } } conn.Close(); return list; } /// <summary> /// 根据ID查找 /// </summary> /// <param name="Uname"></param> /// <returns></returns> public Commodity select(string Uname) { Commodity u = null; cmd.CommandText = "select * from Commodity where Ids=@a"; cmd.Parameters.Clear(); cmd.Parameters.AddWithValue("@a", Uname); conn.Open(); SqlDataReader dr = cmd.ExecuteReader(); if (dr.HasRows) { dr.Read(); u = new Commodity(); u.Ids = (int)dr[0]; u.Name = (string)dr[1]; u.Price = (decimal)dr[2]; u.Bid = (decimal)dr[3]; u.Stock = (int)dr[4]; u.Warning = (int)dr[5]; u.PDate = (DateTime)dr[6]; u.EDate = (int)dr[7]; } conn.Close(); return u; } /// <summary> /// 执行TSQL语句 /// </summary> /// <param name="str"></param> /// <returns></returns> public int zhixing(string str) { conn.Open(); try { cmd.CommandText = str; int i = cmd.ExecuteNonQuery(); conn.Close(); return i; } catch (Exception ex) { conn.Close(); return 0; } } /// <summary> /// 插入数据 /// </summary> /// <param name="u"></param> /// <returns></returns> public bool ins(Commodity u) { cmd.Parameters.Clear(); cmd.Parameters.AddWithValue("@UserName", u.Name); cmd.Parameters.AddWithValue("@pwd", u.Price); cmd.Parameters.AddWithValue("@nick", u.Bid); cmd.Parameters.AddWithValue("@sex", u.Stock); cmd.Parameters.AddWithValue("@bir", u.Warning); cmd.Parameters.AddWithValue("@nation", u.PDate); cmd.Parameters.AddWithValue("@cla", u.EDate); if (zhixing("INSERT INTO Commodity VALUES(@UserName,@pwd,@nick,@sex,@bir,@nation,@cla)") > 0) { return true; } else { return false; } }}
Application对象、ViewState对象、分页展示--2017年1月4日