473,546 Members | 2,249 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

NewPageIndex Doesn't Get Updated in PageIndexChangi ng Event

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);
}
}
Jun 15 '06 #1
3 6468
Hi,

Thank you for your post.

First, this problem can be fixed by using following BuildPage() function:

private void BuildPage()
{
ArrayList al = new ArrayList();
foreach(Control c in Controls)
{
al.Add(c);
}
Controls.Add(ob jForm);

foreach(Control c in al)
{
objForm.Control s.Add(c);
}
}

Following are detailed causes:

1) The root cause of this problem is GridView's PageIndex is stored using
ASP.NET 2.0's new feature called "ControlSta te". See following MSDN for
more info:
http://msdn2.microsoft.com/en-us/lib...tepersister.co
ntrolstate.aspx

A server control that use control state must call the
RegisterRequire sControlState method on each request because registration
for control state is not carried over from request to request during a
postback event. It is recommended that registration occur in the Init event.

GridView calls this method in its OnInit:

protected internal override void OnInit(EventArg s e)
{
base.OnInit(e);
if (this.Page != null)
{
if ((this.DataKeyN ames.Length > 0) &&
!this.AutoGener ateColumns)
{
this.Page.Regis terRequiresView StateEncryption ();
}
this.Page.Regis terRequiresCont rolState(this);
}
}

Please note that it checks for "this.Page != null" first.

2) When adding a control to a ControlCollecti on, it will automatically
first remove it from its old parent.Controls (if its parent is not null),
and removing a control will Unload it first!

In previous code, when we first add the GridView to objForm.Control s,
because objForm is not added to Page.Controls yet, the "this.Page != null"
will be false for the GridView, thus not calling the
RegisterRequire sControlState.

So, what we are doing now is first adding the objForm to Page.Controls,
then add the remaining controls to objForm, this will ensure GridView's
ControlState is persisted.

As a side note, in ASP.NET 2.0, we have a new feature called MasterPage
which is exactly for the purpose of keeping a consistent look and feel for
the entire website.

Hope this helps. Please feel free to post here if anything is unclear.

Regards,
Walter Wang
Microsoft Online Community Support

=============== =============== =============== =====
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
=============== =============== =============== =====

This posting is provided "AS IS" with no warranties, and confers no rights.

Jun 16 '06 #2
Walter, thanks for the reply. That fixed it. However, this brings up
another question. We currently use this base page for our 1.1 apps. We have
some groups in our company that are trying to use our base page compiled with
1.1 in their 2.0 web apps. This is the first issue we have ran into with
trying to use our 1.1 base code in a 2.0 app. If I go change our base page
to load the form into the page object before I load any controls into the
form collection, will this cause any problems with any of the 1.1 controls?

Also, in response to your side note, I wanted to use the Master pages but
unfortunately our base page is in a class library in a seperate assembly
rather than in the web application itself so there was no way to use the
master pages. At least this is what Microsoft told us. We distribute this
assembly throughout our company to let other development teams use in order
to have the corporate look and feel.

"Walter Wang [MSFT]" wrote:
Hi,

Thank you for your post.

First, this problem can be fixed by using following BuildPage() function:

private void BuildPage()
{
ArrayList al = new ArrayList();
foreach(Control c in Controls)
{
al.Add(c);
}
Controls.Add(ob jForm);

foreach(Control c in al)
{
objForm.Control s.Add(c);
}
}

Following are detailed causes:

1) The root cause of this problem is GridView's PageIndex is stored using
ASP.NET 2.0's new feature called "ControlSta te". See following MSDN for
more info:
http://msdn2.microsoft.com/en-us/lib...tepersister.co
ntrolstate.aspx

A server control that use control state must call the
RegisterRequire sControlState method on each request because registration
for control state is not carried over from request to request during a
postback event. It is recommended that registration occur in the Init event.

GridView calls this method in its OnInit:

protected internal override void OnInit(EventArg s e)
{
base.OnInit(e);
if (this.Page != null)
{
if ((this.DataKeyN ames.Length > 0) &&
!this.AutoGener ateColumns)
{
this.Page.Regis terRequiresView StateEncryption ();
}
this.Page.Regis terRequiresCont rolState(this);
}
}

