ListView and DataPager paging programs in asp.net

Source: Internet
Author: User

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 ();
}

Related Article

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.