Question

I have a code behind file that is getting too large. I need to split up into multiple code behind files that control the same page. I am seeing it done with partial classes in C# but not in VB. I tried to implement the partial class concept, but it is not compiling or recognizing the page controls in the secondary file.

Here is my ASPX page directive:

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

Here is the code behind file Default.aspx.vb:

Partial Public Class _Default
    Inherits System.Web.UI.Page

   <!-- methods go here -->

End Class

The class _Default is growing too large. I want to move some of its methods into separate files to make it easier to read and maintain. I tried creating a separate file with another partial public class _Default in it.

Partial Public Class _Default

   <!--- methods go here -->

End Class

However, this second class could not recognize any of the server controls on Default.aspx (control tags marked with runat="server"). I have tried it with declared namespaces surrounding the classes to no avail.

How can I split my code behind file into multiple files to support compartmentalization?

    Update/Edit:

My question is the same as this question but for vb (should not be a difference but somehow there is).

Was it helpful?

Solution 2

@the_lotus -- I think as @Kashif mentioned above, the problem resides in being a website versus being a web application. My project is setup as a website. Web Applications compile into a single assembly which allows for access of aspx page members by multiple files. Websites compile into multiple assemblies, therefore, aspx page members are locked into their specified CodeFile. I did not realize this until I looked at your example and noticed that you used CodeBehind instead of CodeFile. I then referenced the Page Directive manpage on MSDN and looked at the difference between the two. The Web Site vs. Web Application concept was specified on that page. This lead me to understand what @Kashif was talking about in the above comments.

Long story short, I need to make this into a Web Application project in order for this to work. Thank you for you effort and the example you provided. It was most helpful!

OTHER TIPS

I just created a new web project in VS, created a new partial class for _Default and I could easely access all the controls. You might have namespace problem.

Default.aspx

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="WebApplication2._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:TextBox ID="tbTest" runat="server" />
    </div>
    </form>
</body>
</html>

Default.aspx.vb

Partial Public Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Test()

    End Sub

End Class

Default.aspx.Designer.vb

Partial Public Class _Default

    Protected WithEvents form1 As Global.System.Web.UI.HtmlControls.HtmlForm

    Protected WithEvents tbTest As Global.System.Web.UI.WebControls.TextBox
End Class

Newly created test.vb

Partial Public Class _Default

    Public Sub Test()

        tbTest.Text = "123"

    End Sub

End Class
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top