HighTechTalks DotNet Forums  

DataGrid.PageIndexChanged not firing on when 0th page requested

ASP.net Data Grid Control microsoft.public.dotnet.framework.aspnet.datagridcontrol


Discuss DataGrid.PageIndexChanged not firing on when 0th page requested in the ASP.net Data Grid Control forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Marc Woolfson
 
Posts: n/a

Default DataGrid.PageIndexChanged not firing on when 0th page requested - 05-23-2007 , 06:11 AM






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_PageI ndexChanged);
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

Reply With Quote
  #2  
Old   
Alvin Bruney [MVP]
 
Posts: n/a

Default Re: DataGrid.PageIndexChanged not firing on when 0th page requested - 05-23-2007 , 07:55 PM






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" <MarcWoolfson (AT) discussions (DOT) microsoft.com> wrote

Quote:
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_PageI ndexChanged);
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



Reply With Quote
  #3  
Old   
Marc Woolfson
 
Posts: n/a

Default Re: DataGrid.PageIndexChanged not firing on when 0th page requeste - 05-24-2007 , 04:20 AM



Hi Alvin,

I think I found the post you are referring to
(http://groups.google.co.uk/group/mic...e60f550ec24b09)
but the original poster wasn't very generous in explaining the resolution,
and neither have other people
(http://groups.google.co.uk/group/mic...7d023a8b58a1c).
Another old post
(http://groups.google.co.uk/group/mic...ce5ffc1396079e)
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

"Alvin Bruney [MVP]" wrote:

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

--
Regards,
Alvin Bruney


Reply With Quote
  #4  
Old   
Marc Woolfson
 
Posts: n/a

Default Dynamic DataGrid databinding/event handling - 05-24-2007 , 05:40 AM



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

"Alvin Bruney [MVP]" wrote:

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

--
Regards,
Alvin Bruney


Reply With Quote
  #5  
Old   
Scott M.
 
Posts: n/a

Default Re: DataGrid.PageIndexChanged not firing on when 0th page requested - 05-25-2007 , 07:59 AM



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" <MarcWoolfson (AT) discussions (DOT) microsoft.com> wrote

Quote:
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_PageI ndexChanged);
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



Reply With Quote
  #6  
Old   
MasterGaurav \(www.edujini-labs.com\)
 
Posts: n/a

Default Re: DataGrid.PageIndexChanged not firing on when 0th page requeste - 06-04-2007 , 04:54 AM



Quote:
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()?

No.
CreateChildControls is the place where all controls have to be created...

Also just ensure that the data-grid is not bound everytime in Page_Load!
Also ensure that the data-grid is bound for each command - Sort, Edit,
Delete, Cancel, PagIndexChanged etc.


--
Happy Hacking,
Gaurav Vaish | www.mastergaurav.com
www.edujini-labs.com
http://eduzine.edujini-labs.com
-----------------------------------------




Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.