asp.net显示第一页、上一页、下一页和最后一页的分页显示数据表的数据
时间: 2024-05-12 19:13:48 浏览: 13
在 ASP.NET 中,可以使用 GridView 控件来显示数据表,并且可以通过设置 Paging 属性来实现分页显示数据。以下是一个简单的示例代码:
```aspx
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" AllowPaging="True" PageSize="10">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="Age" HeaderText="Age" />
</Columns>
</asp:GridView>
```
在代码后台,需要为 GridView 绑定数据源,并且设置 PagerSettings 属性来设置分页显示的样式和文本。以下是一个示例代码:
```csharp
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindData();
}
}
private void BindData()
{
// 绑定数据源到 GridView
string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM Customers", connection);
DataTable table = new DataTable();
adapter.Fill(table);
GridView1.DataSource = table;
GridView1.DataBind();
}
// 设置分页显示的样式和文本
GridView1.PagerSettings.Mode = PagerButtons.NumericFirstLast;
GridView1.PagerSettings.FirstPageText = "第一页";
GridView1.PagerSettings.LastPageText = "最后一页";
GridView1.PagerSettings.NextPageText = "下一页";
GridView1.PagerSettings.PreviousPageText = "上一页";
}
```
这样就可以在页面上显示一个带有分页的 GridView 控件,用户可以通过点击分页按钮来浏览数据表中的数据。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)