VC# ASP.NET GridView1 分页

VC# ASP.NET GridView1 分页_第1张图片

前台:

 <PagerTemplate>
            <asp:Label ID="lbl_page_size1" runat="server" Text='<%# "每页"+GridView1.PageSize+"条" %>'></asp:Label>
            <asp:Label ID="lblPage" runat="server" Text='<%# "第" + (((GridView)Container.NamingContainer).PageIndex + 1)  + "页/共" + (((GridView)Container.NamingContainer).PageCount) + "页" %> '></asp:Label>
            <asp:LinkButton ID="lbnFirst" runat="Server" Text="首页"  Enabled='<%# ((GridView)Container.NamingContainer).PageIndex != 0 %>' CommandName="Page" CommandArgument="First" ></asp:LinkButton>
            <asp:LinkButton ID="lbnPrev" runat="server" Text="上一页" Enabled='<%# ((GridView)Container.NamingContainer).PageIndex != 0 %>' CommandName="Page" CommandArgument="Prev"  ></asp:LinkButton>
            <asp:LinkButton ID="lbnNext" runat="Server" Text="下一页" Enabled='<%# ((GridView)Container.NamingContainer).PageIndex != (((GridView)Container.NamingContainer).PageCount - 1) %>' CommandName="Page" CommandArgument="Next" ></asp:LinkButton>
            <asp:LinkButton ID="lbnLast" runat="Server" Text="尾页"   Enabled='<%# ((GridView)Container.NamingContainer).PageIndex != (((GridView)Container.NamingContainer).PageCount - 1) %>' CommandName="Page" CommandArgument="Last" ></asp:LinkButton>
            到第
            <asp:TextBox runat="server" ID="inPageNum"></asp:TextBox>页<asp:Button ID="Button1"CommandName="go" runat="server" Text="GO" />               
            </PagerTemplate>

后台:

 //web.config配置数据源

<connectionStrings>
<add name="gft_esp" connectionString="Data Source=192.168.0.113;Initial Catalog=gft_esp_rd;Persist Security Info=True;User ID=sa;Password=sql2k5"/>

</connectionStrings>

//绑定数据的涵数

public static DataSet Query(string SQLString)
    {     
        using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["gft_esp"].ConnectionString))
        {
            DataSet ds = new DataSet();
            try
            {
                connection.Open();
                SqlDataAdapter command = new SqlDataAdapter(SQLString, connection);
                command.Fill(ds, "ds");
            }
            catch (System.Data.SqlClient.SqlException ex)
            {
                throw new Exception(ex.Message);
            }
            return ds;
        }
    }

 

//数据绑定

 protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        string sel = TextBox1.Text.ToString().Trim();

        string sql = "SELECT * FROM [sysuser] where [User_id] like '%" + sel + "%'or [Role_id] like '%" + sel + "%'or [Chi_Name] like '%" + sel + "%'or[Eng_Name] like '%" + sel + "%'or [Company_code] like '%" + sel + "%'or [Dept_ID] like '%" + sel + "%'or  [Phone] like '%" + sel + "%'or  [E-Mail] like '%" + sel + "%'";
       
        GridView1.DataSource = Query(sql); //自定义绑定涵数
        GridView1.DataBind();
    }

//执行指定命令,(跳转到指定页)
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "go")
        {
            try
            {
                TextBox tb = (TextBox)GridView1.BottomPagerRow.FindControl("inPageNum");
                int num = Int32.Parse(tb.Text); GridViewPageEventArgs ea = new GridViewPageEventArgs(num - 1);
                GridView1_PageIndexChanging(null, ea);
            }
            catch { }
        }
    }

 

最后记得

GridView1的

PageIndexChanging 选GridView1_PageIndexChanging

RowCommand选GridView1_RowCommand

 

你可能感兴趣的:(VC# ASP.NET GridView1 分页)