I have a webpage that was built using the All In One style (the VB in the page with the HTML), and I have been asked to separate them.
This is how Default.aspx starts out:
<%# Import Namespace="System.Net" %>
<%# Import Namespace="System.IO" %>
<%# Import Namespace="System.Configuration" %>
<%# Import Namespace="System.Data" %>
<%# Import Namespace="System.Data.OleDb" %>
<%# Import Namespace="System.Data.SqlClient" %>
<%# Import Namespace="System.Globalization" %>
<%# Import Namespace="System.Security" %>
<html>
<head>
<title>View an Activity (AFE)</title>
<script language="VB" runat="server">
Private m_url1, m_url2, m_downloadText, txtSupplementPage, txtExcerpt, SearchLocation, SearchLocation2, SearchLocation3, SearchLocationOrig As String
Private myTotal1, myTotal2 As Double
Private m_credentials As CredentialCache
' Start the initial page
Protected Sub Page_Load(sender As Object, e As EventArgs)
m_credentials = New CredentialCache()
If Not IsPostBack Then
It goes on. This is just to show you what I'm doing.
I created a new, similar file called that Default2.aspx that includes the Page directive, specifies the CodeBehind file, and the namespace (Inherits). Then I took the VB code out:
<%# Page Language="vb" AutoEventWireup="true" CodeBehind="Default.aspx.vb" Inherits="app2._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>View an Activity (AFE)</title>
Since Default.aspx did not use a Code Behind file, I named my new Code Behind file Default.aspx.vb.
I created a new _Default with Namespace app2 within this Code Behind file, and I copied all of the VB from Default.aspx into it:
Namespace app2
Public Class _Default
Inherits System.Web.UI.Page
Inherits System.Net
Inherits System.IO
Inherits System.Configuration
Inherits System.Data
Inherits System.Data.OleDb
Inherits System.Data.SqlClient
Inherits System.Globalization
Inherits System.Security
Private m_url1, m_url2, m_downloadText, txtSupplementPage, txtExcerpt, SearchLocation, SearchLocation2, SearchLocation3, SearchLocationOrig As String
Private myTotal1, myTotal2 As Double
Private m_credentials As CredentialCache
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
m_credentials = New CredentialCache()
If Not IsPostBack Then
I am trying to give enough code that everyone can see the starting points and where I am now.
I got the two new files uploaded to the web server, and then browsed to them to see how they would look.
However, now when I try to display Default2.aspx in the browser, I get this Parser Error:
Parser Error Message: Could not load type 'app2._Default'.
Source Error:
Line 1: <%# Page Language="vb" AutoEventWireup="true" CodeBehind="Default.aspx.vb" Inherits="app2._Default" %>
Line 2:
Line 3:
What have I messed up?
For a web site: Change CodeBehind to CodeFile.
<%# Page Language="vb" AutoEventWireup="true" CodeFile="Default.aspx.vb" Inherits="app2._Default" %>
For a web application: Add the name of the web project to the Inherits attribute.
<%# Page Language="vb" AutoEventWireup="true" CodeBehind="Default.aspx.vb" Inherits="nameOfYourWebProject.app2._Default" %>
Also, the _Default class you provided, you can only have one Inherits, I think you meant to add them as Imports on the top of the page.
Related
I have a WebForms page like so:
<%# Page Language="vb" AutoEventWireup="false" CodeBehind="ReportsTo.aspx.vb" Inherits="MyApp.ReportsTo" %>
<%# Import Namespace="System.Collections.Generic" %>
<%# Import Namespace="System.Data.SqlClient" %>
<%# Import Namespace="MyApp" %>
<%# Import Namespace="MyAppDBTools" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<%
Dim nextHigher As Integer? = UserLevelID
' do more stuff here
%>
</form>
</body>
</html>
And here is the code behind:
Imports MyAppDBTools
Public Class ReportsTo
Inherits System.Web.UI.Page
''' <summary>
''' The ID of the user whose supervisor is being set.
''' </summary>
''' <returns></returns>
Protected ReadOnly Property UserID As Integer
Get
Return CInt(Request("UserID"))
End Get
End Property
''' <summary>
''' The level of the user whose supervisor is being set.
''' </summary>
''' <returns></returns>
Protected ReadOnly Property UserLevelID As Integer
Get
Return DBFunctions.GetUserLevelID(UserID, SessionHelper.ConnectionString)
End Get
End Property
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
End Class
However I get compile errors when I reference the UserID and UserLevelID properties from the page:
BC30451 'UserLevelID' is not declared. It may be inaccessible due to its protection level.
Why is this? Shouldn't I be able to access properties defined in the code-behind from the page? If not, where else can I put them? A singleton class?
I have an aspx file containing some VB code:
<%# Page Language="VB" ContentType="text/html" ResponseEncoding="iso-8859-1" %>
<%# Import Namespace="System.Data" %>
<%# Import Namespace="System.Data.SqlClient" %>
<%# Import Namespace="System.IO" %>
<%# Import Namespace="XXX.Email" %>
<%# Import Namespace="XXX.Functions" %>
<%# Import Namespace="XXX.Rtf" %>
<%# Import Namespace="XXXDBTools" %>
<%# Import Namespace="XXXDBTools.DBFunctions" %>
<%# Import Namespace="XXXStrTools.StrFunctions" %>
<script runat="server">
Dim strToResult
Dim errorMessage
Dim errorMessageFor
Dim referralEmailStr
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
...
End Sub
</script>
<html>
<head>
<title><%=Request.Cookies("State").Value%> Request a Placement</title>
...
</head>
...
</html>
Now I see compile errors on the Request.Cookies("State").Value line, for code that doesn't even exist on this page!
If I delete the line where I'm checking for the state cookie, I still get the same errors, but at the end of the Sub Page_Init I declared above.
The variables mentioned in the errors (such as myTemp and lfyCompanyInfoDSN) do not even exist on this page, so I'm perplexed as to how I could be getting these errors...
Turns out at the very bottom of the <html> tag I had some lines such as this in a script:
set drIntakeWorker = Nothing
Which is not valid because the set keyword is not allowed in VB.NET. Somehow this error was percolating up and screwing up the entire page...
I have .aspx and .vb files. The name of it are: SA_Setup.aspx and SA_Setup.vb.
The 1st 10 lines of the code in SA_Setup.aspx file is:
<%# Page Title="" Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false"
CodeFile="SA_Setup.aspx.vb" Inherits="SA_Setup" %>
<%# Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
<%# MasterType VirtualPath="~/MasterPage.master" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
<link id="Link2" rel="Stylesheet" href="~/Common/OrgSelfAssessment.css" type="text/css"
media="screen" runat="server" />
</asp:Content>
And the 1st 10 lines of code in SA_Setup.aspx.vb file is:
Imports System.Data
Imports GlobalModule
Partial Class SA_Setup
Inherits System.Web.UI.Page
Dim dalGM As GlobalModule
Dim dalSelfAssessment As New SelfAssessmentSetUp
Dim dalSurveyAdmin As New SurveyAdmin
Dim dalClientAdmin As New ClientAdmin
Protected Overrides Sub InitializeCulture()
Try
Dim strCul As String : strCul = Session("Culture")
System.Threading.Thread.CurrentThread.CurrentUICulture = New System.Globalization.CultureInfo(strCul)
System.Threading.Thread.CurrentThread.CurrentCulture = New System.Globalization.CultureInfo(strCul)
Catch ex As Exception
Response.Redirect("~/Error/ErrorPage.aspx?type=SessionTimedOut&ui=" & Request.QueryString("ui"), True) : Exit Sub
End Try
End Sub
Now, I have changed the files name to SA_Setup_MPHLB.aspx and SA_Setup_MPHLB.aspx.vb. I am wondering what changes do I need to do in the codes above after changing the files name. As per my understanding, I have made the following changes:
In .aspx file (SA_Setup_MPHLB.aspx):
<%# Page Title="" Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false"
CodeFile="SA_Setup_MPHLB.aspx.vb" Inherits="SA_Setup_MPHLB" %>
In .aspx.vb file (SA_Setup_MPHLB.aspx.vb):
Partial Class SA_Setup_MPHLB
Just checking if the changes I have did is right or is there any place I have to do the changes ?
If you are able to successfully rebuild the project after name changes, you may not need to do anything else. If you are getting any error, it will surely suggest you what to do next.
This is a simple example of what is happening here.
Default.aspx
<%# Page Language="vb" AutoEventWireup="false" CodeBehind="default.aspx.vb" Inherits="base._default1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<% Response.Write(sName) %>
</div>
</form>
</body>
</html>
and the code behind default.aspx.vb
Public Class _default1
Inherits System.Web.UI.Page
Public sName As String = "Jimmy"
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
End Class
the error is
BC30451 'sName' is not declared. It may be inaccessible due to its protection level. base C:\Users\Jimmy\Documents\Visual Studio 2015\Projects\base\base\teste\default.aspx 12
Where is the problem ?
The biggest issue is your class _default1 is not marked as partial class.
It should rather be
Public partial Class _default1
Inherits System.Web.UI.Page
One more point is that your page directive has the property AutoEventWireup="false" and with that the way you have your page events mapped right now will not work. So set it to true rather AutoEventWireup="true"
Your #Page directive is total weird as can be seen below.
<%# Page AutoEventWireup="false" CodeBehind="default.aspx.vb" Inherits="base._default1" %>
To summarize; below are the issues you should take care off
mark the class partial
set AutoEventWireup="true"
change CodeBehind property to CodeBehind="default1.aspx.vb"
Change your Inherits property to Inherits="your_namespace._default1"
I have masterpage.master.vb where I have properties, such as;
Private _SQLerror As String
Public Property SQLerror() As String
Get
Return _SQLerror
End Get
Set(ByVal value As String)
_SQLerror = String.Empty
End Set
End Property
Then I have an aspx page in which I need to use this property in, such as;
If **[masterpage property sqlerror]** = Nothing Then
InternalSQLErrLabel.Text = ("No Errors Reported")
End If
Can anyone give me an idea how to go about this? I've tried searching but most articles talk in the context of web controls...
Thanks.
Here you go:
How to: Reference ASP.NET Master Page Content
From the article, it looks like
If Master.SQLerror = Nothing Then
InternalSQLErrLabel.Text = ("No Errors Reported")
End If
should work for you.
Just be sure to add the MasterType directive as described or you might get a type conversion error. (Or you could use a variable of your master page type instead of Master, as daRoBBie suggests in his answer.)
I have created a test Web site just to test this out, and it works. Here is the full source of the site:
Site1.Master:
<%# Master Language="VB" AutoEventWireup="false" CodeBehind="Site1.master.vb" Inherits="WebApplication1.Site1" %>
<!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>
This is the Master Page content.
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>
Site1.Master.vb:
Public Partial Class Site1
Inherits System.Web.UI.MasterPage
Private _SQLerror As String
Public Property SQLerror() As String
Get
Return _SQLerror
End Get
Set(ByVal value As String)
_SQLerror = String.Empty
End Set
End Property
End Class
WebForm1.aspx:
<%# Page Title="" Language="vb" AutoEventWireup="false" MasterPageFile="~/Site1.Master"
CodeBehind="WebForm1.aspx.vb" Inherits="WebApplication1.WebForm1" %>
<%# MasterType VirtualPath="~/Site1.Master" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
This is the Content Page content.
<asp:Label ID="InternalSQLErrLabel" runat="server" Text="Label"></asp:Label>
</asp:Content>
WebForm1.aspx.vb:
Public Partial Class WebForm1
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Master.SQLerror = Nothing Then
InternalSQLErrLabel.Text = ("No Errors Reported")
End If
End Sub
End Class
you can cast the masterpage to the correct type:
MyMasterPageType m = (MyMasterPageType)Master;
Then you can access your properties:
m.SqlError
If you have multiple master pages, let all your masterpages inherit from an interface, and cast the masterpage to that interface.
You can use <%# MasterType %> also for this.
If you have followed the steps in Andy West's answer and have one or many compile errors reading: Foo is not a member of 'System.Web.UI.MasterPage', make sure that they are the only compile error in your list. If there are any other compile errors that need to be fixed, they should be fixed before you continue troubleshooting your MasterPage.
These other compile errors may be preventing the compiler from parsing your MasterPage and detecting the extra properties. Once they are resolved, do a full recompile.