weibform中Application、ViewState对象和分页

Application:

  全局公共变量组

  存放位置:服务器

  特点:所有访问用户都是访问同一个变量,但只要服务器不停机,变量一直存在于服务器的内存中,不要使用循环大量的创建Application对象,可能会造

     成服务器崩溃。

  生命周期:永久,只要服务器不停机

  使用方法:与Session一样

ViewState:

  用于记录页面的一些状态,就像人的病例,比如当执行提交后,网页可以保留一些已经输入的文本框而非清空

  webform中微软已经给我们做好了这个对象,自带此功能。

webform中一共6个内置对象:Request  Response  Session  Cookie Application ViewState

分页:

  为了用户查看以及页面加载速度,查看大量数据时需要进行分页操作, 

  主要通过查询语句以及各种判断来实现

例如:食品信息的展示

界面代码:

 1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
 2 
 3 <!DOCTYPE html>
 4 
 5 <html xmlns="http://www.w3.org/1999/xhtml">
 6 <head id="Head1" runat="server">
 7     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 8     <title></title>
 9 </head>
10 <body>
11     <form id="form1" runat="server">
12         <div>
13 
14             <asp:Repeater ID="Repeater1" runat="server">
15                 <HeaderTemplate>
16 
17                     <table style="width: 100%; text-align: center; background-color: navy;">
18                         <thead>
19                             <tr style="color: white;">
20                                 <%--<td>ids</td>--%>
21                                 <td>食品编号</td>
22                                 <td>食品名称</td>
23                                 <td>食品价格</td>
24                                 <td>食品制造商</td>
25                             </tr>
26                         </thead>
27                         <tbody>
28                 </HeaderTemplate>
29 
30                 <ItemTemplate>
31 
32                     <tr style="background-color: white;">
33                         <%--<td><%#Eval("ids") %></td>--%>
34                         <td><%#Eval("ids") %></td>
35                         <td><%#Eval("foodname") %></td>
36                         <td><%#Eval("foodprice") %></td>
37                         <td><%#Eval("foodmade") %></td>
38                         
39                     </tr>
40 
41                 </ItemTemplate>
42                 <FooterTemplate>
43                     </tbody>
44             </table>
45                 </FooterTemplate>
46             </asp:Repeater>
47 
48             当前第【<asp:Label ID="Label_NowPage" runat="server" Text="Label"></asp:Label>】页
49             &nbsp;&nbsp;共【<asp:Label ID="Label_MaxPage" runat="server" Text="Label"></asp:Label>】页&nbsp;&nbsp;
50             <asp:LinkButton ID="btn_first" runat="server">首页</asp:LinkButton>
51             &nbsp;&nbsp;
52             <asp:LinkButton ID="btn_prev" runat="server">上一页</asp:LinkButton>
53             &nbsp;&nbsp;
54             <asp:LinkButton ID="btn_next" runat="server">下一页</asp:LinkButton>
55             &nbsp;&nbsp;
56             <asp:LinkButton ID="btn_last" runat="server">末页</asp:LinkButton>
57             &nbsp;&nbsp;
58             <asp:DropDownList ID="DropDownList1" AutoPostBack="true" runat="server"></asp:DropDownList>
59             <asp:Button ID="Button1" runat="server" Text="跳转" />
60 
61 
62         </div>
63     </form>
64 </body>
65 </html>
界面

