分页问题
using System;using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
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;
namespace WebApplication6
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataGridDataBind();
}
}
private void DataGridDataBind()
{
SqlConnection cn = new SqlConnection("server=127.0.0.1;uid=sa;pwd=1234;database=fen");
cn.Open();
string sql = "select * from wo";
SqlDataAdapter dr = new SqlDataAdapter(sql, cn);
DataSet ds = new DataSet();
dr.Fill(ds);
dg.DataSource = ds;
dg.DataBind();
cn.Close();
}
private void dg_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
//设置当前页的索引
dg.CurrentPageIndex = e.NewPageIndex;
DataGridDataBind();
}
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication6._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.
<html xmlns="http://www. >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<asp:DataGrid ID="dg" runat="server" Style="z-index: 100; left: 180px; position: absolute;
top: 117px" AutoGenerateColumns="false" DataKeyField="id" AllowPaging="True" PageSize="5"
PagerStyle-Mode="NumericPages" CellPadding="4" BackColor="White" BorderWidth="1px" Height="20px"
BorderStyle="None" BorderColor="#3366CC">
<SelectedItemStyle Font-Bold="True" ForeColor="#CCFF99" BackColor="#009999"></SelectedItemStyle>
<ItemStyle ForeColor="#003399" BackColor="White"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="#CCCCFF" BackColor="#003399"></HeaderStyle>
<FooterStyle ForeColor="#003399" BackColor="#99CCCC"></FooterStyle>
<Columns>
<asp:BoundColumn DataField="y" HeaderText="标题"></asp:BoundColumn>
<asp:BoundColumn DataField="t" HeaderText="新闻内容"></asp:BoundColumn>
<asp:BoundColumn DataField="time" HeaderText="发布日期"></asp:BoundColumn>
<asp:HyperLinkColumn DataNavigateUrlField="id" DataNavigateUrlFormatString="detail.aspx?ID={0}" DataTextField="id"
HeaderText="编号"></asp:HyperLinkColumn>
</Columns>
</asp:DataGrid>
</form>
</body>
</html>
运行后点击第2页第3 页 表中内容不变,一直是第1页的内容,高手们帮忙看看哪不对?