Solved

ASP.NET VB: How to properly use Includes??

Posted on 2007-12-04
20
519 Views
Last Modified: 2013-11-07
I am a PHP programmer. I'm used to using an include command and all the code on that page executes, wherever I put it on the page. I am having a slightly different experience with ASP.NET. I've only been using it for less than a year and let's just say it's not my "language of choice", unless someone can explain how these blasted includes work....

Attached is some code to show what I was doing, which I thought was working, but apparently is not... I'm pretty sure the .vb code is not getting executed at all.

The actual file is there, however nothing in the codebehind file gets executed. I've got it all under

      Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


This is very frustrating. I'm not even sure that file includes are my problem. I have a section of code that either shows/hide an ASP:Panel based on an if statement. This code sits inside the header.aspx.vb file. When it's there, the page loads fine, however doesn't execute the code. I've explicitly set it to FALSE and it supposed to hide 1 panel and show another, but doesn't.

If I try to move it the main "holder" page, like default.aspx, it tells me it can't find the ASP:Panel. I would think that if I wanted to control ANYTHING, as long as I put it ABOVE the actual control, it shouldn't matter which file (there goes my PHP thinking again).

So what exactly is the best way to do this? I cannot figure this out.
'First line of Default.aspx

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

'This is how i thought includes work:

<%@ Register TagPrefix="atcu" TagName="header" Src="~/header.ascx" %>
 

'then on the page somewhere:

<atcu:header id="header1" runat="server"></atcu:header>
 
 

'this is how my header.aspx file looks:

<%@ Control Language="vb" AutoEventWireup="false" Codebehind="header.ascx.vb" TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>

Open in new window

0
Comment
Question by:bdichiara
  • 10
  • 6
  • 4
20 Comments
 
LVL 8

Accepted Solution

by:
Chumad earned 250 total points
ID: 20403273
Have you considered using master pages to get your layout and content in? I'd imagine that's much easier than trying to include each portion of the page such as header, footer, etc.
0
 
LVL 8

Expert Comment

by:Chumad
ID: 20403480
In any case, I'll make an attempt to answer your questions. First of all though, PHP and ASP.NET are night and day different languages. I was a PHP coder prior to discovering ASP.NET and have not looked back at PHP since. Basically, take everything you know about PHP and toss it out the window - it won't be very useful in ASP.NET environment.

