Application:
全局公共变量组
存放位置:服务器
特点:所有访问用户都是访问同一个变量,但只要服务器不停机,变量一直存在于服务器的内存中,不要使用循环大量的创建Application对象,可能会造
成服务器崩溃。
生命周期:永久,只要服务器不停机
使用方法:与Session一样
ViewState:
用于记录页面的一些状态,就像人的病例,比如当执行提交后,网页可以保留一些已经输入的文本框而非清空
webform中微软已经给我们做好了这个对象,自带此功能。
webform中一共6个内置对象:Request Response Session Cookie Application ViewState
分页:
为了用户查看以及页面加载速度,查看大量数据时需要进行分页操作,
主要通过查询语句以及各种判断来实现
例:分页展示车辆信息
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!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> </head> <body> <form id="form1" runat="server"> <div> <asp:Repeater ID="Repeater1" runat="server"> <HeaderTemplate> <table style=" 100%; text-align: center; background-color: navy;"> <thead> <tr style="color: white;"> <%--<td>ids</td>--%> <td>code</td> <td>name</td> <td>brand</td> <td>time</td> <td>oil</td> <td>powers</td> <td>exhaust</td> <td>price</td> <td>pic</td> </tr> </thead> <tbody> </HeaderTemplate> <ItemTemplate> <tr style="background-color: white;"> <%--<td><%#Eval("ids") %></td>--%> <td><%#Eval("code") %></td> <td><%#Eval("name") %></td> <td><%#Eval("brand") %></td> <td><%#Eval("time") %></td> <td><%#Eval("oil") %></td> <td><%#Eval("powers") %></td> <td><%#Eval("exhaust") %></td> <td><%#Eval("price") %></td> <td><%#Eval("pic") %></td> </tr> </ItemTemplate> <FooterTemplate> </tbody> </table> </FooterTemplate> </asp:Repeater> 当前第【<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> </body> </html>
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class _Default : System.Web.UI.Page { int Count = 5; //每页显示条数 protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { Label_NowPage.Text = "1"; Label_MaxPage.Text = MaxPageNumber().ToString(); btn_prev.Enabled = false; btn_first.Enabled = false; Repeater1.DataSource = new CarData().Select(Count, 1); Repeater1.DataBind(); int max = MaxPageNumber(); DropDownList1.Items.Clear(); for (int i = 1; i <= max; i++) { DropDownList1.Items.Add(new ListItem(i.ToString(), i.ToString())); } } 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 CarData().Select(Count, nextPage); Repeater1.DataBind(); //修改显示页数 Label_NowPage.Text = nextPage.ToString(); if (nextPage == 1) { btn_first.Enabled = false; btn_prev.Enabled = false; btn_next.Enabled = true; btn_last.Enabled = true; } else if (nextPage == MaxPageNumber()) { btn_first.Enabled = true; btn_prev.Enabled = true; btn_next.Enabled = false; btn_last.Enabled = false; } else { btn_first.Enabled = true; btn_prev.Enabled = true; btn_next.Enabled = true; btn_last.Enabled = true; } } void btn_first_Click(object sender, EventArgs e) { //按照获取的页数绑定相应的数据 Repeater1.DataSource = new CarData().Select(Count, 1); Repeater1.DataBind(); //修改显示页数 Label_NowPage.Text = "1"; btn_first.Enabled = false; btn_prev.Enabled = false; btn_next.Enabled = true; btn_last.Enabled = true; } void btn_prev_Click(object sender, EventArgs e) { //获取当前页,计算要看的下一页的页号 int nextPage = Convert.ToInt32(Label_NowPage.Text) - 1; //按照获取的页数绑定相应的数据 Repeater1.DataSource = new CarData().Select(Count, nextPage); Repeater1.DataBind(); //修改显示页数 Label_NowPage.Text = nextPage.ToString(); if (nextPage <= 1) { btn_first.Enabled = false; btn_prev.Enabled = false; } btn_next.Enabled = true; btn_last.Enabled = true; } void btn_next_Click(object sender, EventArgs e) { //获取当前页,计算要看的下一页的页号 int nextPage = Convert.ToInt32(Label_NowPage.Text) + 1; ////判断是否有下一页 //if (nextPage > MaxPageNumber()) //{ // return; //} //按照获取的页数绑定相应的数据 Repeater1.DataSource = new CarData().Select(Count, nextPage); Repeater1.DataBind(); //修改显示页数 Label_NowPage.Text = nextPage.ToString(); if (nextPage >= MaxPageNumber()) { btn_next.Enabled = false; btn_last.Enabled = false; } btn_prev.Enabled = true; btn_first.Enabled = true; } void btn_last_Click(object sender, EventArgs e) { int hehe = MaxPageNumber(); //按照获取的页数绑定相应的数据 Repeater1.DataSource = new CarData().Select(Count, hehe); Repeater1.DataBind(); //修改显示页数 Label_NowPage.Text = hehe.ToString(); btn_prev.Enabled = true; btn_first.Enabled = true; btn_next.Enabled = false; btn_last.Enabled = false; } public int MaxPageNumber() { List<Car> clist = new CarData().Select(); double end2 = Math.Ceiling(clist.Count / (Count * 1.0)); int end = Convert.ToInt32(end2); return end; } }
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Data.SqlClient; /// <summary> /// CarData 的摘要说明 /// </summary> public class CarData { SqlConnection conn = null; SqlCommand cmd = null; public CarData() { conn = new SqlConnection("server=.;database=mydb;user=sa;pwd=123"); cmd = conn.CreateCommand(); } public List<Car> Select() { List<Car> clist = new List<Car>(); cmd.CommandText = "select *from car"; conn.Open(); SqlDataReader dr = cmd.ExecuteReader(); if (dr.HasRows) { while (dr.Read()) { Car c = new Car(); //c.Ids = Convert.ToInt32(dr[0]); c.Code = dr[0].ToString(); c.Name = dr[1].ToString(); c.Brand = dr[2].ToString(); c.Time = Convert.ToDateTime(dr[3]); c.Oil = Convert.ToDecimal(dr[4]); c.Powers = Convert.ToInt32(dr[5]); c.Exhaust = Convert.ToInt32(dr[6]); c.Price = Convert.ToDecimal(dr[7]); c.Pic = dr[8].ToString(); clist.Add(c); } } conn.Close(); return clist; } public List<Car> Select(int count, int NowPage) { List<Car> clist = new List<Car>(); cmd.CommandText = "select top " + count + " *from car where Code not in(select top " + ((NowPage - 1) * count) + " Code from car)"; conn.Open(); SqlDataReader dr = cmd.ExecuteReader(); if (dr.HasRows) { while (dr.Read()) { Car c = new Car(); //c.Ids = Convert.ToInt32(dr[0]); c.Code = dr[0].ToString(); c.Name = dr[1].ToString(); c.Brand = dr[2].ToString(); c.Time = Convert.ToDateTime(dr[3]); c.Oil = Convert.ToDecimal(dr[4]); c.Powers = Convert.ToInt32(dr[5]); c.Exhaust = Convert.ToInt32(dr[6]); c.Price = Convert.ToDecimal(dr[7]); c.Pic = dr[8].ToString(); clist.Add(c); } } conn.Close(); return clist; } }
另:
常规表单提交:
普通HTML页面,在form表单中action属性可以设置提交到哪个服务端 method属性可以选择提交方式
<form action="Default4.aspx" method="get"></form>
可以通过此方法将纯HTML页面中的数据提交到某个服务端,再通过服务端对数据进行操作。