locked
Partial class inheriting from another partial class? RRS feed

  • Question

  • Okay, lets suppose I have three pages

    - customsearch.aspx
    This is my main ASP.NET page. Lets suppose it has an asp:Literal called 'warningMsg'

    - searchtemplate.aspx.cs
    This is a 'base' class which defines some functionality and methods I want in all 'search' pages. It extends the 'Page' class, obviously.

    - customsearch.aspx.cs
    This extends the 'SearchTemplate' class, overriding a few selected methods and adding any custom ones I need.

    So first off, I do all of this using 1.1 style code behind:
    (only relevant code shown)

    - searchtemplate.aspx.cs
    [code]
    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
    }
    [/code]

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

      //other methods etc
    }
    [/code]


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


    So this all worked great. the warningMsg literal on the main page was indeed set to "Page Loaded Ok", as I'd hoped.

    Now, I decided to try the same thing using 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 searchtemplate.aspx.cs and declared it as a partial class.

    So I now have a partial class inheriting from another 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 searchtemplate.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.

    How can I make this work? Why is 'warningMsg' in scope if I use 1.1 style, but not 2.0 partial classes?
    Tuesday, October 24, 2006 3:28 PM

Answers

  •  AK_00 wrote:
    Okay, lets suppose I have three pages
    How can I make this work? Why is 'warningMsg' in scope if I use 1.1 style, but not 2.0 partial classes?

    The codegen and compilation model changed drastically in 2.0.

    A couple suggestions / workarounds:

    1) Try a CodeFileBaseClass attribute in your @ Page directive so ASP.NET will look for member fields to match with controls.

    2) Assume you have to use a technique like FindControl to initialize the field.

    3) Switch to the Web Application Project model, which codegens and compiles like 1.1.

    Monday, October 30, 2006 9:53 PM

All replies

  • Nobody has any ideas?
    Monday, October 30, 2006 10:39 AM
  • I'm not an expert on ASP.NET, but why have you removed the control declaration from SearchTemplate.aspx.cs?
    Monday, October 30, 2006 1:32 PM
  •  AK_00 wrote:
    Okay, lets suppose I have three pages
    How can I make this work? Why is 'warningMsg' in scope if I use 1.1 style, but not 2.0 partial classes?

    The codegen and compilation model changed drastically in 2.0.

    A couple suggestions / workarounds:

    1) Try a CodeFileBaseClass attribute in your @ Page directive so ASP.NET will look for member fields to match with controls.

    2) Assume you have to use a technique like FindControl to initialize the field.

    3) Switch to the Web Application Project model, which codegens and compiles like 1.1.

    Monday, October 30, 2006 9:53 PM
  •  Scott Allen wrote:

    1) Try a CodeFileBaseClass attribute in your @ Page directive so ASP.NET will look for member fields to match with controls.

    This did the trick. Thanks!

    Tuesday, October 31, 2006 2:07 PM