473,385 Members | 1,445 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Paging does not work in a dynamic DataGrid

js
I am having a problem with a dynamicly added DataGrid that always shows
the first data page even the message in the DataGrid.PreRender shows
the correct datapage as the DropDownList control's SelectedIndex. The
DataGrid CurrentPageIndex is controlled by a DropDownList control.
Anyone knows what might be going wrong? Thanks.

The addDataGrid() is fired by the following 3 events:

private void btnLookup_Click(object sender, System.EventArgs e)
private void cboPager_SelectedIndexChanged(object sender, EventArgs e)
private void dgResult_SortCommand(object source,
DataGridSortCommandEventArgs e)

The datagrid has the following attributes:

private DataGrid addDataGrid()
{
dgResult = new DataGrid();
try
{
dgResult.ID = "dgResult";
dgResult.EnableViewState = true;
dgResult.CellSpacing = 0;
dgResult.AutoGenerateColumns = false;
dgResult.AllowSorting = true;
dgResult.PageSize = 50;
dgResult.AllowPaging = true;
dgResult.PagerStyle.Visible = false;
dgResult.AllowCustomPaging = true;
dgResult.DataBinding += new EventHandler(dgResult_DataBinding);
dgResult.PreRender += new EventHandler(dgResult_PreRender);
dgResult.ItemDataBound += new
DataGridItemEventHandler(dgResult_ItemDataBound);
dgResult.ItemCreated += new
DataGridItemEventHandler(dgResult_ItemCreated);
dgResult.SortCommand += new
DataGridSortCommandEventHandler(dgResult_SortComma nd);

bindDataGrid();
}
catch(Exception ex)
{
showMessage(ex.GetType().ToString() + "<br/>" +
ex.Message + "<br/>" +
ex.StackTrace);
}
return dgResult;
}

private void bindDataGrid()
{
try
{
cForm = FindControl("frmReference");

oReference.referenceType = getReferenceTableName();
oReference.tableSchema = strTableSchema;

TextBox txtTypeValue = (TextBox) cForm.FindControl("txtTypeValue");
if (txtTypeValue != null)
oReference.referenceValue = oUtil.encodeIt(txtTypeValue.Text);
dgResult.DataSource = oReference.Lookup2();
intGridDataSourceRowCount = ((DataSet)
dgResult.DataSource).Tables[0].Rows.Count;
dgResult.CurrentPageIndex = intSelectedPageIndex;
dgResult.DataBind();
intPageCount = dgResult.PageCount;

dgResult.DataKeyField = getDataKeyField();
dgResult.Visible =true;

}
catch(Exception ex)
{
showMessage(ex.GetType().ToString() + "<br/>" +
ex.Message + "<br/>" +
ex.StackTrace);
}
}

private void dgResult_PreRender(object sender, EventArgs e)
{
//this just to confirm that the CurrentPageIndex is on the right
index
showMessage("(Prerender) CurrentPageIndex =" +
dgResult.CurrentPageIndex.ToString());
}

private void dgResult_DataBinding(object sender, EventArgs e)
{
dgResult.VirtualItemCount = intGridDataSourceRowCount;
}

private void cboPager_SelectedIndexChanged(object sender, EventArgs e)
{
intSelectedPageIndex = ((DropDownList) sender).SelectedIndex;
cForm = this.FindControl("frmReference");
RequiredFieldValidator cValidator = (RequiredFieldValidator)
cForm.FindControl("rfvTypeValue");
cValidator.Enabled = false;
addDataGrid();
}

Nov 19 '05 #1
5 3375
Hi,

where do you add the dataGrid to the Controls collection?

Rule 1. Add it before you do anything with the properties which you want to
nbe preserved in ViewState over the postback

Rule 2,. Add dynamical control on every request (on postback as well) and
add it to the same place in Controls collection

Rule 3. Add the dynamical control in the Page_Load event at the latest,
otherwise it is uable to load relevenat postback data and update its state
(e.g throw events)

--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU
http://blogs.aspadvice.com/joteke
Nov 19 '05 #2
js
Thanks for the reponse. There are 3 places that call the
addDataGrid(). On the page there are 6 Buttons, 1 Textbox, and 1
RequiredFieldValidation in the HTML portion of the page. All buttons
except the "Lookup", do not need to addDataGrid. The "Lookup" button is
wired to btnLookup_Click that calls addDataGrid(). When the DataGrid
diplays the lookup result, a DropDownList control is displayed and
wired to cboPager_SelectedIndexChanged event that fires addDataGrid().
The last place that calls the addDataGrid() is in the
dgResult_SortCommand.

