开发者

asp.net partial classes

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


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...


Your second class does not have the partial modifier.

You need:

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


*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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