Solved

Inheritance and partial classes...

Posted on 2006-10-24
5
1,109 Views
Last Modified: 2008-01-09
Okay, so I have the following pages using 1.1 style code behind and inheritance fine, but not working with 2.0. Here is the 1.1 code:

- search.aspx.cs
public class SearchTemplate : System.Web.UI.Page
{
   public Literal warningMsg;
  //other controls here

  void Page_Load(Object sender, EventArgs e)
  {
    warningMsg.Text = "Page Loaded Ok";
    //other code here
  }

  //other methods etc
}

- customsearch.aspx.cs
public class CustomSearch : SearchTemplate
{
  //controls here

  //other methods etc
}

- customsearch.aspx
<%@ Page Language="C#" src="customsearch.aspx.cs" Inherits="CustomSearch" MasterPageFile="search.master" Buffer="true" %>
<!-- other page code here -->

So customsearch.aspx uses customsearch.aspx.cs as it's code behind, which inherits fromt he SearchTemplate class in search.cs. This is all fine. The warningMsg control in the customsearch.aspx page gets set on load, as you would expect.

So now, I decided to use partial classes, ASP.NET 2 style
 - I changed the customsearch.aspx header to use CodeFile= instead of src=
 - I removed the control declarations from customsearch.aspx.cs and declared it as a partial class
 - I removed the control declarations from search.aspx.cs and declared it as a partial class.


I am getting an error when I try to view the page - Object reference not set to an instance of an object. this is caused by the 'warningMsg=' line in the page load of search.aspx.cs. I know this because *everything* else has been commented out, and the page will load if I also comment out this line.

So why is this occuring? The warningMsg control does exist on the customsearch.aspx page, but seems to be 'out of scope' in the parent class of the code behind.

Why does it find the control on the page in 1.1 style, but tell me it doesn't exist in 2.0 style? What am I doing wrong?
0
Comment
Question by:AK_00
  • 2
5 Comments
 
LVL 20

Expert Comment

by:REA_ANDREW
ID: 17794311
when you say you changed to partial class did it look like this?

public partial class CustomSearch : System.Web.UI.Page, SearchTemplate
0
 
LVL 2

Author Comment

by:AK_00
ID: 17794328
No... it looks like:

public partial class CustomSearch : SearchTemplate

I figure it should inherit System.Web.UI.Page through SearchTemplate anyway, which reads:

public partial class SearchTemplate : System.Web.UI.Page


But I tried it anyway, and puitting:

public partial class CustomSearch : System.Web.UI.Page, SearchTemplate

will throw an error that I can't use multiple base classes.
0
 
LVL 2

Author Comment

by:AK_00
ID: 17842066
Have now solved this.

The solution was to use
CodeFileBaseClass="SearchTemplate"
in the customsearch.aspx header. This allows the controls to be found correctly.
0
 
LVL 1

Accepted Solution

by:
Computer101 earned 0 total points
ID: 17983607
PAQed with points refunded (500)

Computer101
EE Admin
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Connection String 16 61
How to close an asp.net web form using C# ? 4 31
performance issue in asp web 5 33
What are the simplest ways to create a Merge Sort? 3 15
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question