首页 > 编程语言 >C# .Net Gridview

C# .Net Gridview

时间:2022-12-29 14:55:56浏览次数:47  
标签:Web Gridview C# bind void System GridView1 using Net

1、分页查询

导图

 

前台:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebGv2.aspx.cs" Inherits="WebApplication6.WebGv2" %>

<!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>
            <%--AllowPaging:True 数据进行分页
                PageSize:3       每页分'3'行
                OnPageIndexChanging: 页码改变事件
                --%>
            <asp:GridView ID="GridView1" runat="server" AllowPaging="True" OnPageIndexChanging="GridView1_PageIndexChanging" PageSize="3">
            </asp:GridView>
        </div>
    </form>
</body>
</html>
View Code

--

后台:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication6
{
    public partial class WebGv2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //打开页面开始加载,后续的提交中不参与加载
            if (!IsPostBack) {
                bind();
            }
        }

        //查询数据的方法
        private void bind()
        {
            string sql = "select * from student order by id";
            //设置表格的数据源为查询的数据表
            GridView1.DataSource= MyDBSql.excutSql(sql);
            //绑定控件
            GridView1.DataBind();
        }


        /// <summary>
        /// 页码改变事件
        /// </summary>
        /// <param name="sender">事件源</param>
        /// <param name="e">对象 事件</param>
        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            //设置数据表格的页索引为当前事件 单击的页的编码
            GridView1.PageIndex = e.NewPageIndex;

            //重新调用查询数据方法
            bind();

        }
    }
}
View Code

--

 

标签:Web,Gridview,C#,bind,void,System,GridView1,using,Net
From: https://www.cnblogs.com/kezhang/p/17012531.html

相关文章