通过DataGrid的AllowPaging属性启用分页,给你一段代码,直接copy到你的项目里即可
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Data" %><html>
<title>Custom Pagination</title>
<style>
  a  {behavior:url(MouseOver.htc);}
  hr {height:2px;color:black;}
  .StdText {font-family:verdana;font-size:9pt;font-weight:bold;}
  .StdTextBox {font-family:verdana;font-size:9pt;border:solid 1px black;filter:progid:DXImageTransform.Microsoft.dropshadow(OffX=2, OffY=2, Color='gray', Positive='true')}
  .Shadow {filter:progid:DXImageTransform.Microsoft.dropshadow(OffX=2, OffY=2, Color='gray', Positive='true');}
</style>
<script runat="server">
public void Page_Load(Object sender, EventArgs e)
{
// Initialize only the first time...
if (!Page.IsPostBack)
{
lblURL.Text = Request.Url + "<hr>";
SetVirtualItemCount();
}
}SqlDataReader dr;
private SqlDataReader CreateDataSource(int nPageIndex)
{
// page index is assumed to be 0-based
int nPageSize = grid.PageSize;
int nBaseProductID = nPageSize * nPageIndex;
// Set up the connection
String strConn = "DATABASE=Northwind;SERVER=localhost;UID=sa;PWD=;";
SqlConnection conn = new SqlConnection(strConn); // Set up the command
String strCmd = "SELECT TOP " + nPageSize + " " +
"productid, productname, quantityperunit, unitsinstock " + 
"FROM products " + 
"WHERE productid >" + nBaseProductID;
SqlCommand cmd = new SqlCommand(strCmd, conn);

// Execute the command
conn.Open();
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
return dr;
}public void ItemCreated(Object sender, DataGridItemEventArgs e)
{
ListItemType elemType = e.Item.ItemType;
if (elemType == ListItemType.Pager) 
{
// The pager as a whole has the following layout:
//
// <TR><TD colspan=X> ... links ... </TD></TR> 
//
// Item points to <TR>. The code below moves to <TD>.
TableCell pager = (TableCell) e.Item.Controls[0]; // Loop through the pager buttons skipping over blanks
// (Blanks are treated as LiteralControl(s)
for (int i=0; i<pager.Controls.Count; i+=2) 
{
Object o = pager.Controls[i];
if (o is LinkButton) 
{
LinkButton h = (LinkButton) o;
h.Text = "[ " + h.Text + " ]"; 
}
else
{
Label l = (Label) o;
l.Text = "Page " + l.Text; 
}
}
}
}public void PageIndexChanged(Object sender, DataGridPageChangedEventArgs e)
{
grid.CurrentPageIndex = e.NewPageIndex;
grid.DataSource = CreateDataSource(grid.CurrentPageIndex);
grid.DataBind();
dr.Close();
}public void OnLoadData(Object sender, EventArgs e)
{
grid.DataSource = CreateDataSource(0);
grid.DataBind();
dr.Close();
}public void SetVirtualItemCount()
{
// Set up the connection
String strConn = "DATABASE=Northwind;SERVER=localhost;UID=sa;PWD=;";
SqlConnection conn = new SqlConnection(strConn); // Set up the command
String strCmd = "SELECT COUNT(*) FROM products";
SqlCommand cmd = new SqlCommand(strCmd, conn);

// Execute the command
conn.Open();
int nItemCount = (int) cmd.ExecuteScalar();
conn.Close(); grid.VirtualItemCount = nItemCount;
return;
}
</script>
<body bgcolor="ivory" style="font-family:arial;font-size:xsmall"><!-- ASP.NET topbar -->
<h2>Custom Pagination</h2>
<asp:Label runat="server" cssclass="StdText" font-bold="true">Current path: </asp:label>
<asp:Label runat="server" id="lblURL" cssclass="StdText" style="color:blue"></asp:label><form runat="server"><!-- Query -->
<asp:label runat="server" cssclass="stdtext" Text="Query" />
<asp:textbox runat="server" Enabled="false" cssclass="stdtextbox" width="600px"
text="SELECT ProductID, ProductName, QuantityPerUnit, UnitsInStock FROM Products" />
<asp:linkbutton runat="server" cssclass="stdtext" OnClick="OnLoadData" Text="Go get data..." /> 
<hr><!-- Show the information --> 
<asp:datagrid runat="server" id="grid"
Font-Size="Smaller" Font-Names="Verdana" 
CellPadding="2" CellSpacing="0" GridLines="vertical"
CssClass="Shadow" BackColor="White"
BorderStyle="solid" BorderColor="black" BorderWidth="1"
DataKeyField="productid"
AllowPaging="True"
AllowCustomPaging="True"
OnItemCreated="ItemCreated"
OnPageIndexChanged="PageIndexChanged"> <PagerStyle Font-Bold="true" Mode="NumericPages" BackColor="palegreen" />
<AlternatingItemStyle BackColor="#eeeeee" />
<ItemStyle BackColor="White" />
<HeaderStyle Font-Bold="True" ForeColor="White" BackColor="Navy" />
</asp:datagrid></form>
</body></html>

解决方案 »

  1.   

    或者
    http://chs.gotdotnet.com/quickstart/aspplus/samples/webforms/ctrlref/webctrl/datagrid/doc_datagrid.aspx#paging
      

  2.   

    不要这么烦的有没有啊,只要能显示就行了,我现在的问题是有12条数据,我在datagrid的属性里设置了每页放5条的,显示模式是nuberpage的,可是画面上只有1,没有2,3;1上也没有联结。这是怎么回事啊。
      

  3.   

    看看你的代码先
    http://chs.gotdotnet.com/quickstart/aspplus/samples/webforms/ctrlref/webctrl/datagrid/doc_datagrid.aspx#paging
    这个是很简单的分页,你看看先
      

  4.   

    我都是在datagrid的属性里设置的,就设置了allowpaging、mode=numberpage,还没有用到分页里的PageIndexChanged(Object sender, DataGridPageChangedEventArgs e)呢,它就是没有象1,2,3...这样的页符。
      

  5.   

    有没有设置allowpage属性:AllowPaging="true",在datagrid属性里设置。
      

  6.   

    在ageIndexChanged事件要加下面代码即可
    MyDataGrid.CurrentPageIndex = e.NewPageIndex;
    MyDataGrid.DataSource =数据源;
    MyDataGrid.DataBind();