Hi Brian,
As for the DataGrid paging problem without ViewState Enabled, I think the
most important thing is to rebind the grid with data when there is any
event fired which indicate us to change the grid's page index. For example,
in the datagrid's PageIndexChanged event, do the following things in
sequence:
grid.CurrentPageIndex = e.NewPageIndex;
ReBindGridWithData();
And since you use a dropdownlist to let the user select a certain page
index to change the page index, you also need to do the samething in the
dropdownlist's SelectedIndexChange event, such as:
get selected index from the dropdownlist
set it to the datagrid's CurrentPageIndex property
Rebind grid with data
and here is a test page I've made, you can have a test on your side if you
have anything unclear:
==============aspx====================
<HTML>
<HEAD>
<title>PageSelector</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 style="HEIGHT: 44px">CurrentPage:<asp:dropdownlist id="lstCurrent"
EnableViewState="True" Runat="server"
AutoPostBack="True"></asp:dropdownlist>
Page Count:
<asp:label id="lblPageCount" Runat="server"></asp:label></td>
</tr>
<tr>
<td><asp:datagrid id="dgPage" runat="server" EnableViewState="False"
AllowPaging="True" PageSize="5">
<PagerStyle Mode="NumericPages"></PagerStyle>
</asp:datagrid></td>
</tr>
<tr>
<td> </td>
</tr>
</table>
</form>
</body>
</HTML>
=========code behind===================
public class PageSelector : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DropDownList lstCurrent;
protected System.Web.UI.WebControls.Label lblPageCount;
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<=27;++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();
if(!IsPostBack)
{
for(int i=1;i<=dgPage.PageCount;++i)
{
lstCurrent.Items.Add(i.ToString());
}
lblPageCount.Text = dgPage.PageCount.ToString();
}
}
#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.lstCurrent.SelectedIndexChanged += new
System.EventHandler(this.lstCurrent_SelectedIndexChanged);
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)
{
dgPage.CurrentPageIndex = e.NewPageIndex;
lstCurrent.SelectedIndex = e.NewPageIndex;
Bind_Grid();
}
private void lstCurrent_SelectedIndexChanged(object sender,
System.EventArgs e)
{
int index = lstCurrent.SelectedIndex;
dgPage.CurrentPageIndex = index;
Bind_Grid();
}
}
=====================================================
Also, if you have any other quetsions, please feel free to post here.
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