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"
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 a project and the strangest thing is happening. I have been trying to figure it out for multiple hours. The issue is if I create a new web form, the code behind will see all the controls, but from the .aspx page I can use any public functions from the code behind. For example if I create the following function:
Public Function Hey()
Return True
End Function
Then I go to the .aspx source and put the following:
<%# hey%>
I get the following error: Error 2 'hey' is not declared. It may be inaccessible due to its protection level.
The following is WebForm5.aspx:
<%# Page Title="Web Form" Language="vb" AutoEventWireup="false" MasterPageFile="~/Site.Master" CodeBehind="WebForm5.aspx.vb" Inherits="InventoryManagement.WebForm5" Async="true" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<%# hey%>
</div>
</form>
</body>
</html>
WebForm5.aspx.vb:
Public Class WebForm5
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Public Function Hey()
Return True
End Function
End Class
I have "older" pages that I can call the exact same function from code behind. Any help would be greatly appreciated as this issue has been a problem for the last 3+ hours. Thanks in advance!!
The solution to this issue was to clean and rebuild project. This solved the issue right away!
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.
I am trying to design a form using VB.Net for WebForms. I have been following the following link My label's text isn't changing on page_load asp.net. But the solution there is not working for me. I already have Handles Me.Load defined for my Page_Load event handler, however the result is a "blank" page, with the HTML output below.
form.aspx:
<%# Page Language="vb" AutoEventWireup="false" CodeBehind="form.aspx.vb" Inherits="MyApp.form" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Test</title>
</head>
<body>
<asp:Label id="vcode" runat="server" />
</body>
</html>
form.aspx.vb:
Imports System.Web
Public Class form
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
vcode.Text = "Why not?"
End Sub
End Class
HTML Output:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>
Test
</title></head>
<body>
<span id="vcode"></span>
</body>
</html>
I suspect it has to do with the Inherits attribute, but if I take out Page_load from that class definition, I get an error:
Statement is not valid in a namespace
I also tried setting the AutoEventWireup attribute on the page to true, but no change.. still blank label.
What am I missing here?
Try adding a namespace like so:
Namespace MyApp
Public Class form Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
vcode.Text = "Why not?"
End Sub
End Class
End Namespace
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.