DataGrid.PageIndexChanged not firing on when 0th page requested



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
.



Relevant Pages

  • RE: DataGrid doesnt fire PageIndexChanged
    ... i try what you said and it still doens't fire PageIndexChanged on the second ... i've an ASP.NET app that uses a datagrid control to show a database table. ... the first time i load my web page and change my page index to page 2 it all ... firing the proper PageIndexChanged event and rebind the datagrid ...
    (microsoft.public.dotnet.framework.aspnet.webcontrols)
  • Re: Integrated Authentication Global.asax
    ... I am using basic authentication on my site, which always prompts the user ... don't want an event that will fire for every request. ... > ther file extensionsf through the asp.net pipeline: ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Damn you, FEDEX! or Nikon D40 lost in Springfield, MO blackhole.
    ... the 2 mp Mavica he had been using with a Nikon D40. ... After shopping around, he got me to order one for him. ... The shipper had it insured, but from what I have read it could take weeks to sort this crap out. ... You may get your insurance from FedEx and a couple weeks later they find it and deliver it. ...
    (alt.photography)
  • Re: My Ideal (hopefully somewhat realistic) Setlist
    ... Streets of Fire ... Drift Away (sign request) ... with it he likes this song and will play it) ... The Wrestler (solo piano) ...
    (rec.music.artists.springsteen)
  • Re: Form Security
    ... If I am authorized to fire employees, then a request sent by me to fire ... a particular employee is successful. ... imagine that either of the following safeguards are implemented: ...
    (comp.lang.php)

Loading