Page_Load Not Firing?

  • Thread starter Thread starter Rohan Parkes
  • Start date Start date
R

Rohan Parkes

I'm fairly new to ASP.NET, and am trying out the Online Newsletter
Manager project in Wrox's Beginning C#. The pages are supposed to bind
various controls (dropdown lists, datagrids) when the pages load,
through code in the Page_Load event.

However, it never works the first time the page loads. As far as I can
see, the Page_Load event doesn't fire, and if I set a break point, the
compiler says it won't be hit. It does work, however, when I click any
of the controls that fire isPostBack.

I haven't had this problem with other ASP.NET applications. The only
difference I can see is that all of the pages inherit from a base class
that just contains some utility functions.

I've reproduced some of the code below. I've seen similar code in some
of the ASP.NET examples in the SDK. I'm using the 1.0 framework.

I'd be grateful for any help.

--
Rohan Parkes
Melbourne
Australia

namespace NewsMailer
{
public class Lists : NewsMailer.BasePage
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
// bind the page's controls
BindGrid();
}
}

protected void BindGrid()
{
NewsMailer.ListsDB lists = new NewsMailer.ListsDB
(GetConnString());
// get all the lists
DataView myDV = lists.GetLists().Tables
[0].DefaultView;

// sort the data according to the SortExpression
value
if ( ListsGrid.Attributes["SortExpression"] != null
)
myDV.Sort = ListsGrid.Attributes
["SortExpression"];

ListsGrid.DataSource = myDV;
ListsGrid.DataBind();
}
 
In the InitializeComponent function (which resides in the "Web Form Designer
generated code" region), you should have a line which says:

this.Load += new System.EventHandler(this.Page_Load);

This line of code should be automatically generated, and tells the framework
to execute the Page_Load function when the page loads. If this line is
missing, the Page_Load function won't fire. Adding this line should solve
your problem.

Hope this helps,

Mun
 
In the InitializeComponent function (which resides in the "Web Form Designer
generated code" region), you should have a line which says:

this.Load += new System.EventHandler(this.Page_Load);

This line of code should be automatically generated, and tells the framework
to execute the Page_Load function when the page loads. If this line is
missing, the Page_Load function won't fire. Adding this line should solve
your problem.

Thanks - that was it. I can only assume that importing the pages messed
something up.
 
Back
Top