![]() | |
![]() |
| | Thread Tools | Search this Thread | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
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 |
#3
| |||
| |||
|
|
I swear this question came up 3 weeks ago - there about. Have a google in here for the solution. -- Regards, Alvin Bruney |
#4
| |||
| |||
|
|
I swear this question came up 3 weeks ago - there about. Have a google in here for the solution. -- Regards, Alvin Bruney |
#5
| |||
| |||
|
|
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 |
#6
| |||
| |||
|
|
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()? |
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
| |