Solved

Inheritance and partial classes...

Posted on 2006-10-24
5
1,115 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

626 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