Spurious error: Property inaccessible on ASPX page - asp.net

On a legacy ASP.NET project I'm trying to maintain, there is what seems to be a spurious design-time error, namely, that a property is inaccessible. It is declared Public. It works at runtime.
To troubleshoot, I added a new WebForm to the project to simplify things.
Public Class Logon2017
Inherits System.Web.UI.Page
Dim _Foo As Boolean
Public Property Foo As Boolean
Get
Return True
End Get
Set(value As Boolean)
_Foo = value
End Set
End Property
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
End Class
And on the ASPX page:
<%# Page Language="vb" AutoEventWireup="false" CodeBehind="Logon2017.aspx.vb" Inherits="BlahBlah.Logon2017" %>
<snip>
.
.
.
<script type="text/javascript">
var s = <%=Foo%>
</script>
In the editor in Visual Studio 2015, HTML view, "Foo" is underlined in red with the error that it is not declared.
What causes the ASPX page to be unable to find the Property declaration in the code-behind?
Is there some pointer in one of the project files that has gotten corrupted?

Related

VB .net asp label undefined

I want to set asp label to catch Cookie content,
I set
<p><asp:Label ID="lblUserID" runat="server" Text=""></asp:Label></p>
in USERINFO.aspx
then set
Public Class USERINFO
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
If (Request.Cookies("userInfo")("vID") IsNot Nothing) Then
lblUserID.Text = Request.Cookies("userInfo")("vID").ToString
End If
End If
End Sub
End Class
in USERINFO.aspx.vb
But got visual studio alert :lblUserID not defined
What should I do to connect the label correctly ?
I am not sure exactly, but you may still double-check if there's no typo in design and code-behind files.
Also, I noticed you're using ToString without parentheses, which is not correct. Please change it to -
lblUserID.Text = Request.Cookies("userInfo")("vID").ToString()
Thanks to #Andrew Morton, I solve the problem.
My issue is caused by that I didn't declare lblUserID in in USERINFO.aspx.designer.vb,
I also didn't wrote the Inherits tag in USERINFO.aspx like
<%# Page Language="vb" AutoEventWireup="false" CodeBehind="USERINFO.aspx.vb" **Inherits="MyProject.USERINFO"** %>
Correctly.

Can't use LINQ in a VB.NET ASPX file, even if I import System.Linq

I've run into this problem over and over again, and it's rather frustrating - if I have an ASPX file with VB code in it, I can't use LINQ in that VB code, even if I import System.Linq:
<% Page Language="vb" ContentType="text/html" %>
<%# Import Namespace="System.Collections.Generic" %>
<%# Import Namespace="System.Linq" %>
<script runat="server">
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim list As New List(Of String)
' populate list here
list = list.OrderBy(Function(q) q).ToList() ' error BC30456: 'ToList' is not a member of 'IEnumerable(Of Object)'
End Sub
</script>
Why is LINQ not working? But if I make my own extension methods and import the appropriate namespace, they work. I can call LINQ extension methods in a VB class or module, or even in the code-behind VB file of an ASPX page, but if I try to do it in the ASPX page itself, in a <% %> block or a <script runat="server">, I get this error. So I don't think it's the language version setting of the project, since it does work in VB files and for my own custom extension methods. Is there a separate language version setting that applies to only ASPX pages? Either in Visual Studio or IIS?
Oh, I discovered something else interesting: Enumerable.Empty is not available either:
Dim seq As IEnumerable(Of Object) = Enumerable.Empty(Of Object) ' error BC30451: 'Enumerable' is not declared. It may be inaccessible due to its protection level.
I know that System.Linq.Enumerable is where the LINQ methods are defined - why would this one class out of all of the .NET class library be unavailable in an ASPX file?
And if I fully qualify it:
Dim seq As IEnumerable(Of Object) = System.Linq.Enumerable.Empty(Of Object) ' error BC30456: 'Linq' is not a member of 'System'
So the entire System.Linq namespace does not exist?!

Code-behind properties inaccessible at design time but do work properly at runtime

