Error parsing attribute on #page directive - asp.net

I am trying to set a custom property "DisableBrowserCache" on the page directive like so
<%# Page Language="VB" AutoEventWireup="false" DisableBrowserCache="True"
CodeFile="Info-services.aspx.vb" Inherits="Manager_Info_services" %>
This is the inheritance chain
Partial Class Manager_Info_services
Inherits EltApp.ELTPage
'Code
End Class
Namespace EltApp
Public Class ELTPage
Inherits System.Web.UI.Page
Public Property DisableBrowserCache() As Boolean
Get
Return _DisableBrowserCache
End Get
Set(value As Boolean)
_DisableBrowserCache = value
End Set
End Property
End Class End Namespace
As you can see I inherit from a class that inherits from System.Web.UI.Page. This issue is that setting the property on the directive gives me the following error
System.Web.HttpParseException (0x80004005):
Error parsing attribute 'disablebrowsercache':
Type 'System.Web.UI.Page' does not have a public property named 'disablebrowsercache'.
---> system.Web.HttpParseException (0x80004005):
Error parsing attribute 'disablebrowsercache':
Type 'System.Web.UI.Page' does not have a public property named 'disablebrowsercache'.
---> System.Web.HttpException (0x80004005):
Error parsing attribute 'disablebrowsercache':
Type 'System.Web.UI.Page' does not have a public property named 'disablebrowsercache'.
at System.Web.UI.TemplateParser.ProcessError(String message)
at System.Web.UI.TemplateControlParser.ProcessUnknownMainDirectiveAttribute(String filter, String attribName, String value)
I have a feeling it's because im not directly inheriting from System.Web.UI.Page in the codebehind file.

That's not the way the Page directive works. You're asking it to understand your derived class even before the page is parsed.
You should put this in the Page_Init() event of Manager_Info_services.

Related

can I pass an aspx page class to a subroutine?

Here's what I'd like to do: Let's say I have a page named "foo.aspx". The class is called "foo". On the page is a checkbox named "bar". I want a subroutine to update that checkbox.
So what I want to write is something like:
In foo.aspx.vb:
partial class foo
... whatever ...
dim util as new MyUtility
util.update_checkbox(me)
In MyUtility
public sub update_checkbox(foo1 as foo)
foo1.bar.checked=true
end sub
But this doesn't work, as Visual Studio doesn't accept "foo" as a class name. Why not? Is there a magic namespace on it, or something else I have to do to identify the class besides say "foo"?
(And yes, I realize that in this trivial example, I could just pass in the checkbox, or move the one line of code into the aspx.vb, etc. My real problem involves setting a number of controls on the form, and I want to be able to do this in a class that has subtypes, so I can create an instance of the proper subtype, then just call one function and set all the controls differently depending on the subtype.)
Update
NDJ's answer works. For anyone else dropping by here, let me add that I was able to do something a little more flexible than his suggestion. I was able to create a property that returns the control itself, rather than some attribute of the control. Namely:
public interface ifoo
readonly property bar_property as literal
end interface
partial class foo
inherits system.web.page
implements ifoo
Public ReadOnly Property bar_property As Literal Implements ITest.bar_roperty
Get
' assuming the aspx page defines a control with id "bar"
Return bar
End Get
End Property
...
dim util=new MyUtility()
util.do_something(me)
...
end class
public class MyUtility
public sub do_something(foo as IFoo)
foo.bar_property.text="Hello world!"
foo.bar_property.visible=true
end sub
end class
This is a bit of a pain as you have to create an interface, and then create a property for each control that you want to be able to manipulate, but it does appear to work.
If there's a way to make the aspx class itself public, this is all unnecessary baggage in most cases. (It might be valuable if you have multiple pages that have controls that you want to manipulate in the same way.) But I can't figure out how to do that.
You can do this, but there are a few hoops to jump through.
Using your example...
If you create an interface with a Boolean property, then implement it in your page, then you can pass the interface about and changing the property will automatically change the checkbox. i.e.
interface:
Public Interface IFoo
Property Bar As Boolean
End Interface
implementation:
Partial Class _Foo
Inherits Page
Implements IFoo
Public Property Bar As Boolean Implements IFoo.Bar
Get
Return Me.CheckBox1.Checked
End Get
Set(value As Boolean)
Me.CheckBox1.Checked = value
End Set
End Property
Then some handler just needs to accept the interface:
Public Module SomeModule
Public Sub SetValues(foo As IFoo)
foo.Bar = True
End Sub
End Module
and the caller from the page passes itself:
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
SomeModule.SetValues(Me)
End Sub
You can expose the checkbox as a public property on the page. I don't write in VB.net, but it would look something like this in C#:
Can someone convert this to VB.Net?
public bool MyCheckBoxSetting
{
get { return mycheckbox.Checked; }
set { mycheckbox.Checked = value; }
}

How to wire up simple Web API controller in global.asax

