Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Data grid with current page combo box selector

Reply
Thread Tools

Data grid with current page combo box selector

 
 
Brian Henry
Guest
Posts: n/a
 
      07-19-2004
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


 
Reply With Quote
 
 
 
 
Steven Cheng[MSFT]
Guest
Posts: n/a
 
      07-20-2004
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



 
Reply With Quote
 
 
 
 
Steven Cheng[MSFT]
Guest
Posts: n/a
 
      07-22-2004
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to reference one combo box from another combo box SM Javascript 2 04-28-2007 04:49 PM
How to update the current web page? (delete current web page + rewrite the page) laryten@yahoo.com Javascript 3 10-12-2006 07:33 PM
how to selecet check box in the data grid ?? only one check box mit ASP .Net 1 01-25-2006 06:47 PM
Fill Combo Box based on another Combo Box dthmtlgod ASP General 1 11-16-2005 04:08 PM
COMBO BOX INSTEAD OF DROP DOWN BOX =?Utf-8?B?QW5nZWw=?= ASP .Net 3 06-29-2004 06:38 PM



Advertisments