WebForm GridView分页
/前台代码
<asp:GridView ID="gridviewId" Width="60%" runat="server" AutoGenerateColumns="false" AllowPaging="True" AllowSorting="True" CellPadding="4" ForeColor="#333333" GridLines="None" OnRowDataBound="GridView1_RowDataBound" PageSize="5" OnPageIndexChanging="GridView1_PageIndexChanging"><AlternatingRowStyle BackColor="White" /><Columns><asp:BoundField DataField="Id" HeaderText="编号" Visible="False"><ItemStyle HorizontalAlign="Center" /></asp:BoundField><asp:BoundField DataField="Name" HeaderText="姓名"><ItemStyle HorizontalAlign="Center" /></asp:BoundField><asp:BoundField DataField="age" HeaderText="年龄"><ItemStyle HorizontalAlign="Center" /></asp:BoundField><asp:BoundField DataField="city" HeaderText="城市"><ItemStyle HorizontalAlign="Left" /></asp:BoundField><asp:BoundField DataField="formateName" HeaderText="关键字"><ItemStyle HorizontalAlign="Center" /></asp:BoundField></Columns><PagerTemplate><table style="width: 100%; font-size: 12px;"><tr><td style="float: right">第<asp:Label ID="label1" runat="server" Text="<%#((GridView)Container.Parent.Parent).PageIndex+1 %>"></asp:Label>页/共<asp:Label ID="labeltotal" runat="server" Text="<%#((GridView)Container.Parent.Parent).PageCount %>"></asp:Label>页 <asp:LinkButton ID="firstPage" runat="server" CommandArgument="First" CommandName="Page" Visible="<%# ((GridView)Container.NamingContainer).PageIndex!=0 %>">首页</asp:LinkButton><asp:LinkButton ID="prevPage" runat="server" CommandArgument="Prev" CommandName="Page" Visible="<%# ((GridView)Container.NamingContainer).PageIndex != 0 %>">上一页</asp:LinkButton><asp:LinkButton ID="nextPage" runat="server" CommandArgument="Next" CommandName="Page" Visible="<%# ((GridView)Container.NamingContainer).PageIndex != ((GridView)Container.NamingContainer).PageCount-1 %>">下一页</asp:LinkButton><asp:LinkButton ID="lastPage" runat="server" CommandArgument="Last" CommandName="Page" Visible="<%# ((GridView)Container.NamingContainer).PageIndex != ((GridView)Container.NamingContainer).PageCount -1 %>">尾页</asp:LinkButton><asp:TextBox ID="txtnewPage" runat="server" Text="<%# ((GridView)Container.Parent.Parent).PageIndex + 1 %>" width="20px"></asp:TextBox><asp:LinkButton ID="btngo" runat="server" CausesValidation="false" CommandArgument="GO" CommandName="Page" Text="跳转" OnClick="btngo_Click"></asp:LinkButton></td></tr></table></PagerTemplate><EditRowStyle BackColor="#2461BF" /><FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /><HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /><PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Right" /><RowStyle BackColor="#EFF3FB" /><SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" /><SortedAscendingCellStyle BackColor="#F5F7FB" /><SortedAscendingHeaderStyle BackColor="#6D95E1" /><SortedDescendingCellStyle BackColor="#E9EBEF" /><SortedDescendingHeaderStyle BackColor="#4870BE" /></asp:GridView>
//后台代码
protected void Page_Load(object sender, EventArgs e){if (!IsPostBack){Binds();}}public void Binds(){this.gridviewId.DataSource = new TestDAL().getStuBypageIndex(10, 1).DefaultView;this.gridviewId.DataBind();}protected void gridviewId_RowDataBound(object sender, GridViewRowEventArgs e){if (e.Row.RowType == DataControlRowType.DataRow){TableCellCollection cells = e.Row.Cells;foreach (TableCell cell in cells){cell.Text = Server.HtmlDecode(cell.Text);}}if (e.Row.RowIndex != -1){int index = this.gridviewId.PageIndex * this.gridviewId.PageSize + e.Row.RowIndex + 1;e.Row.Cells[0].Text = index.ToString();}}protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e){}protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e){this.gridviewId.PageIndex = e.NewPageIndex;Binds();}protected void btngo_Click(object sender, EventArgs e){if (((LinkButton)sender).CommandArgument.ToString().ToLower().Equals("go")) {// GridViewRow rowNum = this.gridviewId.BottomPagerRow;TextBox numBox = (TextBox)this.gridviewId.BottomPagerRow.FindControl("txtnewPage");int inputNum = Convert.ToInt32(numBox.Text);gridviewId.PageIndex = inputNum - 1;Binds();}}
分页方法DAL代码可以根据不同方式进行变换
/// <summary>/// 学生表分页/// </summary>/// <param name="pagesize"></param>/// <param name="pageindex"></param>/// <returns></returns>public DataTable getStuBypageIndex(int pagesize, int pageindex){string sql = @"SELECT*FROM (SELECTROW_NUMBER() OVER (ORDER BY id ASC) AS rowname,*FROM Student) AS tempWHERE temp.rowname BETWEEN " + ((pageindex - 1) * pagesize + 1) + " AND " + (pagesize * pageindex) + "";DataTable dt = SqlHelper.GetDataTable(sql, CommandType.Text);return dt;}
实例类
public class Student{public int Id { get; set; }public string name { get; set; }public int age { get; set; }public string city { get; set; }public string formateName { get; set; }public int subjectId { get; set; }}
SQLHelper帮助类
public class SqlHelper{public static String connectionString = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;public static String ConnectionString{get { return SqlHelper.connectionString; }}/// <summary>/// 开启数据库/// </summary>/// <param name="comText"></param>/// <param name="comType"></param>/// <param name="parameters"></param>/// <returns></returns>private static SqlCommand PrepareCommand(string comText, CommandType comType, params SqlParameter[] parameters){SqlCommand com = new SqlCommand();com.CommandText = comText;com.CommandType = comType;com.Connection = new SqlConnection(ConnectionString);if (parameters != null && parameters.Length != 0){com.Parameters.AddRange(parameters);}com.Connection.Open();return com;}/// <summary>/// 增删改/// </summary>/// <param name="comText"></param>/// <param name="comType"></param>/// <param name="parameters"></param>/// <returns></returns>public static int ExecuteNonQuery(string comText, CommandType comType, params SqlParameter[] parameters){SqlCommand com = PrepareCommand(comText, comType, parameters);int result = com.ExecuteNonQuery();com.Connection.Close();return result;}/// <summary>/// 查询单个列/// </summary>/// <param name="comText"></param>/// <param name="comType"></param>/// <param name="parameters"></param>/// <returns></returns>public static object ExecuteScalar(string comText, CommandType comType, params SqlParameter[] parameters){SqlCommand com = PrepareCommand(comText, comType, parameters);object result = com.ExecuteScalar();com.Connection.Close();return result;}/// <summary>/// 查询多列/// </summary>/// <param name="comText"></param>/// <param name="comType"></param>/// <param name="parameters"></param>/// <returns></returns>public static SqlDataReader ExecuteReader(string comText, CommandType comType, params SqlParameter[] parameters){SqlCommand com = PrepareCommand(comText, comType, parameters);SqlDataReader result = com.ExecuteReader(CommandBehavior.CloseConnection);return result;}/// <summary>/// 绑定dataTable数据/// </summary>/// <param name="comText"></param>/// <param name="comType"></param>/// <param name="parameters"></param>/// <returns></returns>public static DataTable GetDataTable(string comText, CommandType comType, params SqlParameter[] parameters){SqlCommand com = PrepareCommand(comText, comType, parameters);SqlDataAdapter da = new SqlDataAdapter(com);DataSet ds = new DataSet();da.Fill(ds);com.Connection.Close();return ds.Tables[0];}/// <summary>/// 执行多条sql语句/// </summary>/// <param name="sqlTexts"></param>/// <returns></returns>public static Int32[] ExcuteSQL(params String[] sqlTexts){SqlConnection cn = new SqlConnection(ConnectionString);cn.Open();SqlCommand cmd = new SqlCommand();cmd.Connection = cn;//启动事务SqlTransaction trans;trans = cn.BeginTransaction();cmd.Transaction = trans;int num = 0;try{int[] affectRows = new int[sqlTexts.Length];for (int i = 0; i < affectRows.Length; ++i){if (sqlTexts[i] != null){cmd.CommandText = sqlTexts[i];affectRows[i] = cmd.ExecuteNonQuery();num = i;}}trans.Commit();return affectRows;}catch (Exception ex){trans.Rollback();string s = ex.Message;return null;}finally{cn.Close();}}}
web.config中拼接字符串
<connectionStrings><add name="connStr" connectionString="server=服务器名称;DataBase=数据库名称";uid=用户登录名;pwd=密码/></connectionStrings>**不知道这样写好理解吗?**
WebForm GridView分页相关推荐
- Asp.net(c#)GridView分页时用图片显示上一页,下一页
Asp.net(c#)GridView分页时用图片显示上一页,下一页 效果展示: 需要的两张图片: 详细代码: Code <%@ Page Language="C#" ...
- GridView分页详解
本次使用GridView是工厂模式下 无极限分类 GridView详解 1.GridView前台分析 2.GridView分页讲解 3.GridView绑定,编辑,更新,取消,删除,排序 Grid ...
- GridView分页的实现以及自定义分页样式功能实例
http://www.jb51.net/article/39677.htm GridView分页的实现 要在GridView中加入 //实现分页 AllowPaging="true" ...
- 如何实现GridView分页功能?
实现GridView分页功能的四个关键步骤 1.设置AllowPaging="True" 2.设置PageSize=每页纪录数. 3.设置分页事件OnPageIndexChangi ...
- Asp.net GridView分页功能的实现
最近学习了Asp.net的GridView分页的实现,当然,GridView自带分页功能,但是这个分页功能的后台数据库操作的时候仍是 需要查询出所有的记录,只是前台页面显示GridView的时候有一种 ...
- asp.net 中的gridview 之gridview 分页
在web开发中我们通常都会用到asp.net 中的gridview 分页以便更好的展示数据,现在就让我们共同学习一下gridview 分页,这是本人的分页代码,也许不是最简单的,但是可以使用. 1. ...
- android gridview分页显示,GridView使用自带分页功能时分页方式及样式PagerStyle
GridView分页,使用自带分页功能,类似下面样式: 在aspx页面中,GridView上的PagerStyle下CssClass属性,设置为bubufxPagerCss,具体aspx页面代码: A ...
- GridView分页操作
转自开源中国:http://www.oschina.net/code/snippet_54100_8873 当GridView中显示的记录很多的时候,可以通过GridView的分页功能来分页显示这些记 ...
- webform asp.net gridview 分页 利用aspnetpager 分页
最近做项目成功温习了一个10年前的老项目asp.net webform 哈哈. 这里温习下,gridview 真分页 1. BindDataPage 真分页 2. BindData 假分页 都给了相应 ...
最新文章
- java sqlite使用小记
- D触发器Verilog描述
- MongoDB分组查询,聚合查询,以及复杂查询
- PHP中如何判断属性类型,php – 如何获取doctrine实体属性的类型
- html插入视频时不自动播放,html5中嵌入视频自动播放的问题解决
- Java之数组(上)
- 保存 Hexo 博客源码到 GitHub
- Java实验一:博饼
- 生活大爆炸第三季 那些精妙的台词翻译
- 使用Python-Flask框架开发Web网站系列课程(四)构建前端
- 两波形相位差的计算值_有功功率、无功功率和视在功率该怎么计算?
- 【差分约束 模板题】 洛谷P5960(未完待续)
- 打造零售数据中台 数澜助百果园数字化转型
- omap3530支持gpu模块
- BOOST升压电路参数计算
- 踩过一个FM24C64与FM24CL64的坑
- 关于栈顶指针初值为-1和0的区别
- H3C模拟器---HCL使用心得
- 10招有效预防电脑辐射
- Power bi 4.3 子弹图