Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Inheritance and partial classes...

Posted on 2006-10-24
5
Medium Priority
?
1,116 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

688 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