Please note that it checks for "this.Page != null" first.

2) When adding a control to a ControlCollecti on, it will automatically
first remove it from its old parent.Controls (if its parent is not null),
and removing a control will Unload it first!

In previous code, when we first add the GridView to objForm.Control s,
because objForm is not added to Page.Controls yet, the "this.Page != null"
will be false for the GridView, thus not calling the
RegisterRequire sControlState.

So, what we are doing now is first adding the objForm to Page.Controls,
then add the remaining controls to objForm, this will ensure GridView's
ControlState is persisted.

As a side note, in ASP.NET 2.0, we have a new feature called MasterPage
which is exactly for the purpose of keeping a consistent look and feel for
the entire website.

Hope this helps. Please feel free to post here if anything is unclear.

Regards,
Walter Wang
Microsoft Online Community Support

=============== =============== =============== =====
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
=============== =============== =============== =====

This posting is provided "AS IS" with no warranties, and confers no rights.

Jun 16 '06 #3

Hi,

Thank you for your update and I'm glad that the suggestion worked.

Based on my research, adding an HtmlForm instance first is the preferred
way to mimic a normal ASP.NET page, since Control.Page property will use
parent to find the Page reference. If you add controls to HtmlForm first,
their Page reference are null and you don't know if they will check this
reference in OnInit or not. In a word, this "advanced" Page inheritance
technique is not officially supported so we have to test to see if it
really works.

As for the MasterPage issue, unfortunately it does need the *.master source
to be distributed.

Please feel free to post here if there's anything I can help.

Regards,
Walter Wang
Microsoft Online Community Support

=============== =============== =============== =====
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
=============== =============== =============== =====

This posting is provided "AS IS" with no warranties, and confers no rights.

Jun 17 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
6531
by: Lars-Erik Aabech | last post by:
Hi! I've got problems with serializing my collections of business objects. The objects themselves serialize fine, but the collections fail. I've got the following structure: Base collection class: Derives MarshalByValueComponent Implements ICollection, IList and ISerializable Explicitly implements the IList methods as private members,...
12
8673
by: Jim Hammond | last post by:
I am passing the whole object instead or parameters in my select and update methods. I can get the updated object if I set UpdateMethod, let ASP.NET autogenerate an update button, and then press update after making changes, but I don't want that update button. How can I get the updated object when the user presses one of my other action...
5
2357
by: Bob | last post by:
I've got a .NET Framework V1.1 web service running on a Windows 2003 Server that has 2 web methods that are called from a web application on the same server. One is a fire-and-forget method that executes a long running process that results in a database being updated. The other is a normal syncronous method that returns configuration...
6
6499
by: Carlos Albert | last post by:
Hi everybody, I'm working with a gridview (4 bound columns and 1 template column, using databind from codebehind). It works just fine, but I tried to add paging, and when I click any paging button (next, last, or page #), it return this error and I don't know what's wrong: Source Error: An unhandled exception was generated during the...
1
9947
by: Jason Huang | last post by:
Hi, In my C# 2.0 web form project, I have GridView1 in Form1.aspx. I am wondering why I can't find the PageIndexChanging event in the Form1.aspx.cs but the GridView1 still can go to other pages. So, where exactly does the C# 2.0 web handle the PageIndexChanging thing? Thanks for help.
1
4725
by: Stuart Shay | last post by:
Hello All: I am working with a FormView Control <asp:FormView ID="FormView1" DataKeyNames="Id,Date" runat="server"/> In the FormView1_PageIndexChanging(object sender, FormViewPageEventArgs e) How do I get the DataKeys for the New Page Index Selected ?
2
3089
by: napstar | last post by:
I have a gridview in a web form and when I build the website I get this error:"The GridView 'PatientGridView' fired event PageIndexChanging which wasn't handled" Can any one helpe me out with this?
2
2785
shek124
by: shek124 | last post by:
In my form, the gridview is bind , according to my selectedvalue in dropdownlist How can do paging in gridview using the pageindexchanging event for this condition.. please hwlp me
0
7435
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7694
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7461
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7792
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6026
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5080
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3470
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1046
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
747
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.