By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,133 Members | 1,113 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,133 IT Pros & Developers. It's quick & easy.

Data grid with current page combo box selector

P: n/a
Hi,

I have a data grid that is set up like this

Page items displayed = 10
EnableViewState = false (i dont want to send large amounts of data over the
internet!)
CustomPaging = false
AllowPaging = true

now at the bottom of the data grid page, i have the numbers showing up as
the paging method, i got it to switch pages and show the proper page when
enableviewstate = false was on, but what I need to do now is show a combo
box above the grid that says Page x of y is the current one showing then in
the list show all the pages availabe to select... that part I have done... i
based it on the pages count in the grid and the value of the item for the
combo box is the page index of the grid that page is associated with... but
all the attemps at making the newly selected item in the combo box change
the current page in the data grid have failed... i tried changeing the data
grid's curent page index and got nothing out of it... how would you go about
doing this?! thanks
Nov 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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>&nbsp;</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_SelectedIndexC hanged);
this.dgPage.PageIndexChanged += new
System.Web.UI.WebControls.DataGridPageChangedEvent Handler(this.dgPage_PageIn
dexChanged);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void dgPage_PageIndexChanged(object source,
System.Web.UI.WebControls.DataGridPageChangedEvent Args 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

Nov 18 '05 #2

P: n/a
Hi Brian,

Have you had a chance to check out the suggestions in my last reply or have
you got any further ideas on this issue? If you have anything unclear or if
there're anything else we can help, 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

Nov 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.