Asp.net GridView usage (pagination)

Source: Internet
Author: User

Implement the pagination function provided by the GridView:

To implement the GrdView paging Function
The procedure is as follows:
1. Change the AllowPaging attribute of the GrdView control to true.
2. Change the PageSize attribute of the GrdView control to any value (10 by default)
3. Change PageSetting-> Mode of the GrdView control to Numeric (default: Numeric). This attribute is a paging style.
After the GridView property is set, the page style is displayed on the page.

Now we start to implement the paging function:

1. After <asp: GridView ID =...>, add OnPageIndexChanging = "GridView1_PageIndexChanging"
2. Add the following in the corresponding aspx. cs:
Protected void GridView1_PageIndexChanging (object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e. NewPageIndex;
InitPage (); // rebind the function of the GridView data
}
3,
GridView1.PageIndex = e. NewPageIndex;
Then re-bind the GridView.

Add a CheckBox column in The GridView to enable the full/All cancellation function

First, edit the template in the GridView, add the CheckBox control on the template, and then convert the newly added column field to the TemplateFiled

Background code:

Copy codeThe Code is as follows: 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 = beifeng trade; 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 feihu studio where ID number = '" + GridView1.DataKeys [I]. Value + "'";
Sqlcom = new SqlCommand (sqlstr, sqlcon );
Sqlcon. Open ();
Sqlcom. ExecuteNonQuery ();
Sqlcon. Close ();
}
}
Bind ();
}
Protected void button#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 feihu Studio ";
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 [] {"ID card number "};
GridView1.DataBind ();
Sqlcon. Close ();
}
}

Main front-end code:Copy codeThe Code is as follows: <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 = "ID card number" HeaderText = "User ID" SortExpression = "ID card number"/>
<Asp: BoundField DataField = "name" HeaderText = "User Name" SortExpression = "name"/>

<Asp: BoundField DataField = "Home Address" HeaderText = "Home Address" SortExpression = "Home Address"/>

</Columns>
<RowStyle ForeColor = "#000066" type = "regxph" text = "yourobjectname"/>
<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 = "select all"/>
<Asp: Button ID = "Button1" runat = "server" Font-Size = "9pt" Text = "cancel" onClick = "button#click"/>
<Asp: Button ID = "Button2" runat = "server" Font-Size = "9pt" Text = "delete" onClick = "Button2_Click"/>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.