0

I want to create two partial classes for the single aspx file. I am using vs2005 dotnet 2.0. i could not able to access method from one partial class in another partial classes.

Can anybody assist me.

Partial class 1 : my main aspx page

public partial class _Default : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) { } 
    private void meth() { } 

} 

Partial class : 2

public class _Default : System.Web.UI.Page 
{ 
    public _Default() { } 
} 

i could not able to access meth method in partial class 2

codeulike
  • 22,514
  • 29
  • 120
  • 167
muthukumarm
  • 197
  • 4
  • 14
  • 1
    can you please post the class definition here? – Zain Shaikh Nov 12 '10 at 07:26
  • 1
    make sure both of your classes have `same name` and `partial` modifier and extend `System.Web.UI.Page` – Zain Shaikh Nov 12 '10 at 07:27
  • i have the same , what you said.. but still it didnt work out. Partial class 1 : my main aspx page public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } private void meth() { } } Partial class : 2 public class _Default : System.Web.UI.Page { public _Default() { } } i could not able to access meth method in partial class 2. – muthukumarm Nov 12 '10 at 07:32

3 Answers3

3

You should use the partial modifier for both class declarations, like so:

// Default.aspx.cs
public partial class _Default : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) { } 
    private void meth() { } 
} 

// Default_Part2.aspx.cs
public partial class _Default : System.Web.UI.Page 
{ 
    public _Default() 
    { 
        meth();
    }
} 

In VS 2005, it may be also necessary to update the aspx file's dependencies, found here:

http://aspnetresources.com/blog/partial_class_files_in_vs2k5

In short, open the project file with a plain text editor, and look for:

<Compile Include="Default_Part2.aspx.cs">
    <SubType>ASPXCodeBehind</SubType>
</Compile>

Add your file like so:

<Compile Include="Default_Part2.aspx.cs">
   <SubType>ASPXCodeBehind</SubType>
   <DependentUpon>Default.aspx</DependentUpon>
</Compile>

I don't know if this will work, as I don't have VS 2005 to test it on. Hope it helps though...

Ioannis Karadimas
  • 7,746
  • 3
  • 35
  • 45
  • i missed the partial modifier in the comment. but i am using the same code as shown above. it is throwing compile time error says Meth() does not exists in the current context. More over i could able to access System.Web.UI.Page for inheriting in the second class. My second class is available in .cs file. please help... – muthukumarm Nov 12 '10 at 08:29
  • In the second class declaration you posted above, you missed the "partial" modifier. Did you also miss it in your code, or just in your post? – Ioannis Karadimas Nov 12 '10 at 08:47
  • File Name : Default.aspx.cs ---------------------------- public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } private void meth() { } } FileName : Class1.cs ----------------------- public partial class _Default : System.Web.UI.Page { public _Default() { meth(); } } – muthukumarm Nov 12 '10 at 08:48
  • Do the classes share the same namespace? – Ioannis Karadimas Nov 12 '10 at 08:49
  • can u send me the sample project which contains partial classes of single aspx file. email id : bolforever@yahoo.co.in – muthukumarm Nov 12 '10 at 08:53
  • Hi ..i got it..but u r using vs2008....i am using vs2005...Even i got it in vs2008...i could not able to get it in vs2005 dotnet2.0... – muthukumarm Nov 12 '10 at 09:39
  • did u try in dotnet2.0 vs2005? – muthukumarm Nov 12 '10 at 09:43
2

Your second class does not have the partial modifier.

You need:

public partial class _Default : System.Web.UI.Page 
{ 
    public _Default() { } 
} 
codeulike
  • 22,514
  • 29
  • 120
  • 167
1

*Something About Partial Class:-*

All the partial definitions must proceeded with the key word "Partial". All the partial types meant to be the part of same type must be defined within a same assembly and module. Method signatures (return type, name of the method, and parameters) must be unique for the aggregated typed (which was defined partially). The partial types must have the same accessibility. If any part is sealed, the entire class is sealed. If any part is abstract, the entire class is abstract. Inheritance at any partial type applies to the entire class.