I have a simple Web API (sitting inside webforms site) that generates a simple "Tip of the Day". The code is as follows:
AppCode\TipOfTheDayController.vb
Imports System.Net
Imports System.Web.Http
Public Class TipOfTheDayController
Inherits ApiController
Private Function GenerateTip() As TipOfTheDay
Dim tipCol As New List(Of Tip)
tipCol.Add(New Tip("Tip Text 01"))
tipCol.Add(New Tip("Tip Text 02"))
tipCol.Add(New Tip("Tip Text 03"))
Dim rnd As New Random
Dim i As Int16 = rnd.Next(0, tipCol.Count - 1)
Dim td As New TipOfTheDay
td.TipString = tipCol(i).TipString
td.TipNumber = i
Return td
End Function
Public Function GetTip() As TipOfTheDay
Return GenerateTip()
End Function
End Class
Public Class Tip
Public Property TipString As String
Public Sub New(ts As String)
Me.TipString = ts
End Sub
End Class
Public Class TipOfTheDay
Public Property TipString As String
Public Property TipNumber As String
End Class
I am trying to wire this up so that it can be called using
http://www.mysite.com/api/tips
Assuming the above URL is okay, I cannot figure this bit out in Global.asax. I've seen loads of examples online but all have an optional "ID" value which I don't need. Can anyone please show me what I need to do to retrieve my random "tip" from the API?
<%# Application Language="VB" %>
<%# Import Namespace="System.Web.Routing" %>
<%# Import Namespace="System.Web.Optimization" %>
<%# Import Namespace="System.Web.Http" %>
<script runat="server">
Sub RegisterRoutes(routes As RouteCollection)
routes.MapHttpRoute("TipOfTheDay", "api/tips")
End Sub
</script>
The routing is looking for an Index Action on your controller if the action was not specified in the route or by entering into the url. Rename your GetTip function to Index.
If that is not acceptable, you can add a route similar to the following in lieu of your current route.
routes.MapHttpRoute("TipOfTheDay", "api/tips", new { Controller = "TipOfTheDay" Action = "GetTip" });
I wouldn't recommend this route, however, since it will try to use GetTip as the default action every time one is not specified.
Here is a good resource for routing in a web forms application:
http://msdn.microsoft.com/en-us/library/dd329551.ASPX

ASP.Net User Control public function is not a member

I have a user control on a Web Site with this inside.
Namespace MenuTreePanel
Public Class MenuTreePanel
Inherits System.Web.UI.UserControl
Public root As New MenuNode(0, 0, "root", "")
Public WithEvents Spany1 As HtmlGenericControl = New HtmlGenericControl("UL")
Public WithEvents Spany2 As HtmlGenericControl = New HtmlGenericControl("UL")
Public WithEvents Spany3 As HtmlGenericControl = New HtmlGenericControl("UL")
Public Function getRoot() As MenuNode
Return root
End Function
End Class
End Namespace
When I go to access the getRoot function I get Error
'getRoot' is not a member of 'ASP.MenuTreePanel'.
The namespace is incorrectly labelled as ASP, and I was wondering where that might be coming from. In the object explorer, my control is listed under both the correct namespace and the ASP namespace.
Referenced on the page using
<%# Register TagPrefix="MenuTreePanel" Src="~/MenuTreePanel.ascx" TagName="MenuTree" %>
<MenuTreePanel:MenuTree ID="menuTreeSelect" runat="server"></MenuTreePanel:MenuTree>
Edit 2:
<%# Control Language="vb" CodeBehind="~/MenuTreePanel.ascx.vb"className="MenuTreePanel" %>
and the attempt to access it
Dim root As New MenuNode(0, 0, "root", "")
root = (menuTreeSelect).getRoot()
The problem is likely that you're attempting to access the property statically. My assumption is that you do not want to access it statically, since it's a control.
My suggestion is that you look at how you're using the MenuTreePanel object.
You should be accessing it like this:
menuTreeSelect.getRoot();
and not like this:
MenuTreePanel.getRoot();
Try:
Public Shared Function getRoot() As MenuNode
Return root
End Function
I wasn't linking the CodeFile and the ASCX correctly with a Web Site.
I had to change CodeBehind to CodeFile and add an inherits, and now everything is working correctly.
Thanks for your help.

Expose Body tag from Master Page to Content Pages in ASP.NET

I want to expose the <body> of my Master Page to my Content Pages. Therefore I do:
Master.aspx
<body id="MasterPageBodyTag" runat="server">
Master.aspx.vb
Public Property Messaging() As Messaging
Get
Return mMessaging
End Get
Set(ByVal value As Messaging)
mMessaging = value
End Set
End Property
Public Property BodyTag() As HtmlGenericControl
Get
Return MasterPageBodyTag
End Get
Set(ByVal value As HtmlGenericControl)
MasterPageBodyTag = value
End Set
End Property
ContentPage.aspx
<%# MasterType VirtualPath="~/my.master" %>
ContentPage.aspx.vb
Master.BodyTag.Attributes.Add("onload", "MyScript()")
However, not only I don't get the BodyTag in my content pages but I also receive an error that I cannot access the Messaging property (error: is not a member of Master), that before was working correctly. What can be the cause?
I am not expert at VB. But it seems that you need at first to cast me.Master reference to the Class type of the your custom master page before trying to access properties and method specific to you custom master page. I suppose that in this case automatic custom can't be done. So you can try to do the following:
Dim myCustomMaster As Site = CType(me.Master, Site)
where Site type is custom master class type.

Reference to a non shared member requires an object reference

I have added one class under namespace BusinessLogics.
I have inherited System.Web.UI.Page to class and showing error as 'end expected' in
System.Web.UI.Page
Namespace BusinessLogics
Public Class BllUploadImages Inherits System.Web.UI.Page
End Class
End Namespace
How can i remove my error.Can anybody help?
The Server property is an instance property of the Page class, so you need a Page instance in order to access it. There are a couple of different ways for you to solve this.
It looks like objDesign is of a type that inherits System.Web.UI.Page. Perhaps you can use that instance to invoke the MapPath method:
serverPath = objDesign.Server.MapPath(".") + "\"
One other approach is to fetch the current HttpContext object and use the Server property of that object:
serverPath = HttpContext.Current.Server.MapPath(".") + "\"

Resources