Hi Brian,
As John has mentioned, the DataGrid's CurrentPageIndex property will work
only when we enable its ViewState because the DataGrid maintain this
information in the ViewState collection. If we disable the View
State, this property will always return 0.
In addtion to keeping the Viewstate turn on, we can use some other means
to
keep the currentPageIndex for the datagrid, for example, we can use a
invisible Label or a <input type="hidden" > html field to store the
DataGrid's index. Then when in the DataGrid's PageIndex change event, we
update this value so that we can retrieve back it later. This allow us
still let the DataGrid with ViewState disabled.
Here is a demo page I've made using the above means, you can have a look
if
you feel anything unclear:
============aspx page============
<HTML>
<HEAD>
<title>PagingGrid</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>
<form id="Form1" method="post" runat="server">
<table width="100%" align="center">
<tr>
<td><input id="dgOriginalPageIndex" type="hidden" value="0"
runat="server"></td>
</tr>
<tr>
<td><asp:datagrid id="dgPage" runat="server" EnableViewState="False"
PageSize="6" AllowPaging="True"
AutoGenerateColumns="False">
<Columns>
<asp:BoundColumn DataField="index"
HeaderText="Index"></asp:BoundColumn>
<asp:BoundColumn DataField="name"
HeaderText="Name"></asp:BoundColumn>
<asp:BoundColumn DataField="price"
HeaderText="Price"></asp:BoundColumn>
</Columns>
<PagerStyle PageButtonCount="6" Mode="NumericPages"></PagerStyle>
</asp:datagrid>
</td>
</tr>
</table>
</form>
</body>
</HTML>
=========code behind class========
public class PagingGrid : System.Web.UI.Page
{
protected System.Web.UI.HtmlControls.HtmlInputHidden dgOriginalPageIndex;
protected System.Web.UI.WebControls.DataGrid dgPage;
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
Session["DATASOURCE"] = GetDataSource();
}
Bind_Grid();
}
protected DataTable GetDataSource()
{
DataTable tb = new DataTable();
tb.Columns.Add("index");
tb.Columns.Add("name");
tb.Columns.Add("price",typeof(double));
DataRow dr = null;
bool[] flags = {true,false};
for(int i=1;i<=20;++i)
{
dr = tb.NewRow();
dr["index"] = i.ToString();
dr["name"] = "Name" + i.ToString();
dr["price"] = 3.434 * (i%3 +1);
tb.Rows.Add(dr);
}
return tb;
}
protected void Bind_Grid()
{
DataTable tb =(DataTable)Session["DATASOURCE"];
dgPage.DataSource = tb;
dgPage.DataBind();
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.dgPage.PageIndexChanged += new
System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.dgPage_PageIn
dexChanged);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void dgPage_PageIndexChanged(object source,
System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
Response.Write("<br>Original Page Index: "+dgOriginalPageIndex.Value );
Response.Write("<br>New Page Index: " + e.NewPageIndex);
dgPage.CurrentPageIndex = e.NewPageIndex;
dgOriginalPageIndex.Value = e.NewPageIndex.ToString();
Bind_Grid();
}
}
===========================================
Hope helps. Thanks.
Regards,
Steven Cheng
Microsoft Online Support
Get Secure!
www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx