程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> ASP.NET >> ASP.NET基礎 >> Asp.net GridView使用大全(分頁實現)

Asp.net GridView使用大全(分頁實現)

編輯:ASP.NET基礎

GridView自帶的分頁功能實現:

要實現GrdView分頁的功能
操作如下:
1、更改GrdView控件的AllowPaging屬性為true。
2、更改GrdView控件的PageSize屬性為 任意數值(默認為10)
3、更改GrdView控件的PageSetting->Mode為Numeric等(默認為Numeric)該屬性為分頁樣式。
GridView屬性設置好了,從頁面上也能看到分頁樣式。

現在開始實現分頁的功能:

1、在<<asp:GridView ID=......>後添加,OnPageIndexChanging="GridView1_PageIndexChanging"
2、在對應的aspx.cs中添加:
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        InitPage(); //重新綁定GridView數據的函數
    }
3、
GridView1.PageIndex = e.NewPageIndex;
完了之後再重新綁定一下GridView。

GridView添加CheckBox列實現全選/全部取消功能

首先GridView編輯模版,在模板上添加CheckBox控件,之後將新添加列字段轉換為TemplateFiled



後台代碼:
復制代碼 代碼如下:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class Default5 : System.Web.UI.Page
{
    SqlConnection sqlcon;
    string strCon = "Data Source=(local);Database=北風貿易;Uid=sa;Pwd=sa";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bind();
        }
    }
    protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
    {
        for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
        {
            CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
            if (CheckBox2.Checked == true)
            {
                cbox.Checked = true;
            }
            else
            {
                cbox.Checked = false;
            }
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        sqlcon = new SqlConnection(strCon);
        SqlCommand sqlcom;
        for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
        {
            CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
            if (cbox.Checked == true)
            {

                string sqlstr = "delete from 飛狐工作室 where 身份證號碼='" + GridView1.DataKeys[i].Value + "'";
                sqlcom = new SqlCommand(sqlstr, sqlcon);
                sqlcon.Open();
                sqlcom.ExecuteNonQuery();
                sqlcon.Close();
            }
        }
        bind();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        CheckBox2.Checked = false;
        for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
        {
            CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
            cbox.Checked = false;
        }
    }
    public void bind()
    {
        string sqlstr = "select top 5 * from 飛狐工作室";
        sqlcon = new SqlConnection(strCon);
        SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
        DataSet myds = new DataSet();
        sqlcon.Open();
        myda.Fill(myds, "tb_Member");
        GridView1.DataSource = myds;
        GridView1.DataKeyNames = new string[] { "身份證號碼" };
        GridView1.DataBind();
        sqlcon.Close();
    }
}

前台主要代碼:
復制代碼 代碼如下:
<asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False"
                        CellPadding="3" Font-Size="9pt" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px">
                        <FooterStyle BackColor="White" ForeColor="#000066" />
                        <Columns>
                             <asp:TemplateField>
                                <ItemTemplate>
                                    <asp:CheckBox ID="CheckBox1" runat="server" />
                                </ItemTemplate>
                            </asp:TemplateField>
                             <asp:BoundField DataField="身份證號碼" HeaderText="用戶ID" SortExpression="身份證號碼" />
                            <asp:BoundField DataField="姓名" HeaderText="用戶姓名" SortExpression="姓名"/>

                            <asp:BoundField DataField="家庭住址" HeaderText="家庭住址" SortExpression="家庭住址"/>

                        </Columns>
                        <RowStyle ForeColor="#000066" />
                        <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
                        <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
                        <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
                    </asp:GridView>
                     <asp:CheckBox ID="CheckBox2" runat="server" AutoPostBack="True" Font-Size="9pt" OnCheckedChanged="CheckBox2_CheckedChanged"
                        Text="全選" />
                    <asp:Button ID="Button1" runat="server" Font-Size="9pt" Text="取消" onClick="Button1_Click" />
                    <asp:Button ID="Button2" runat="server" Font-Size="9pt" Text="刪除" onClick="Button2_Click" />

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved