We have a base class that is responsible for creating the navigation and look
and feel of our applications. So all our web pages inherit from this base
page so they don't have to worry about the look and feel and navigation, only
the main content. Everything has woked just fine until we converted to 2.0.
Now we have a problem with the new GridView and the paging. With the
DataGrid in 1.1, the paging worked just fine. Now with paging turned on and
the mode set to "NextPrevio us" the NewPageIndex doesn't get updated in the
PageIndexChangi ng event. However if I change the web page to not inherit
from my base class but just from the System.web.ui.p age then the paging works
correctly. This is a code snipit of our base class and how it loads the
controls from the derived page. I am really stumped on this one so if anyone
has any ideas it would be greatly appreciated.
ASPX Page:
<%@ Page Language="C#" AutoEventWireup ="true" CodeFile="Defau lt.aspx.cs"
Inherits="_Defa ult" %>
<asp:GridView ID="GridView1" runat="server" AllowPaging="tr ue"
OnPageIndexChan ging="GridView1 _PageIndexChang ing">
<PagerSetting s Mode="NextPrevi ous"></PagerSettings>
</asp:GridView>
Code Behind (scaled down and only showing the basics):
using System;
using System.Data;
using System.Configur ation;
using System.Web;
using System.Web.Secu rity;
using System.Web.UI;
using System.Web.UI.W ebControls;
using System.Web.UI.W ebControls.WebP arts;
using System.Web.UI.H tmlControls;
using System.Data.Ole Db;
public partial class _Default : MyBasePage
{
protected void Page_Load(objec t sender, EventArgs e)
{
if (!Page.IsPostBa ck)
{
GridView1.DataS ource = GetDataSource() ;
GridView1.DataB ind();
}
}
protected void GridView1_PageI ndexChanging(ob ject sender,
GridViewPageEve ntArgs e)
{
GridView1.PageI ndex = e.NewPageIndex;
GridView1.DataS ource = GetDataSource() ;
GridView1.DataB ind();
}
private DataSet GetDataSource()
{
OleDbConnection cn = new
OleDbConnection ("Provider=Micr osoft.Jet.OLEDB .4.0; Ole DB Services=-4; Data
Source=C:\\Prog ram Files\\Microsof t Visual Studio\\VB98\\n wind.mdb");
OleDbDataAdapte r da = new OleDbDataAdapte r("Select CompanyName,
ContactName, Address from Customers", cn);
DataSet customers = new DataSet();
da.Fill(custome rs, "Customers" );
return customers;
}
}
public class MyBasePage : System.Web.UI.P age
{
HtmlForm objForm;
public MyBasePage()
{
objForm = new HtmlForm();
}
protected override void OnInit(System.E ventArgs e)
{
BuildPage();
base.OnInit(e);
}
private void BuildPage()
{
for (int i = 0; i < this.Controls.C ount; i++)
{
System.Web.UI.C ontrol objCtrl = this.Controls[0];
objForm.Control s.Add(objCtrl);
this.Controls.R emove(objCtrl);
}
this.Controls.A dd(objForm);
}
}