实体类:

  1 using System;
  2 using System.Collections;
  3 using System.Collections.Generic;
  4 using System.Data.SqlClient;
  5 using System.Linq;
  6 using System.Web;
  7 
  8 /// <summary>
  9 /// foodData 的摘要说明
 10 /// </summary>
 11 public class foodData
 12 {
 13     SqlConnection conn = null;
 14     SqlCommand cmd = null;
 15 
 16     public foodData()
 17     {
 18         conn = new SqlConnection("server=.;database=Data0216;user=sa;pwd=123");
 19         cmd = conn.CreateCommand();
 20     }
 21 
 22     public List<food> SelectAll()
 23     {
 24         List<food> clist = new List<food>();
 25         cmd.CommandText = "select *from food";
 26         conn.Open();
 27         SqlDataReader dr = cmd.ExecuteReader();
 28         while (dr.Read())
 29         {
 30             food c = new food();
 31             c.Ids = Convert.ToInt32(dr["ids"]);
 32             c.foodname = dr["foodname"].ToString();
 33             c.foodprice = Convert.ToDecimal(dr["foodprice"]);
 34             c.foodmade = dr["foodmade"].ToString();
 35             clist.Add(c);
 36         }
 37         conn.Close();
 38         return clist;
 39     }
 40 
 41 
 42     public List<food> Select(int pcount, int pnumber)
 43     {
 44         List<food> clist = new List<food>();
 45         cmd.CommandText = "select top " + pcount + " * from food where ids not in(select top " + (pcount * (pnumber - 1)) + " ids from food)";
 46         conn.Open();
 47         SqlDataReader dr = cmd.ExecuteReader();
 48         while (dr.Read())
 49         {
 50             food c = new food();
 51             c.Ids = Convert.ToInt32(dr["ids"]);
 52             c.foodname = dr["foodname"].ToString();
 53             c.foodprice = Convert.ToDecimal(dr["foodprice"]);
 54             c.foodmade = dr["foodmade"].ToString();
 55             clist.Add(c);
 56         }
 57         conn.Close();
 58         return clist;
 59     }
 60 
 61 
 62     public int SelectCount()
 63     {
 64         int a = 0;
 65         cmd.CommandText = "select count(*) from food";
 66         conn.Open();
 67         SqlDataReader dr = cmd.ExecuteReader();
 68         dr.Read();
 69         a = Convert.ToInt32(dr[0]);
 70         conn.Close();
 71         return a;
 72     }
 73 
 74 
 75 
 76     public List<food> SelectAll(string tsql, Hashtable hh)
 77     {
 78         List<food> clist = new List<food>();
 79         cmd.CommandText = tsql;
 80         cmd.Parameters.Clear();
 81         foreach(string s in hh.Keys)
 82         {
 83             cmd.Parameters.Add(s, hh[s]);
 84         }
 85         conn.Open();
 86         SqlDataReader dr = cmd.ExecuteReader();
 87         while (dr.Read())
 88         {
 89             food c = new food();
 90             c.Ids = Convert.ToInt32(dr["ids"]);
 91             c.foodname = dr["foodname"].ToString();
 92             c.foodprice = Convert.ToDecimal(dr["foodprice"]);
 93             c.foodmade = dr["foodmade"].ToString();
 94             clist.Add(c);
 95         }
 96         conn.Close();
 97         return clist;
 98     }
 99 
100 }
实体类

