Link to home
Start Free TrialLog in
Avatar of alorentz
alorentzFlag for United States of America

asked on

ASP/ASP.Net Include files

I am moving into ASP.Net from ASP.  I use a lot of include files and would like to know how to do this effectively in .Net.  Most of the sites I do are constructed like building blocks.  For instance, the nav menu will be a stand alone page with HTML and Server Side Script, as well as the top and bottom of the page.  Each include file has it's own server side vbscript that builds the HTML or data for the specific include file.  Below is an example of a home page.

ASP:

<!--#include File="openpage.asp" -->
<!--#include File="openconns.asp" -->
<!--#include File="reset.asp" -->
<!--#include File="top.asp" -->

                 <%
                     server vbscript for this specific page
                   %>
                 <table WIDTH="100%" BORDER="0" CELLSPACING="0" CELLPADDING="3" id="TABLE2">
          <tr>
                 <td class=td_blue><b>Welcome to the Home Page</b></td>
          </tr>
      </table>
                                                                           
 <!--#include File="closeconns.asp" -->
<!--#include File="bottom.asp" -->
                           
.Net:
I've seen the use of <%@ Register TagPrefix="topMenu" TagName="topMenu" Src="topMenu.ascx" %> but this is not the best choice I hope.



The problem is that .aspx pages each have there on codebehind page, and it doesn't appear that you can have multiple code behind pages.  So for above example there would be codebehind for each of the include pages.  Don't think this will work?

Basically I would like to maintain the building block method.  Any suggestions would be appreciated!

I don't think this is very difficult for an experienced programmer so points are low.  If not let me know.

Avatar of mmarinov
mmarinov

in the .NET <!--#include File="page.asp" --> is replaced by User Controls
every User control is a file with its own code behid
the code you saw
<%@ Register TagPrefix="topMenu" TagName="topMenu" Src="topMenu.ascx" %>
is exactly the user control register tag in the aspx file
this is the best way to move from asp to asp.net

B..G
Avatar of YZlat
at the top of each include file put
<%@ Control Language="VB" EnableViewState="False" %>

and save them with .ascx extension

then in your main file put Register directives at the top of the page:
<%@ Register TagPrefix="UserControl1" TagName="Top" Src="top.ascx" %>
<%@ Register TagPrefix="UserControl2" TagName="Reset" Src="reset.ascx" %>
<%@ Register TagPrefix="UserControl3" TagName="OpenPage" Src="openpage.ascx" %>
<%@ Register TagPrefix="UserControl4" TagName="Conns" Src="openconns.ascx" %>

and between the <body> tags put

<body>
    <UserControl1:Top runat="server" />

    <UserControl2:Reset runat="server" />
    <UserControl3:OpenPage runat="server" />
    <UserControl4:Conns runat="server" />

  </body>


Avatar of alorentz

ASKER

YZlat -

That should work, but seems like a big pain in the ass!  Is there a better way to make modules global.  Like a menu, or header, so you don't have to rewrite the code on every page....which is not an option of course.
you don't have to rewrite the code, what you do is convert it to UserControl, meaning you'll put <%@ Control Language="VB" EnableViewState="False" %> at the top of the page and change the file extension to ascx. That's the only thing you can do
One more thing:  will each .ascx file reference the aspx.vb file as normal?

Because each .ascx page will have it's own directive at hte top to the appropriate aspx.vb file like:

top.ascx will have as first line:
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="top.aspx.vb" Inherits="localhost.top"%>

menu.ascx will have as first line:
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="menu.aspx.vb" Inherits="localhost.menu"%>

bottom.ascx will have as first line:
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="bottomp.aspx.vb" Inherits="localhost.bottom"%>

Regards.
the user control is file with extension ascx
so the Page tag has to contain Codebehind="yourusercontrolfilename.acsx.vb" no aspx.vb
but for your question - yes the .ascx file reference the ascx.vb as normal

B..G
ASKER CERTIFIED SOLUTION
Avatar of YZlat
YZlat
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I know this may be a bit late, but reading your question I came up with another possible option, or atleast an expansion of the posted solution.

You can put your global code into a class file such as "functions.cs" (or .vb).  Create a new public class called functions in the same namespace as your existing code.  Within that class you can create public static functions that can be access globally.  Here is an example:

namespace MyProgram
{
      public class MyFunctions
      {
            public MyFunctions()
            {

            }

            public static string DoSomething ()
            {
                  // CODE HERE
            }
      }
}

To access your functions in other areas of your program use the following:

      MyFunctions.DoSomething();

I hope this helps.