Data grid current page number?

B

Brian Henry

Hi, I have a data grid that has the following properties

EnableViewState = false
AllowPaging = true
AllowSorting = true
AllowCustomPaging = false

now, when the page switches I want to get the current page number and
display it in a label on the form... but it doesnt seem to return anything
but zero no mater which page i am on in CurrentPageIndex property of the
grid how do you get the current page number? thanks
 
J

John Saunders

Brian Henry said:
Hi, I have a data grid that has the following properties

EnableViewState = false
AllowPaging = true
AllowSorting = true
AllowCustomPaging = false

now, when the page switches I want to get the current page number and
display it in a label on the form... but it doesnt seem to return anything
but zero no mater which page i am on in CurrentPageIndex property of the
grid how do you get the current page number? thanks

Try leaving ViewState turned on and see if that helps.
 
S

S. Justin Gengo

Brian,

Assuming that CurrentPageIndex can't be zero for all pages (because if it
was you wouldn't be able to switch pages) the page number will always be
CurrentPageIndex + 1

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
 
B

Brian Henry

yes that helps, but also sends me back a 1MB HTML file *LOL* not good for
internet connections...
 
S

Steven Cheng[MSFT]

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
 
B

Brian Henry

when you use Session() does it send that information to the client with the
HTML page like viewstate() does or is session completely server side? that
is one of the concerns i had was using session() to store the data source if
it was sent to the client too in the ViewState data also.. thanks!


Steven Cheng said:
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
 
S

Steven Cheng[MSFT]

Hi Brian,

As for the Sesssion(SessionState), this is a user context based and which
is completely stored on the serverside. And the client keep its reference
to its serverside session by a SessionId. Generally, the sessionid is
stored in cookie. When the use request a page , the request will post with
this cookie so that the serverside can associate the proper session state
for the user.
And normally most sessionstate implement are in memory session, that means
t he session data all stored in the server's memory, so if we use session
too much , there may cause memory challenge to the server.

In conclusion, the ViewState store in the page content and will cause the
transfer issue when storing large data while the Session will cause server
memory concern, so we have to make our decision between them regarding on
our specified scenario.
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top