using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Configuration;
namespace WebApplication1
{
/// <summary>
/// news 的摘要说明。
/// </summary>
public class news : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.WebControls.LinkButton LinkButton1;

private void Page_Load(object sender, System.EventArgs e)
{
this.DataGrid1.AllowPaging=true;
if(!this.IsPostBack)
{
Data_Bind();
}

} #region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{    
this.LinkButton1.Click += new System.EventHandler(this.LinkButton1_Click);
this.DataGrid1.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DataGrid1_PageIndexChanged);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion
void Data_Bind()
{
SqlConnection objconn=new SqlConnection(ConfigurationSettings.AppSettings["connstr"]);
objconn.Open();
string objsql="select * from news";
SqlDataAdapter da=new SqlDataAdapter(objsql,objconn);
DataSet ds=new DataSet();
da.Fill(ds);
//this.DataGrid1.AutoGenerateColumns=true;
this.DataGrid1.DataSource=ds.Tables[0].DefaultView;
this.DataGrid1.DataBind();
objconn.Close();
} private void LinkButton1_Click(object sender, System.EventArgs e)
{
this.Response.Redirect("newsadd.aspx");
} private void DataGrid1_PageIndexChanged(object source,System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{

DataGrid1.CurrentPageIndex=e.NewPageIndex;
Data_Bind();


}
}
}这是用datagrid做的新闻列表分页,不知道为什么事件不触发?下面是前台代码:
<%@ Page language="c#" Codebehind="news.aspx.cs" AutoEventWireup="false" Inherits="WebApplication1.news" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>news</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<FONT face="宋体">
<asp:linkbutton id="LinkButton1" style="Z-INDEX: 101; LEFT: 64px; POSITION: absolute; TOP: 16px"
runat="server" Width="152px" Height="24px">添加新闻</asp:linkbutton><asp:datagrid id="DataGrid1" style="Z-INDEX: 102; LEFT: 56px; POSITION: absolute; TOP: 48px" runat="server"
Width="648px" Height="184px" AutoGenerateColumns="False" BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" BackColor="White" CellPadding="4" GridLines="Vertical"
ForeColor="Black" AllowSorting="True" AllowCustomPaging="True" AllowPaging="True" PageSize="5">
<SelectedItemStyle Font-Bold="True" ForeColor="White" BackColor="#CE5D5A"></SelectedItemStyle>
<AlternatingItemStyle BackColor="White"></AlternatingItemStyle>
<ItemStyle BackColor="#F7F7DE"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="White" BackColor="#6B696B"></HeaderStyle>
<FooterStyle BackColor="#CCCC99"></FooterStyle>
<Columns>
<asp:BoundColumn DataField="newsid" HeaderText="编号"></asp:BoundColumn>
<asp:BoundColumn DataField="newstitle" HeaderText="标题"></asp:BoundColumn>
<asp:BoundColumn DataField="newspublisher" HeaderText="发布者"></asp:BoundColumn>
<asp:BoundColumn DataField="newsdatatime" HeaderText="发布时间"></asp:BoundColumn>
<asp:ButtonColumn Text="选择" CommandName="Select"></asp:ButtonColumn>
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="更新" CancelText="取消" EditText="编辑"></asp:EditCommandColumn>
<asp:ButtonColumn Text="删除" CommandName="Delete"></asp:ButtonColumn>
</Columns>
<PagerStyle HorizontalAlign="Right" ForeColor="Black" BackColor="#F7F7DE" Mode="NumericPages"></PagerStyle>
</asp:datagrid></FONT></form>
</body>
</HTML>

解决方案 »

  1.   

    AllowCustomPaging="True"设为flash试试,同时看看事件有没有丢失,在属性栏上点闪电的标示能看到
      

  2.   

    你说的不触发事件事什么意思?
    没有执行DataGrid1_PageIndexChanged方法?你是否跟踪代码确定确实没有执行这段代码?
      

  3.   

    不触发事件就是你点击datagrid生成的数字分页码,没任何反映。
    事件没有丢失,我检查过代码,大家可以看我贴出的代码。我在VS的属性生成器里,发现了错误的原因在“分页”选项里,选择“允许分页”,但不能选择“允许自定义分页”
    而且“页大小”和“数值按纽”的数值必须一样,才可以触发分页的事件
    但我在代码里没有看出什么明显的变化。
      

  4.   

    DataGrid1.AllowPaging = true;
    DataGrid1.AllowCustomPaging = false;