This article introduces how to use ListView and DataPager to implement code of the Data paging program in. net. If you need to learn, refer
The page code is as follows:
The Code is as follows: |
Copy code |
<Asp: ListView runat = "server" ID = "_ simpleTableListView"> <LayoutTemplate> <Table> <Thead> <Tr> <Th id = "Th1" runat = "server"> ID </th> <Th id = "Th1" runat = "server"> StudentID </th> <Th id = "Th3" runat = "server"> Name </th> <Th id = "Th4" runat = "server"> Math </th> <Th id = "Th5" runat = "server"> English </th> <Th id = "Th6" runat = "server"> Chinese </th> </Tr> </Thead> <Tbody> <Asp: PlaceHolder runat = "server" ID = "itemPlaceholder"/> </Tbody> </Table> </LayoutTemplate> <ItemTemplate> <Tr> <Td> <Asp: Label ID = "IDLabel" runat = "server" Text = '<% # Eval ("ID") %>'/> </Td> <Td> <Asp: Label ID = "StudentIDLabel" runat = "server" Text = '<% # Eval ("StudentID") %>'/> </Td> <Td> <Asp: Label ID = "NameLabel" runat = "server" Text = '<% # Eval ("Name") %>'/> </Td> <Td> <Asp: Label ID = "MathLabel" runat = "server" Text = '<% # Eval ("Math") %>'/> </Td> <Td> <Asp: Label ID = "EnglishLabel" runat = "server" Text = '<% # Eval ("English") %>'/> </Td> <Td> <Asp: Label ID = "ChineseLabel" runat = "server" Text = '<% # Eval ("Chinese") %>'/> </Td> </Tr> </ItemTemplate> </Asp: ListView> <Asp: DataPager ID = "DataPager2" runat = "server" PagedControlID = "_ simpleTableListView" PageSize = "2" Onprerender = "DataPager2_PreRender"> <Fields> <Asp: NextPreviousPagerField ButtonType = "Button" ShowFirstPageButton = "True" ShowLastPageButton = "True"/> </Fields> </Asp: DataPager>
|
Background code:
The Code is as follows: |
Copy code |
Protected void DataPager2_PreRender (object sender, EventArgs e) { BindData (); } Private void BindData () { Using (OleDbConnection conn = new OleDbConnection (OleDbHelper. ConnectionString )) { String SQL = "select * from studentscore "; OleDbDataAdapter dp = new OleDbDataAdapter (SQL, conn ); DataTable dt = new DataTable (); Dp. Fill (dt ); _ SimpleTableListView. DataSource = dt; _ SimpleTableListView. DataBind (); } }
|
Note: to use this method, you do not need to bind data to the ListView control in the Page_Load event of the page,
Otherwise, there will be problems with paging.
2nd methods:
The Code is as follows: |
Copy code |
Protected void Page_Load (object sender, EventArgs e) { If (! IsPostBack) { BindData (); } } Protected void _ simpleTableListView_PagePropertiesChanging (object sender, PagePropertiesChangingEventArgs e) { DataPager2.SetPageProperties (e. StartRowIndex, e. MaximumRows, false ); BindData (); }
|