DataGrid.PageIndexChanged not firing on when 0th page requested

M

Marc Woolfson

Hello,

I have a Web Part that contains a relatively simple DataGrid, paginated
across four pages. Switching between pages 2, 3 and 4 works fine, but if you
request the first (0th) page again, the PageIndexChanged event does not fire.
Please see my code below:

1 public class ListOfDataWebPart : WebPart
2 {
3
4 protected DataGrid TestDataGrid;
5 protected DataSet pt_dgData;
6
7 /// <summary>
8 /// Prepare data to display in the datagrid on first page request
9 /// </summary>
10 /// <param name="e"></param>
11 protected override void OnLoad(EventArgs e)
12 {
13 base.OnLoad(e);
14
15 if (!this.Page.IsPostBack)
16 {
17 pt_dgData = GetDataSource();
18 ViewState["dgData"] = pt_dgData;
19 }
20 }
21
22 /// <summary>
23 /// Render the web part, and controls contained therein
24 /// </summary>
25 /// <param name="writer"></param>
26 protected override void Render(System.Web.UI.HtmlTextWriter
writer)
27 {
28 this.Title = "List of Data";
29 this.AllowClose = false;
30 this.AllowMinimize = false;
31 this.AllowEdit = false;
32
33 TestDataGrid.RenderControl(writer);
34
35 writer.Write("Current page shown in grid: " +
TestDataGrid.CurrentPageIndex);
36 }
37
38 /// <summary>
39 /// Add additional controls to the web part
40 /// </summary>
41 protected override void CreateChildControls()
42 {
43 GetDataGrid();
44
45 base.CreateChildControls();
46 }
47
48 /// <summary>
49 /// Create, populate, event handle and stylise the datagrid
control
50 /// </summary>
51 private void GetDataGrid()
52 {
53 // core grid
54 TestDataGrid = new DataGrid();
55 TestDataGrid.ID = "TestDG";
56 TestDataGrid.AllowPaging = true;
57 TestDataGrid.PageSize = 5;
58 TestDataGrid.PagerStyle.Mode = PagerMode.NumericPages;
59 TestDataGrid.AutoGenerateColumns = false;
60 TestDataGrid.EnableViewState = true;
61
62 // styles
63 TestDataGrid.CellPadding = 1;
64 TestDataGrid.GridLines = GridLines.None;
65 TestDataGrid.Width = new Unit(100, UnitType.Percentage);
66 TestDataGrid.ItemStyle.CssClass = "ms-vb2";
67 TestDataGrid.AlternatingItemStyle.CssClass =
"ms-alternating ms-vb2";
68 TestDataGrid.HeaderStyle.CssClass = "ms-vh2-nofilter";
69 TestDataGrid.PagerStyle.CssClass = "ms-menutoolbar
ms-toolbar";
70 TestDataGrid.PagerStyle.HorizontalAlign =
HorizontalAlign.Right;
71
72 // events
73 TestDataGrid.PageIndexChanged += new
DataGridPageChangedEventHandler(TestDataGrid_PageIndexChanged);
74
75 // columns
76 BoundColumn l_idCol = new BoundColumn();
77 l_idCol.HeaderText = "ID";
78 l_idCol.DataField = "ID";
79 TestDataGrid.Columns.Add(l_idCol);
80
81 BoundColumn l_ValueCol = new BoundColumn();
82 l_ValueCol.HeaderText = "Value";
83 l_ValueCol.DataField = "Value";
84 TestDataGrid.Columns.Add(l_ValueCol);
85
86 // rendering
87 RebindDG();
88 this.Controls.Add(TestDataGrid);
89 }
90
91 /// <summary>
92 /// Change the page displayed within the datagrid control
(seemingly fails to execute when 0th page requested)
93 /// </summary>
94 /// <param name="source"></param>
95 /// <param name="e"></param>
96 protected void TestDataGrid_PageIndexChanged(Object source,
DataGridPageChangedEventArgs e)
97 {
98 this.Page.Response.Write("New page requested from grid: " +
e.NewPageIndex);
99 TestDataGrid.CurrentPageIndex = e.NewPageIndex;
100 RebindDG();
101 }
102
103 /// <summary>
104 /// Rebind the datagrid to the data held in ViewState
105 /// </summary>
106 private void RebindDG()
107 {
108 pt_dgData = (DataSet)ViewState["dgData"];
109
110 TestDataGrid.DataSource = pt_dgData;
111 TestDataGrid.DataBind();
112 }
113
114 /// <summary>
115 /// Create a dummy dataset to test the control
116 /// </summary>
117 /// <returns>The DataSet to bind to the grid</returns>
118 private DataSet GetDataSource()
119 {
120 DataSet l_ds = new DataSet();
121 DataTable l_dt = new DataTable();
122 DataRow l_row;
123
124 l_dt.Columns.Add("ID", typeof(int));
125 l_dt.Columns.Add("Value", typeof(string));
126
127 // create 20 entries with the row number in the ID column
and a random string in the Value column
128 for (int i = 0; i < 20; i++)
129 {
130 l_row = l_dt.NewRow();
131 l_row[0] = i;
132 l_row[1] = Guid.NewGuid().ToString();
133 l_dt.Rows.Add(l_row);
134 }
135 l_ds.Tables.Add(l_dt);
136
137 return l_ds;
138 }
139
140 }

