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>页&nbsp;&nbsp;
                                <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 (SELECT
                                            ROW_NUMBER() OVER (ORDER BY id ASC) AS rowname,
                                            *
                                            FROM Student) AS temp
                                    WHERE 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>

**不知道这样写好理解吗?**
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

双叶红于二月花

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值