For Rule 1, I think I add the DataGrid prior to setting its properties.
For Rule 2, yes, I am adding the DataGrid on every post back because
one of the 3 events will cause post back.
For Rule 3, there is nothing is Page_Load event. I tried the following
code in Page_Load and ended up getting two DataGrid:

if (IsPostBack)
addDataGrid();

Nov 19 '05 #3
But it is the rule number 3 you break when you add the DataGrid to Controls
collection after Page_load. Note that postback events like btnLookup_Click
or cboPager_SelectedIndexChanged or dgResult_SortCommand happen after
Page_Load.

For the first time when the grid is added to the Controls collection (when
it is constructed for the very first time), it is OK, to add dynamical
control in postback event. But from that point on following subsequent
postbacks, it must be recreated at the Page_Load (the latest possible event
in lifecycle to add dynamical control)

Read this carefully:

Dynamic Web Controls, Postbacks, and View State
http://aspnet.4guysfromrolla.com/articles/092904-1.aspx
--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU
http://blogs.aspadvice.com/joteke
Nov 19 '05 #4
js
OK.

I execute addDataGrid after IsPostBack in the Page_Load I got multiple
DataGrids. It seems to me that any code in Page_Load that would render
UI on the page will be executed twice. Therefore, checking the
IsPostBack is not sufficient enough to determine if the DataGrid needs
to be added. How do I know in the Page_Load if the postback is caused
by btnLookup click, cboPager change, or grid column click event?

Thank you very much.

Nov 19 '05 #5
Hi,

First of all, in the beginning of the addDataGrid() routine call
Controls.Clear() for the container where you add the grid, that way there
won't be many of them. Say you add it to PlaceHolder2 then call
PlaceHolder2.Controls.Clear(). THis ensures that there's not multitude of
those controls.

You don't necessarily need to do it that way (by checking in Page_load what
caused the postback), if you at the end the first creation, set for example
a flag (bool value) to ViewState. In Page_Load (when it is a postback) check
if this flag is true and then if it is, call the creation method again.
This way conterol recreation will keep up and if you need to stop the
creation, just set the flag off (false).

Anyways, if you want to know how to check the postbacking control Page_Load,
here's one way:
http://blogs.aspadvice.com/joteke/ar...8/05/1444.aspx

--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU
http://blogs.aspadvice.com/joteke
Nov 19 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Stephen | last post by:
This is a real brain-teaser and i'd really appreciate it if someone can try and understand what im trying to do and give me a few pointers or ideas to help me work out my problem. Im basically...
2
by: Max | last post by:
Is it possible or more effecient to use a stored procedure to populate a datagrid when using datagrid or custom paging? Is it (ADO.NET?) pulling the entire table into the dataset or is it just...
2
by: Steven K | last post by:
Hello, I am converting an ASP page to ASP.Net. In one page, I am using record paging. I have read that while the DataGrid control has paging, it can tax resources. So I did a Google and found...
2
by: RJN | last post by:
Hi Sorry for posting again. I have a datagrid which is put inside a div tag to make it scrollable. I need to page the datagrid. The page numbers appear at the bottom of the datagrid and has...
3
by: Pat | last post by:
I have a 2 nested Datagrid. When i select a row in the Master Datagrid i populate the Child databrid using myDataGrid.SelectedIndex value as the filter and setting the DataKeyField. I enabled...
1
by: Steve Wonderful | last post by:
Hi, I have dynamically created a datagrid by: 1. created a number of columns from reading a column difinition so I can dynamically create the columns I want to display. 2. bind to a...
0
by: richard | last post by:
OK, Im finished pulling my hair out and now I need help. I have created a VB class file for my custom paging, in it I create a table with 2 rows, in the rows I have linkbuttons for first page,...
5
by: Donald Adams | last post by:
Hi, I will have both web and win clients and would like to page my data. I could not find out how the datagrid control does it's paging though I did find some sample code that says they do it...
7
by: =?Utf-8?B?SmVmZiBCZWVt?= | last post by:
The default paging behavior of the gridview doesn't work well with very large sets of data which means we have to implement some sort of custom paging. The examples I have seen (4guysfromrolla,...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.