后台代码:

  1 using System;
  2 using System.Collections.Generic;
  3 using System;
  4 using System.Collections.Generic;
  5 using System.Linq;
  6 using System.Web;
  7 using System.Web.UI;
  8 using System.Web.UI.WebControls;
  9 
 10 public partial class _Default : System.Web.UI.Page
 11 {
 12     int PageCount = 5; //每页显示条数
 13 
 14     protected void Page_Load(object sender, EventArgs e)
 15     {
 16         if (!IsPostBack)
 17         {
 18             Label_NowPage.Text = "1";
 19             Label_MaxPage.Text = MaxPageNumber().ToString();
 20             btn_prev.Enabled = false;
 21             btn_first.Enabled = false;
 22 
 23             Repeater1.DataSource = new foodData().Select(PageCount, 1);
 24             Repeater1.DataBind();
 25 
 26 
 27             int max = MaxPageNumber();
 28             DropDownList1.Items.Clear();
 29             for (int i = 1; i <= max; i++)
 30             {
 31                 DropDownList1.Items.Add(new ListItem(i.ToString(), i.ToString()));
 32             }
 33         }
 34 
 35 
 36 
 37         DropDownList1.SelectedIndexChanged += Button1_Click;
 38         btn_first.Click += btn_first_Click;//首页按钮
 39         btn_prev.Click += btn_prev_Click;
 40         btn_next.Click += btn_next_Click;
 41         btn_last.Click += btn_last_Click;
 42         Button1.Click += Button1_Click;
 43     }
 44 
 45     void Button1_Click(object sender, EventArgs e)
 46     {
 47         //获取当前页,计算要看的下一页的页号
 48         int nextPage = Convert.ToInt32(DropDownList1.SelectedValue);
 49 
 50         //按照获取的页数绑定相应的数据
 51         Repeater1.DataSource = new foodData().Select(PageCount, nextPage);
 52         Repeater1.DataBind();
 53 
 54         //修改显示页数
 55         Label_NowPage.Text = nextPage.ToString();
 56 
 57         if (nextPage == 1)
 58         {
 59             btn_first.Enabled = false;
 60             btn_prev.Enabled = false;
 61             btn_next.Enabled = true;
 62             btn_last.Enabled = true;
 63         }
 64         else if (nextPage == MaxPageNumber())
 65         {
 66             btn_first.Enabled = true;
 67             btn_prev.Enabled = true;
 68             btn_next.Enabled = false;
 69             btn_last.Enabled = false;
 70         }
 71         else
 72         {
 73             btn_first.Enabled = true;
 74             btn_prev.Enabled = true;
 75             btn_next.Enabled = true;
 76             btn_last.Enabled = true;
 77         }
 78     }
 79 
 80     void btn_first_Click(object sender, EventArgs e)
 81     {
 82         //按照获取的页数绑定相应的数据
 83         Repeater1.DataSource = new foodData().Select(PageCount, 1);
 84         Repeater1.DataBind();
 85 
 86         //修改显示页数
 87         Label_NowPage.Text = "1";
 88 
 89 
 90         btn_first.Enabled = false;
 91         btn_prev.Enabled = false;
 92         btn_next.Enabled = true;
 93         btn_last.Enabled = true;
 94     }
 95 
 96     void btn_prev_Click(object sender, EventArgs e)
 97     {
 98         //获取当前页,计算要看的下一页的页号
 99         int nextPage = Convert.ToInt32(Label_NowPage.Text) - 1;
100 
101         //按照获取的页数绑定相应的数据
102         Repeater1.DataSource = new foodData().Select(PageCount, nextPage);
103         Repeater1.DataBind();
104 
105         //修改显示页数
106         Label_NowPage.Text = nextPage.ToString();
107 
108         if (nextPage <= 1)
109         {
110             btn_first.Enabled = false;
111             btn_prev.Enabled = false;
112         }
113         btn_next.Enabled = true;
114         btn_last.Enabled = true;
115     }
116 
117     void btn_next_Click(object sender, EventArgs e)
118     {
119         //获取当前页,计算要看的下一页的页号
120         int nextPage = Convert.ToInt32(Label_NowPage.Text) + 1;
121 
122         ////判断是否有下一页
123         //if (nextPage > MaxPageNumber())
124         //{
125         //    return;
126         //}
127 
128         //按照获取的页数绑定相应的数据
129         Repeater1.DataSource = new foodData().Select(PageCount, nextPage);
130         Repeater1.DataBind();
131 
132         //修改显示页数
133         Label_NowPage.Text = nextPage.ToString();
134 
135         if (nextPage >= MaxPageNumber())
136         {
137             btn_next.Enabled = false;
138             btn_last.Enabled = false;
139         }
140         btn_prev.Enabled = true;
141         btn_first.Enabled = true;
142     }
143 
144     void btn_last_Click(object sender, EventArgs e)
145     {
146         int hehe = MaxPageNumber();
147 
148         //按照获取的页数绑定相应的数据
149         Repeater1.DataSource = new foodData().Select(PageCount, hehe);
150         Repeater1.DataBind();
151 
152         //修改显示页数
153         Label_NowPage.Text = hehe.ToString();
154 
155 
156         btn_prev.Enabled = true;
157         btn_first.Enabled = true;
158         btn_next.Enabled = false;
159         btn_last.Enabled = false;
160     }
161 
162 
163     public int MaxPageNumber()
164     {
165         int a = 0;
166         int maxcount = new foodData().SelectCount();
167         decimal d = Convert.ToDecimal(maxcount) / PageCount;
168         a = Convert.ToInt32(Math.Ceiling(d));
169         return a;
170     }
171 }
后台代码

 

posted on 2017-05-23 15:57  朱利军  阅读(162)  评论(0编辑  收藏  举报