In .NET, there are really no such thing as includes. The closest thing would be user controls.  A user control has it's own scope - basically, if a user control has a panel, that panel is only visible within the code behind of the user control page.  The "holding" page -- default.aspx in your case -- can't directly interact with it unless you provide some public methods for it (but it's generally not a good idea).

As far as what you've got, you are registering the control properly. Have you set break points and tried to debug through it to see if anything is executing inside your header.ascx page?

0
 
LVL 7

Author Comment

by:bdichiara
ID: 20403622
I tried this:

Sub Page_Load(ByVal Src As Object, ByVal E As EventArgs)
      Response.End()
End Sub

And the page still loads fine. I've even tried stuff like "asldfha;sjdlafh a2;43fj;cwlk" to throw an error, but nothing happens. It's like my header.ascx file doesn't see the header.ascx.vb file.

I don't know how to set breakpoints. I only have Visual Web Developer and Dreamweaver to work in. I'm not using any localhost/server on my local computer. I have a development site I upload to.
0
 
LVL 7

Author Comment

by:bdichiara
ID: 20403682
I don't know what Master pages are. Here's how my site is setup:

I have a default.aspx

I have a URL Rewriting script which rewrites my URLS to point to content.aspx.

Content.aspx and Default.aspx have 3 "includes" (or controls), the header, footer, and a file which generates the navigation menu. (because I couldn't do this in the header). The reason these are 2 separate files is so we have complete control over the homepage via static content. All other pages load the content wrapped with header/footer.

Content.aspx will retrieve from the database whatever content it needs based on the URL.

I find myself constantly using Session variables for things not meant for session variables, such as the creation of the Nav menus. Since I can't put the code into an ASP:Literal on both Default.aspx and Content.aspx from the separate "control" i'm using, I must use the session variable.
0
 
LVL 19

Expert Comment

by:DreamMaster
ID: 20403709
Try changing:

Sub Page_Load(ByVal Src as Object, ByVal E As EventArgs)
to
Sub Page_Load(ByVal Src as Object, ByVal E as EventArgs) Handles Me.Load

You can debug without having a localhost, as Visual Studio will create its own debug server. Adding a breakingpoint is as easy as clicking inside the gray bar in front of your codebehind file.

Regards,
Max.
0
 
LVL 8

Expert Comment

by:Chumad
ID: 20403791
Masterpages do basically what you are trying to do with headers/footers and menus.

They are pretty simple - to try it out, create a new masterpage and toss some random content into it.

Then create a new ASPX page and specify that it should have a masterpage. Basically when you do this, the ASPX page contains just the content, and that content is inserted into the masterpage's "content place holder" whereever you've specified it.
0
 
LVL 7

Author Comment

by:bdichiara
ID: 20404126
I tried your suggestion, but nothing different happened.  As for the breakpoint, I clicked inside the bar (using Visual Web Developer), and a big red dot appeared. I copied the file to the server and went to the page, but nothing different happened.
0
 
LVL 8

Expert Comment

by:Chumad
ID: 20404313
Once you have the big red dot, hit F5 to start the debugger. You won't need to copy anything to the server for it to work... hitting F5 should launch a new browser window.
0
 
LVL 19

Expert Comment

by:DreamMaster
ID: 20405136
What Chumad said...you don't need to copy anything to any server. Visual Studio has its own build in Webserver to debug even if you don't have IIS.

Regards,
Max.
0
 
LVL 7

Author Comment

by:bdichiara
ID: 20405143
Here's what I get:

Error      1      It is an error to use a section registered as allowDefinition='MachineToApplication' beyond application level.  This error can be caused by a virtual directory not being configured as an application in IIS.      Y:\dotIR\Web.config      46      
Warning      2      Namespace or type specified in the Imports 'Microsoft.Data.Odbc' doesn't contain any public member or cannot be found. Make sure the namespace or the type is defined and contains at least one public member. Make sure the imported element name doesn't use any aliases.      Y:\header.ascx.vb      13      9      Y:\
Error      3      Name 'panLoginForm' is not declared.      Y:\header.ascx.vb      60      5      Y:\
Error      4      Name 'panLoginLink' is not declared.      Y:\header.ascx.vb      61      5      Y:\
Error      5      Name 'panLoginForm' is not declared.      Y:\header.ascx.vb      63      5      Y:\
Error      6      Name 'panLoginLink' is not declared.      Y:\header.ascx.vb      64      5      Y:\
Error      7      Name 'panLoginForm' is not declared.      Y:\header.ascx.vb      67      4      Y:\
Error      8      Name 'panLoginLink' is not declared.      Y:\header.ascx.vb      68      4      Y:\



My explanation:
Error 1: I have no idea...

Error 2: I use ODBC to connect to MySQL database. You know of a better way, please tell me!!! Far as I know all ODBC functions are working fine. not sure about this error.

Error 3 - 8: panLoginForm and panLoginLink are my 2 panels inside the header.ascx file which i toggle at one point in the script. I've never had to declare these values before...

Thanks again for helping me with this.
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 7

Author Comment

by:bdichiara
ID: 20405224
Oh, the first error is some folder (dotIR) that I'm not using on the live site. It was supposed to be some dynamic text font tool that converts text to images, but I'm no longer using it.
0
 
LVL 19

Expert Comment

by:DreamMaster
ID: 20405282
Those panLoginForm and panLoginLink are in the header.ascx file? Since you are obviously referring to them in the header.ascx.vb file.

The fact that they are not declared suggests that you may have declared them elsewhere, possibly in default.aspx? You won't be able to reference them from header.ascx.vb in that case.

Could you show the code for header.ascx and the codebehind (header.ascx.vb) so I can have a look whether they are defined there...

To reference the panLoginForm and panLoginLink from header.ascx.vb they need to be physically declared in header.ascx so if you have something like this in header.ascx you have done it correctly, if the code below is in another file, you haven't ;):

<asp:Panel id="panLoginForm" runat="server" visible="false">
 ...
</asp:Panel>

0
 
LVL 7

Author Comment

by:bdichiara
ID: 20405359
The only place I have that referenced is inside the header.ascx file:

<asp:Panel ID="panLoginForm" runat="server">

... some stuff  ...

</asp:Panel>
<asp:Panel ID="panLoginLink" runat="server" Visible="false">

... some other stuff ...

</asp:Panel>

ALL Inside header.ascx

Here is the part on the default page that references the header file.

<%@ Register TagPrefix="atcu" TagName="header" Src="~/header.ascx" %>
...

<body>
<atcu:header id="header1" runat="server"></atcu:header>

...


There's a log of code in the header.ascx file; I hope the code above helps. Here's the header.ascx.vb file:

Imports System
Imports System.Net
Imports System.Data
Imports System.Data.Odbc
Imports System.Configuration
Imports System.Text.RegularExpressions
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls

Public Class _Header
      Inherits System.Web.UI.Page

      Public Shared Function UrlIsValid(ByVal smtpHost As String) As Boolean
            Dim httpReq As HttpWebRequest = CType(WebRequest.Create(smtpHost), HttpWebRequest)
            httpReq.AllowAutoRedirect = False
            Dim httpRes As HttpWebResponse
            Dim exists As Boolean = False
            Try
                  httpRes = CType(httpReq.GetResponse(), HttpWebResponse)
                  If httpRes.StatusCode = HttpStatusCode.OK Then
                        exists = True
                  End If
            Catch ex As Exception

            End Try

'            Return exists
            Return False
      End Function

      Sub Page_Load(ByVal Src As Object, ByVal E As EventArgs) Handles Me.Load
            'page load stuff
            'as you can see by JUNK, i'm trying to throw this error, but it doesn't do anything.
            If UrlIsValid("https://www.telcoonlineJUNK.org/cgi-bin/mcw000.cgi?MCWSTART") Then
                  'I obtain the following session variables inside my 3rd control file (header, footer, other)
                  If Len(Session("mcwexstring")) <= 0 And Len(Session("mcwassstring")) <= 0 And Len(Session("mcwsystring")) <= 0 Then
                        panLoginForm.Visible = True
                        panLoginLink.Visible = False
                  Else
                        panLoginForm.Visible = False
                        panLoginLink.Visible = True
                  End If
            Else
                  panLoginForm.Visible = False
                  panLoginLink.Visible = True
            End If
      End Sub

End Class
0
 
LVL 7

Author Comment

by:bdichiara
ID: 20407254
I decided to try a different method. On my site sometimes I need to include a separate page which has a form on it. I found some code that does this and I thought I'd give it a try:

'load header
Dim Hbuilder As StringBuilder = New StringBuilder
Hbuilder.Append("header.ascx")
Dim HControl As Control = LoadControl(Hbuilder.ToString())
panHeader.Controls.Add(HControl)
            
'load footer
Dim Fbuilder As StringBuilder = New StringBuilder
Fbuilder.Append("footer.ascx")
Dim FControl As Control = LoadControl(Fbuilder.ToString())
panFooter.Controls.Add(FControl)

I thought this would work, my files are setup exactly the same as the other files. But instead I get this error:

Compilation Error: Make sure that the class defined in this code file matches the 'inherits' attribute, and that it extends the correct base class (e.g. Page or UserControl).

Source File: header.ascx.vb    Line: 1

This is the top line of the header file:
<%@ Control Language="VB" EnableViewState="False" CodeFile="~/header.ascx.vb" Inherits="_Header" %>

and this is the first few lines of the corresponding .vb file:
Imports System
Imports System.Net
Imports System.Data
Imports System.Data.Odbc
Imports System.Configuration
Imports System.Text.RegularExpressions
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports Microsoft.Data.Odbc

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


This has been very frustrating. You'd think something like including a file would be simple...
0
 
LVL 19

Expert Comment

by:DreamMaster
ID: 20407308
Well...it usually is...so my thought is you started off wrong.. ;)

I usually start off with a master page. That contains my entire outer shell, my template if you will. All the other pages are nothing more than mere content..or controls to add to the page.

Regards,
Max.
0
 
LVL 7

Author Comment

by:bdichiara
ID: 20407471
Ok, well, I did get it working, however am do have one problem. An ASP:Panel creates a "div" tag. Is there a way to override that. I tried using an ASP:Literal, but it says it doesn't allow child controls. Everything else is working great, now it's just a formatting problem.
0
 
LVL 7

Author Comment

by:bdichiara
ID: 20407525
Well, for the time being, I just ended </div> at the top of my footer and started <div> at the bottom of my header. That seems to work.

Thanks.
0
 
LVL 19

Assisted Solution

by:DreamMaster
DreamMaster earned 250 total points
ID: 20407594
You could also work with placeholder controls. The only way to override the default behaviour of a panel is to do as it says..override it.. ;) Which actually means you take what you like of the default behaviour and then add your own code to change the way it behaves.. :)

Regards,
Max.
0
 
LVL 7

Author Comment

by:bdichiara
ID: 20415278
Hey, rock 'n roll on the placeholder. That worked great! Thanks DreamMaster! I may look into Master Pages for my next project, but no time for this one. Thanks Chumad for the additional assistance and the time to help.
0
 
LVL 19

Expert Comment

by:DreamMaster
ID: 20415318
Glad to have been helpful :)

Regards,
Max.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now