G
Guest
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 "NextPrevious" the NewPageIndex doesn't get updated in the
PageIndexChanging event. However if I change the web page to not inherit
from my base class but just from the System.web.ui.page 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="Default.aspx.cs"
Inherits="_Default" %>
<asp:GridView ID="GridView1" runat="server" AllowPaging="true"
OnPageIndexChanging="GridView1_PageIndexChanging">
<PagerSettings Mode="NextPrevious"></PagerSettings>
</asp:GridView>
Code Behind (scaled down and only showing the basics):
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
public partial class _Default : MyBasePage
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
GridView1.DataSource = GetDataSource();
GridView1.DataBind();
}
}
protected void GridView1_PageIndexChanging(object sender,
GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataSource = GetDataSource();
GridView1.DataBind();
}
private DataSet GetDataSource()
{
OleDbConnection cn = new
OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Ole DB Services=-4; Data
Source=C:\\Program Files\\Microsoft Visual Studio\\VB98\\nwind.mdb");
OleDbDataAdapter da = new OleDbDataAdapter("Select CompanyName,
ContactName, Address from Customers", cn);
DataSet customers = new DataSet();
da.Fill(customers, "Customers");
return customers;
}
}
public class MyBasePage : System.Web.UI.Page
{
HtmlForm objForm;
public MyBasePage()
{
objForm = new HtmlForm();
}
protected override void OnInit(System.EventArgs e)
{
BuildPage();
base.OnInit(e);
}
private void BuildPage()
{
for (int i = 0; i < this.Controls.Count; i++)
{
System.Web.UI.Control objCtrl = this.Controls[0];
objForm.Controls.Add(objCtrl);
this.Controls.Remove(objCtrl);
}
this.Controls.Add(objForm);
}
}
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 "NextPrevious" the NewPageIndex doesn't get updated in the
PageIndexChanging event. However if I change the web page to not inherit
from my base class but just from the System.web.ui.page 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="Default.aspx.cs"
Inherits="_Default" %>
<asp:GridView ID="GridView1" runat="server" AllowPaging="true"
OnPageIndexChanging="GridView1_PageIndexChanging">
<PagerSettings Mode="NextPrevious"></PagerSettings>
</asp:GridView>
Code Behind (scaled down and only showing the basics):
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
public partial class _Default : MyBasePage
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
GridView1.DataSource = GetDataSource();
GridView1.DataBind();
}
}
protected void GridView1_PageIndexChanging(object sender,
GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataSource = GetDataSource();
GridView1.DataBind();
}
private DataSet GetDataSource()
{
OleDbConnection cn = new
OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Ole DB Services=-4; Data
Source=C:\\Program Files\\Microsoft Visual Studio\\VB98\\nwind.mdb");
OleDbDataAdapter da = new OleDbDataAdapter("Select CompanyName,
ContactName, Address from Customers", cn);
DataSet customers = new DataSet();
da.Fill(customers, "Customers");
return customers;
}
}
public class MyBasePage : System.Web.UI.Page
{
HtmlForm objForm;
public MyBasePage()
{
objForm = new HtmlForm();
}
protected override void OnInit(System.EventArgs e)
{
BuildPage();
base.OnInit(e);
}
private void BuildPage()
{
for (int i = 0; i < this.Controls.Count; i++)
{
System.Web.UI.Control objCtrl = this.Controls[0];
objForm.Controls.Add(objCtrl);
this.Controls.Remove(objCtrl);
}
this.Controls.Add(objForm);
}
}