Writing the values back to the screen show that when the nth page is
requested the messages show the correct values, but the 0th page only shows
the current page number, not the requested one as well.

I have seen a few articles on problems with this event not firing at all,
but this doesn't seem to be my issue. The one post I found that seemed to
match this problem didn't have a reply :'( . Does anyone have any pointers?

Thanks,

Marc
 
A

Alvin Bruney [MVP]

I swear this question came up 3 weeks ago - there about. Have a google in
here for the solution.

--
Regards,
Alvin Bruney
------------------------------------------------------
Shameless author plug
Excel Services for .NET is coming...
https://www.microsoft.com/MSPress/books/10933.aspx
OWC Black Book www.lulu.com/owc
Professional VSTO 2005 - Wrox/Wiley


Marc Woolfson said:
Hello,

I have a Web Part that contains a relatively simple DataGrid, paginated
across four pages. Switching between pages 2, 3 and 4 works fine, but if
you
request the first (0th) page again, the PageIndexChanged event does not
fire.
Please see my code below:

1 public class ListOfDataWebPart : WebPart
2 {
3
4 protected DataGrid TestDataGrid;
5 protected DataSet pt_dgData;
6
7 /// <summary>
8 /// Prepare data to display in the datagrid on first page
request
9 /// </summary>
10 /// <param name="e"></param>
11 protected override void OnLoad(EventArgs e)
12 {
13 base.OnLoad(e);
14
15 if (!this.Page.IsPostBack)
16 {
17 pt_dgData = GetDataSource();
18 ViewState["dgData"] = pt_dgData;
19 }
20 }
21
22 /// <summary>
23 /// Render the web part, and controls contained therein
24 /// </summary>
25 /// <param name="writer"></param>
26 protected override void Render(System.Web.UI.HtmlTextWriter
writer)
27 {
28 this.Title = "List of Data";
29 this.AllowClose = false;
30 this.AllowMinimize = false;
31 this.AllowEdit = false;
32
33 TestDataGrid.RenderControl(writer);
34
35 writer.Write("Current page shown in grid: " +
TestDataGrid.CurrentPageIndex);
36 }
37
38 /// <summary>
39 /// Add additional controls to the web part
40 /// </summary>
41 protected override void CreateChildControls()
42 {
43 GetDataGrid();
44
45 base.CreateChildControls();
46 }
47
48 /// <summary>
49 /// Create, populate, event handle and stylise the datagrid
control
50 /// </summary>
51 private void GetDataGrid()
52 {
53 // core grid
54 TestDataGrid = new DataGrid();
55 TestDataGrid.ID = "TestDG";
56 TestDataGrid.AllowPaging = true;
57 TestDataGrid.PageSize = 5;
58 TestDataGrid.PagerStyle.Mode = PagerMode.NumericPages;
59 TestDataGrid.AutoGenerateColumns = false;
60 TestDataGrid.EnableViewState = true;
61
62 // styles
63 TestDataGrid.CellPadding = 1;
64 TestDataGrid.GridLines = GridLines.None;
65 TestDataGrid.Width = new Unit(100, UnitType.Percentage);
66 TestDataGrid.ItemStyle.CssClass = "ms-vb2";
67 TestDataGrid.AlternatingItemStyle.CssClass =
"ms-alternating ms-vb2";
68 TestDataGrid.HeaderStyle.CssClass = "ms-vh2-nofilter";
69 TestDataGrid.PagerStyle.CssClass = "ms-menutoolbar
ms-toolbar";
70 TestDataGrid.PagerStyle.HorizontalAlign =
HorizontalAlign.Right;
71
72 // events
73 TestDataGrid.PageIndexChanged += new
DataGridPageChangedEventHandler(TestDataGrid_PageIndexChanged);
74
75 // columns
76 BoundColumn l_idCol = new BoundColumn();
77 l_idCol.HeaderText = "ID";
78 l_idCol.DataField = "ID";
79 TestDataGrid.Columns.Add(l_idCol);
80
81 BoundColumn l_ValueCol = new BoundColumn();
82 l_ValueCol.HeaderText = "Value";
83 l_ValueCol.DataField = "Value";
84 TestDataGrid.Columns.Add(l_ValueCol);
85
86 // rendering
87 RebindDG();
88 this.Controls.Add(TestDataGrid);
89 }
90
91 /// <summary>
92 /// Change the page displayed within the datagrid control
(seemingly fails to execute when 0th page requested)
93 /// </summary>
94 /// <param name="source"></param>
95 /// <param name="e"></param>
96 protected void TestDataGrid_PageIndexChanged(Object source,
DataGridPageChangedEventArgs e)
97 {
98 this.Page.Response.Write("New page requested from grid: "
+
e.NewPageIndex);
99 TestDataGrid.CurrentPageIndex = e.NewPageIndex;
100 RebindDG();
101 }
102
103 /// <summary>
104 /// Rebind the datagrid to the data held in ViewState
105 /// </summary>
106 private void RebindDG()
107 {
108 pt_dgData = (DataSet)ViewState["dgData"];
109
110 TestDataGrid.DataSource = pt_dgData;
111 TestDataGrid.DataBind();
112 }
113
114 /// <summary>
115 /// Create a dummy dataset to test the control
116 /// </summary>
117 /// <returns>The DataSet to bind to the grid</returns>
118 private DataSet GetDataSource()
119 {
120 DataSet l_ds = new DataSet();
121 DataTable l_dt = new DataTable();
122 DataRow l_row;
123
124 l_dt.Columns.Add("ID", typeof(int));
125 l_dt.Columns.Add("Value", typeof(string));
126
127 // create 20 entries with the row number in the ID column
and a random string in the Value column
128 for (int i = 0; i < 20; i++)
129 {
130 l_row = l_dt.NewRow();
131 l_row[0] = i;
132 l_row[1] = Guid.NewGuid().ToString();
133 l_dt.Rows.Add(l_row);
134 }
135 l_ds.Tables.Add(l_dt);
136
137 return l_ds;
138 }
139
140 }

Writing the values back to the screen show that when the nth page is
requested the messages show the correct values, but the 0th page only
shows
the current page number, not the requested one as well.

I have seen a few articles on problems with this event not firing at all,
but this doesn't seem to be my issue. The one post I found that seemed to
match this problem didn't have a reply :'( . Does anyone have any
pointers?

Thanks,

Marc
 
M

Marc Woolfson

Hi Alvin,

I think I found the post you are referring to
(http://groups.google.co.uk/group/mi...read/thread/5d2d46c629871c9a/d0e60f550ec24b09)
but the original poster wasn't very generous in explaining the resolution,
and neither have other people
(http://groups.google.co.uk/group/mi...read/thread/a1038800585554d2/86d7d023a8b58a1c).
Another old post
(http://groups.google.co.uk/group/mi...b992a15f201/ccce5ffc1396079e#ccce5ffc1396079e)
had a similar issue but this was resolved by setting ViewState = true on the
DataGrid. Mine already has this setting.

The more I look at this, the more it seems that I am creating and binding
the dynamic control at the wrong stage in the ASP.NET life cycle. I have
tried putting trace messages everywhere and analysing the results, and it
seems that, as the last post above, the DataGrid gets recreated *twice* upon
most Postbacks. Should its creation be moved out of the CreateChildControls()
method and into (e.g.) the OnInit()?

Thanks,

Marc
 
M

Marc Woolfson

Hi again,

I found one post which resolved a similar issue by databinding the grid in
the PreRender event. I tried this, which seemed to fix the '0th page'
problem.

I still have an issue with multiple databinding upon postbacks which now
seems to be preventing my Command event from firing - or more precisely,
reporting it as firing in the trace - and having an effect on the rendered
grid. I also have a SortCommand event which as well as sorting the data
writes a value to ViewState. This is definitely getting executed as that
particular value is changing, but the grid is seemingly only sorting
correctly on the second databind, so the trace doesn't even show evidence of
the SortCommand itself being fired.

It would be great if MS provided an example of DataGrids as dynamic controls
with all relevant events/GUI controls working as desired...
 
S

Scott M.

Make sure that on the first page_load, you are calling the grid's databind
method. But, on subsequent loads, it should NOT be in Page_Load, but
instead have it in the various eventhandlers for your gird
(PageIndexChanged, EditCommand, DeleteCommand, UpdateCommand, SortCommand,
etc.).


Marc Woolfson said:
Hello,

I have a Web Part that contains a relatively simple DataGrid, paginated
across four pages. Switching between pages 2, 3 and 4 works fine, but if
you
request the first (0th) page again, the PageIndexChanged event does not
fire.
Please see my code below:

1 public class ListOfDataWebPart : WebPart
2 {
3
4 protected DataGrid TestDataGrid;
5 protected DataSet pt_dgData;
6
7 /// <summary>
8 /// Prepare data to display in the datagrid on first page
request
9 /// </summary>
10 /// <param name="e"></param>
11 protected override void OnLoad(EventArgs e)
12 {
13 base.OnLoad(e);
14
15 if (!this.Page.IsPostBack)
16 {
17 pt_dgData = GetDataSource();
18 ViewState["dgData"] = pt_dgData;
19 }
20 }
21
22 /// <summary>
23 /// Render the web part, and controls contained therein
24 /// </summary>
25 /// <param name="writer"></param>
26 protected override void Render(System.Web.UI.HtmlTextWriter
writer)
27 {
28 this.Title = "List of Data";
29 this.AllowClose = false;
30 this.AllowMinimize = false;
31 this.AllowEdit = false;
32
33 TestDataGrid.RenderControl(writer);
34
35 writer.Write("Current page shown in grid: " +
TestDataGrid.CurrentPageIndex);
36 }
37
38 /// <summary>
39 /// Add additional controls to the web part
40 /// </summary>
41 protected override void CreateChildControls()
42 {
43 GetDataGrid();
44
45 base.CreateChildControls();
46 }
47
48 /// <summary>
49 /// Create, populate, event handle and stylise the datagrid
control
50 /// </summary>
51 private void GetDataGrid()
52 {
53 // core grid
54 TestDataGrid = new DataGrid();
55 TestDataGrid.ID = "TestDG";
56 TestDataGrid.AllowPaging = true;
57 TestDataGrid.PageSize = 5;
58 TestDataGrid.PagerStyle.Mode = PagerMode.NumericPages;
59 TestDataGrid.AutoGenerateColumns = false;
60 TestDataGrid.EnableViewState = true;
61
62 // styles
63 TestDataGrid.CellPadding = 1;
64 TestDataGrid.GridLines = GridLines.None;
65 TestDataGrid.Width = new Unit(100, UnitType.Percentage);
66 TestDataGrid.ItemStyle.CssClass = "ms-vb2";
67 TestDataGrid.AlternatingItemStyle.CssClass =
"ms-alternating ms-vb2";
68 TestDataGrid.HeaderStyle.CssClass = "ms-vh2-nofilter";
69 TestDataGrid.PagerStyle.CssClass = "ms-menutoolbar
ms-toolbar";
70 TestDataGrid.PagerStyle.HorizontalAlign =
HorizontalAlign.Right;
71
72 // events
73 TestDataGrid.PageIndexChanged += new
DataGridPageChangedEventHandler(TestDataGrid_PageIndexChanged);
74
75 // columns
76 BoundColumn l_idCol = new BoundColumn();
77 l_idCol.HeaderText = "ID";
78 l_idCol.DataField = "ID";
79 TestDataGrid.Columns.Add(l_idCol);
80
81 BoundColumn l_ValueCol = new BoundColumn();
82 l_ValueCol.HeaderText = "Value";
83 l_ValueCol.DataField = "Value";
84 TestDataGrid.Columns.Add(l_ValueCol);
85
86 // rendering
87 RebindDG();
88 this.Controls.Add(TestDataGrid);
89 }
90
91 /// <summary>
92 /// Change the page displayed within the datagrid control
(seemingly fails to execute when 0th page requested)
93 /// </summary>
94 /// <param name="source"></param>
95 /// <param name="e"></param>
96 protected void TestDataGrid_PageIndexChanged(Object source,
DataGridPageChangedEventArgs e)
97 {
98 this.Page.Response.Write("New page requested from grid: "
+
e.NewPageIndex);
99 TestDataGrid.CurrentPageIndex = e.NewPageIndex;
100 RebindDG();
101 }
102
103 /// <summary>
104 /// Rebind the datagrid to the data held in ViewState
105 /// </summary>
106 private void RebindDG()
107 {
108 pt_dgData = (DataSet)ViewState["dgData"];
109
110 TestDataGrid.DataSource = pt_dgData;
111 TestDataGrid.DataBind();
112 }
113
114 /// <summary>
115 /// Create a dummy dataset to test the control
116 /// </summary>
117 /// <returns>The DataSet to bind to the grid</returns>
118 private DataSet GetDataSource()
119 {
120 DataSet l_ds = new DataSet();
121 DataTable l_dt = new DataTable();
122 DataRow l_row;
123
124 l_dt.Columns.Add("ID", typeof(int));
125 l_dt.Columns.Add("Value", typeof(string));
126
127 // create 20 entries with the row number in the ID column
and a random string in the Value column
128 for (int i = 0; i < 20; i++)
129 {
130 l_row = l_dt.NewRow();
131 l_row[0] = i;
132 l_row[1] = Guid.NewGuid().ToString();
133 l_dt.Rows.Add(l_row);
134 }
135 l_ds.Tables.Add(l_dt);
136
137 return l_ds;
138 }
139
140 }

Writing the values back to the screen show that when the nth page is
requested the messages show the correct values, but the 0th page only
shows
the current page number, not the requested one as well.

I have seen a few articles on problems with this event not firing at all,
but this doesn't seem to be my issue. The one post I found that seemed to
match this problem didn't have a reply :'( . Does anyone have any
pointers?

Thanks,

Marc
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top