Paging does not work in a dynamic DataGrid

J

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_SortCommand);

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();
}
 
T

Teemu Keiski

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)
 
J

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();
 
T

Teemu Keiski

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
 
J

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.
 
T

Teemu Keiski

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/archive/2004/08/05/1444.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,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top