learningtech
learningtech

Reputation: 33695

Can't override preload because not function?

Another .net newbie question

I created a class that inherits System.Web.UI.Page with the intention of overriding the preload and Unload functions. But Visual Studios complains I can't override because "System.Web.UI.Page.PreLoad" is not a function. What's wrong with my code? Here it is:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;

public partial class BTPage : System.Web.UI.Page
{
    protected SqlConnection cnx;

    public override void PreLoad(object sender, EventArgs e)
    {
        cnx = new SqlConnection(ConfigurationManager.AppSettings["btdatabase"]);
        cnx.Open();
    }

    protected void Unload(object sender, EventArgs e)
    {
        cnx.Close();
    }
}

If there's anything else alarming about my code, please tell me. I'm new to .Net and I'm not sure if I'm doing things in the ".NET way".

What I also want to do is have every web page inherit from BTPage so that they all open a connection to the database on preload.

Upvotes: 1

Views: 2118

Answers (1)

Matthew Abbott
Matthew Abbott

Reputation: 61589

PreLoad is not a method of the Page type. You need to override OnPreLoad

Upvotes: 2

Related Questions