I have to make some changes to a legacy ASP.NET website. It has a Property declaration like this:
Public ReadOnly Property Foo As String
Get
If Not Session("Foo") Is Nothing Then
Return Session("Foo").ToString()
Else
Return ""
End If
End Get
End Property
At design-time, looking at some javascript on the markup page, the property is inaccessible, although it works properly at run-time. That wrinkle makes this question a little different from others like it which have been asked here.
var f = <%= Foo %>;
In the Visual Studio editor, Foo is underlined in red; the error shown in the tooltip: Foo is undeclared. It may be inaccessible due to its protection level.
All of the properties on the page are inaccessible, not just this one.
What could be causing that spurious error at design-time?
EDIT: Here's some new info:
On an object declaration on the page, I am getting this error:
Private WithEvents MyGizmo as Gizmo
Class MyAssemblyName.Gizmo
MyAssemblyName -- Not Available
MyAssemblyName -- Available
I do not understand it. MyAssemblyName is mentioned twice, once as not available and once as available.
END OF NEW INFO
I've checked the page declaration:
MyAssemblyName matches the assembly name and the root namespace as shown on the Project Properties page.
The application type is Class Library.
I've checked the class declaration for protection level:
Public Class Widgets
End Class
and have made sure that the Web Form Designer Generated Code is present:
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
What else to check?

How do I access a property from the .vb file associated with the .aspx file?

So In my somepage.aspx.vb I have this
Public Class somepage
Inherits System.Web.UI.Page
Protected Property StringToPass As String
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
StringToPass = "hello"
End Sub
End Class
and then in the .aspx I try to do this
<%# Page Language="vb" AutoEventWireup="false" CodeBehind="somepage.aspx.vb" Inherits="mywebpage.somepage" %>
...
<%=StringToPass%>
...
but this doesn't work as expected, the .aspx file has no idea of its existence. How do I get that string from A to B?
EDIT: actually it does work, when I load the page, the string is passed. Its just that the debugger thinks StringToPass is an undeclared variable, whats up with that?

Dynamic nested master pages, shared properties

I have a base master page that specifies the main layout template of a website. It also handles some logic that changes tabs depending on the section, and also sets page meta information.
I'm dynamically loading nested master pages by looking at the querystring, loading up a record from the database, and setting the nested master page dynamically based on a value found in that record. I need to load dynamic nested master pages for layout and functional differences.
There is additional information in that record that I want to use in the base master page and in the dynamically loaded master page so I can avoid additional database calls.
Currently, I have set up a class that inherits MasterPage to act as the base class for the base master page. I have a shared (static) property that holds the object representing the database call that I want to share between the base master page and the nested, dynamically called master page.
It works, but it seems a little ugly. Are there any other better solutions?
You could always pass the record in the HttpContext.Items collection. Once it is in the Items collection it is available to every thing that can reach the HttpContext for the duration of the request.
Ok, I had to sleep on this one a bit, but I came up with a cleaner solution. I ended up using a base class for the page, instead of a base class for the master page. The base page sets the meta that I was going to set in the base master page.
Public Class PageBase
Inherits Page
Private _DocDetails As FolderDocument
Public Overridable ReadOnly Property DocDetails() As FolderDocument
Get
Return _DocDetails
End Get
End Property
Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack() Then
SetMeta()
End If
End Sub
Protected Sub SetMeta()
If DocDetails IsNot Nothing Then
Page.Title = DocDetails.MetaTitle
If DocDetails.MetaKeywords <> String.Empty Then
Dim metaKeywords As New HtmlMeta()
metaKeywords.Name = "Keywords"
metaKeywords.Content = DocDetails.MetaKeywords
Page.Header.Controls.Add(metaKeywords)
End If
If DocDetails.MetaDescription <> String.Empty Then
Dim metaDescription As New HtmlMeta()
metaDescription.Name = "Description"
metaDescription.Content = DocDetails.MetaDescription
Page.Header.Controls.Add(metaDescription)
End If
End If
End Sub
End Class
..And then the aspx page inherits this base page and dynamically sets the master page.
<%# Page Language="VB" Inherits="PageBase" %>
<script runat="server">
Private _DocDetails As FolderDocument
Public Overrides ReadOnly Property DocDetails() As FolderDocument
Get
Return _DocDetails
End Get
End Property
Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As System.EventArgs)
_DocDetails = FolderDocuments.GetFolderDocument()
If _DocDetails IsNot Nothing Then
If _DocDetails.MasterPage <> "" Then
Me.MasterPageFile = String.Format("~/templates/{0}.master", _DocDetails.MasterPage)
End If
End If
End Sub
</script>
...and in the dynamically called master page I can reference the page's base class by casting:
Dim parentPage As PageBase = DirectCast(Page, PageBase)
Response.write(parentPage.DocDetails.Title)

Resources