Cannot appear outside of a method body

That is the error I’m getting. Here is the code with the problem line in red.


<script language="VB" runat="server">
	Dim HeaderImage as String
	Dim RightPanelContent as String
	
		[color=red]If Not Session("PersonID") = "" Then	'user is logged in[/color]
			'determine what department the user is in
			Select Case lcase(Session("DepartmentID"))
				'have to add more Cases as the Intranet TicketSystem is improved...
				Case "sales"
					HeaderImage = "header-sales.gif"
				Case Else
					HeaderImage = "header-general.gif"
			End Select
			'show logout button
			RightPanelContent = "<p><br /><a href=""/logout.aspx""><img src=""/modules/header/header-logout.gif"" alt=""Logout"" width=""80"" height=""20"" border=""0"" /></a>&nbsp;&nbsp;</p>"
		Else	'user is not logged in, show general stuff
			HeaderImage = "header-general.gif"
			RightPanelContent = "&nbsp;"
		End If

	'write it to the page!

	Response.Write( vbTab & vbTab & _
		"<table width=""100%"" cellpadding=""0"" cellspacing=""0"">" & vbCrLf & vbTab & vbTab & vbTab & _
  			"<tr>" & vbCrLf & vbTab & vbTab & vbTab & vbTab & _
				"<td width=""188"" background=""/modules/header/header-bg.gif""><a href=""/default.aspx""><img src=""/modules/header/header-corner.gif"" alt=""Ticket System Home"" width=""188"" height=""100"" border=""0"" /></a></td>" & vbCrLf & vbTab & vbTab & vbTab & vbTab & _
				"<td align=""center"" background=""/modules/header/header-bg.gif""><img src=""modules/header/" & HeaderImage & """ width=""233"" height=""100"" /></td>" & vbCrLf & vbTab & vbTab & vbTab & vbTab & _
				"<td width=""188"" background=""modules/header/header-bg.gif"">" & RightPanelContent & "</td>" & vbCrLf & vbTab & vbTab & vbTab & _
  			"</tr>" & vbCrLf & vbTab & vbTab & _
		"</table>"
</script>

This is inside a .ascx user control file called by every page.

I’ve tried placing the entire IF…Then statement inside of a Sub and calling the sub like so:


<script language="VB" runat="server">
	Dim HeaderImage as String
	Dim RightPanelContent as String
	Sub DetermineContent()
		If Not Session("PersonID") = "" Then	'user is logged in
			'determine what department the user is in
			Select Case lcase(Session("DepartmentID"))
				'have to add more Cases as the Intranet TicketSystem is improved...
				Case "sales"
					HeaderImage = "header-sales.gif"
				Case Else
					HeaderImage = "header-general.gif"
			End Select
			'show logout button
			RightPanelContent = "<p><br /><a href=""/logout.aspx""><img src=""/modules/header/header-logout.gif"" alt=""Logout"" width=""80"" height=""20"" border=""0"" /></a>&nbsp;&nbsp;</p>"
		Else	'user is not logged in, show general stuff
			HeaderImage = "header-general.gif"
			RightPanelContent = "&nbsp;"
		End If
	End Sub

	'write it to the page!
	
	[color=red]Call DetermineContent()[/color]
	
	Response.Write( vbTab & vbTab & _
		"<table width=""100%"" cellpadding=""0"" cellspacing=""0"">" & vbCrLf & vbTab & vbTab & vbTab & _
  			"<tr>" & vbCrLf & vbTab & vbTab & vbTab & vbTab & _
				"<td width=""188"" background=""/modules/header/header-bg.gif""><a href=""/default.aspx""><img src=""/modules/header/header-corner.gif"" alt=""Ticket System Home"" width=""188"" height=""100"" border=""0"" /></a></td>" & vbCrLf & vbTab & vbTab & vbTab & vbTab & _
				"<td align=""center"" background=""/modules/header/header-bg.gif""><img src=""modules/header/" & HeaderImage & """ width=""233"" height=""100"" /></td>" & vbCrLf & vbTab & vbTab & vbTab & vbTab & _
				"<td width=""188"" background=""modules/header/header-bg.gif"">" & RightPanelContent & "</td>" & vbCrLf & vbTab & vbTab & vbTab & _
  			"</tr>" & vbCrLf & vbTab & vbTab & _
		"</table>"
</script>

Then I got a bad syntax error on Call DetermineContent(). So I tried every variation of parenthases/no parenthases, etc that I could think of, but it was always a syntax error.

Can anyone spot my ignorance? Thanks!
Brandon

the keyword “Call” is no longer needed/supported in .NET

see, I thought I remembered reading that, so I tried it and it gave me a “Declaration expected” error on that line.

Then, I went looking for examples online and saw that people were using the Call (even Microsoft)… only to go back now and realize that they were written while Beta 1 was out…

So, then, what declaration is it expecting??

You need to put the code is a Sub-routine or function:

[vbs]
<script language=“VB” runat=“server”>

Dim HeaderImage as String
Dim RightPanelContent as String

Sub DetermineContent()
	If Not Session("PersonID") = "" Then	'user is logged in
		'determine what department the user is in
		Select Case lcase(Session("DepartmentID"))
			'have to add more Cases as the Intranet TicketSystem is improved...
			Case "sales"
				HeaderImage = "header-sales.gif"
			Case Else
				HeaderImage = "header-general.gif"
		End Select
		'show logout button
		RightPanelContent = "&lt;p&gt;&lt;br /&gt;&lt;a href=""/logout.aspx""&gt;&lt;img src=""/modules/header/header-logout.gif"" alt=""Logout"" width=""80"" height=""20"" border=""0"" /&gt;&lt;/a&gt;  &lt;/p&gt;"
	Else	'user is not logged in, show general stuff
		HeaderImage = "header-general.gif"
		RightPanelContent = " "
	End If
End Sub

'write it to the page!

Sub Page_Load(obj As Object, e As EventArgs)
DetermineContent()

Response.Write( vbTab & vbTab & _
	"&lt;table width=""100%"" cellpadding=""0"" cellspacing=""0""&gt;" & vbCrLf & vbTab & vbTab & vbTab & _
		"&lt;tr&gt;" & vbCrLf & vbTab & vbTab & vbTab & vbTab & _
			"&lt;td width=""188"" background=""/modules/header/header-bg.gif""&gt;&lt;a href=""/default.aspx""&gt;&lt;img src=""/modules/header/header-corner.gif"" alt=""Ticket System Home"" width=""188"" height=""100"" border=""0"" /&gt;&lt;/a&gt;&lt;/td&gt;" & vbCrLf & vbTab & vbTab & vbTab & vbTab & _
			"&lt;td align=""center"" background=""/modules/header/header-bg.gif""&gt;&lt;img src=""modules/header/" & HeaderImage & """ width=""233"" height=""100"" /&gt;&lt;/td&gt;" & vbCrLf & vbTab & vbTab & vbTab & vbTab & _
			"&lt;td width=""188"" background=""modules/header/header-bg.gif""&gt;" & RightPanelContent & "&lt;/td&gt;" & vbCrLf & vbTab & vbTab & vbTab & _
		"&lt;/tr&gt;" & vbCrLf & vbTab & vbTab & _
	"&lt;/table&gt;"

End Sub
</script>
[/vbs]

:slight_smile:

yah what dhtmlgod said. also i would reccommend using this:

If Not Session.Item("PersonID") is nothing

instead of this:

If Not Session("PersonID") = ""

…and actually my preferred syntax is:

If Not Session!PersonID is nothing

ok, soon, I will start to grasp the reasons for all this, but now my code looks like this:

[VBS]



<script language="VB" runat="server">
	Dim HeaderImage as String
	Dim RightPanelContent as String
	
	Sub DetermineContent()
		If Not Session!PersonID is nothing Then	'user is logged in
			'determine what department the user is in
			Select Case lcase(Session("DepartmentID"))
				'have to add more Cases as the Intranet TicketSystem is improved...
				Case "sales"
					HeaderImage = "header-sales.gif"
				Case Else
					HeaderImage = "header-general.gif"
			End Select
			'show logout button
			RightPanelContent = "<p><br /><a href=""/logout.aspx""><img src=""/modules/header/header-logout.gif"" alt=""Logout"" width=""80"" height=""20"" border=""0"" /></a>&nbsp;&nbsp;</p>"
		Else	'user is not logged in, show general stuff
			HeaderImage = "header-general.gif"
			RightPanelContent = "&nbsp;"
		End If
	End Sub

	'write it to the page!
	
	Sub Page_Load(obj As Object, e As EventArgs)
		DetermineContent()
	
		Response.Write( vbTab & vbTab & _
			"<table width=""100%"" cellpadding=""0"" cellspacing=""0"">" & vbCrLf & vbTab & vbTab & vbTab & _
  				"<tr>" & vbCrLf & vbTab & vbTab & vbTab & vbTab & _
					"<td width=""188"" background=""/modules/header/header-bg.gif""><a href=""/default.aspx""><img src=""/modules/header/header-corner.gif"" alt=""Ticket System Home"" width=""188"" height=""100"" border=""0"" /></a></td>" & vbCrLf & vbTab & vbTab & vbTab & vbTab & _
					"<td align=""center"" background=""/modules/header/header-bg.gif""><img src=""/modules/header/" & HeaderImage & """ width=""233"" height=""100"" /></td>" & vbCrLf & vbTab & vbTab & vbTab & vbTab & _
					"<td width=""188"" align=""right"" valign=""top"" background=""/modules/header/header-bg.gif"">" & RightPanelContent & "</td>" & vbCrLf & vbTab & vbTab & vbTab & _
  				"</tr>" & vbCrLf & vbTab & vbTab & _
			"</table>")
	End Sub
</script>


[/VBS]

…and I get a “header.ascx does not have a property named ‘table’” error.

In some tuts that I’ve looked at the people had straight HTML in their ascx file with a CodeBehind handling properties and such… Is that my problem, or should this method work too?

I’d imagine that I really shouldn’t call Page_Load in an .ascx file, should I?

alright. I’ve gone down that road… Where the main page has a user control on it that links to my .ascx file. The .ascx file has only an HTML layout of the control and looks like this:


<%@ Control Language="VB" Inherits="AlveyHeader" Src="header.vb" Description="Alvey Intranet Ticket System Header" %>
	<script language="VB" runat="server">	
		CreateHeader()
	</script>
	
	<table width="100%" cellpadding="0" cellspacing="0">
  		<tr>
			<td width="188" background="/modules/header/header-bg.gif"><a href="/default.aspx"><img src="/modules/header/header-corner.gif" alt="Ticket System Home" width="188" height="100" border="0" /></a></td>
			<td align="center" background="/modules/header/header-bg.gif"><asp:image AlternateText="Intranet Ticket System" ID="MainImage" ImageUrl="header-general.gif" width="233" height="100" runat="server" /></td>
			<td width="188" align="right" valign="top" background="/modules/header/header-bg.gif"><p><br />
				<asp:imagebutton AlternateText="Logout" Height="20" Width="80" Visible="false" OnClick="Logout_Click" ID="Logout" ImageUrl="/modules/header/header-logout.gif" runat="server" />&nbsp;&nbsp;&nbsp;</p></td>
  		</tr>
	</table>

The Codebehind, referenced by the <%@ Control %> directive in the above code, looks like this:


Imports System
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls

Public Class AlveyHeader
		
	Public Sub CreateHeader()
		If Not Session("PersonID") = nothing Then	'user is logged in
			'determine what department the user is in
			Select Case lcase(Session("DepartmentID"))
				'have to add more Cases as the Intranet TicketSystem is improved...
				Case "sales"
					MainImage.ImageUrl = ImageLocation & "header-sales.gif"
				Case Else
					MainImage.ImageUrl = ImageLocation & "header-general.gif"
			End Select
			'show logout button
			LogoutButton.Visible = True
		Else
			'user is not logged in, show default stuff
		End If
	End Sub
	
	Public Sub Logout_Click(sender As Object, e As EventArgs)
		Response.Redirect("/logout.aspx")
	End Sub
	
End Class

This code once again shows the Session(“PersonID”) form rather than Crowdozer’s Session!PersonID because
I was trying to iron out the problem, but alas, they both error telling me that “Session” is not declared… :bawling: