Reference to a non shared member requires an object reference - asp.net

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(".") + "\"

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 set connection string in Linq to SQL constructor

Im trying to set a connection string for a my data access layer which is using a value from my Web.Config file from another project.
I create a new class library and add a dbml (Linq to SQL) file and drag a table.
I then add this code to the code file
Partial Public Class MyDataContext
Public Sub New()
MyBase.New(ConfigurationManager.ConnectionStrings("WebConnectionString").ConnectionString, mappingSource)
OnCreated()
End Sub
End Class
This gave me an error "'Public Sub New()' has multiple definitions with identical signatures."
I understood what the error means so did a quick search on a way around it as when i recompiled the project the same problem remained. The way to approach this was/is to override the OnCreated method so i changed the code to:
Private Sub OnCreated()
Me.New(ConfigurationManager.ConnectionStrings("WebConnectionString").ConnectionString, mappingSource)
End Sub
This gave the error "Constructor call is valid only as the first statement in an instance constructor" but not a lot of ways to overcome it (well i see a few C# examples but im sure im an converting it to the correct VB .Net code)
After further research no matter what i do, i dont seem to be able to set the connection string in my DAL which should be using the Web.Config connection string value from another project.
What could i be doing wrong?
OK, In C# first:
partial class MyDataContext
{
public static MyDataContext Create()
{
return new MyDataContext(ConfigurationManager.ConnectionStrings("WebConnectionString").ConnectionString,
mappingSource);
}
// etc
}
Now, my attempt to translate that in VisualBasic.NET
Partial Public Class MyDataContext
Public Shared Function Create() as MyDataContext
return New MyDataContext (ConfigurationManager.ConnectionStrings("WebConnectionString").ConnectionString,
mappingSource)
End Function
End Class
Called via:
Dim db as MyDataContext = MyDataContext.Create()

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.

Error parsing attribute on #page directive

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.

Getting a ScriptReference from a ScriptResourceMapping definition in a custom control

I am building a custom control with client side scripts that I would like to reference using ScriptManager.ScriptResourceMapping (to make use of the Path and DebugPath attributes).
I would like the custom control to be easily ported to other projects - i.e. I would like to drag and drop the codebehind files (and eventually make the control a separate DLL, but for now the drag and drop will suffice). I would therefore like to avoid (1) having the client script as an embedded resource, (2) referenced as a WebResource in the AssemblyInfo, or (3) have the ScriptManager.ScriptResourceMapping.AddDefinition in global.asax.
In simple terms can I get the script management code to be in just the custom control's code?
At the moment I am getting an error stating that the script reference cannot be found in the assembly, and I guess I am setting the wrong assembly.
My custom control code is as follows:
Public Class MyControl
Inherits System.Web.UI.LiteralControl
Implements ISectionControl, IScriptControl
Private _scriptReference As ScriptReference
Public Sub New()
' Add the resource mapping
ScriptManager.ScriptResourceMapping.AddDefinition("MyControlScript", New ScriptResourceDefinition With {
.ResourceAssembly = System.Reflection.Assembly.GetExecutingAssembly,
.ResourceName = "MyControlScript.js",
.Path = "Path.To.MyControlScript.minimised.js",
.DebugPath = "Path.To.MyControlScript.original.js"
})
' Set the script reference
_scriptReference = New ScriptReference("MyControlScript.js", Assembly.GetExecutingAssembly.FullName)
End Sub
Protected Overrides Sub OnPreRender(e As System.EventArgs)
MyBase.OnPreRender(e)
' Register the script
ScriptManager.GetCurrent(Page).RegisterScriptControl(Of MyControl)(Me)
' Some code to set the Text of the literal control
' ...
End Sub
Public Function GetScriptDescriptors() As System.Collections.Generic.IEnumerable(Of System.Web.UI.ScriptDescriptor) Implements System.Web.UI.IScriptControl.GetScriptDescriptors
Return New ScriptDescriptor() {}
End Function
Public Function GetScriptReferences() As System.Collections.Generic.IEnumerable(Of System.Web.UI.ScriptReference) Implements System.Web.UI.IScriptControl.GetScriptReferences
Return New ScriptReference() {_scriptReference}
End Function
End Class
I hope the question makes sense. Thanks for taking the time to read through.
Ali
Answered this myself, I was getting confused with the assemblies and the constructors for ScriptReference. I just wanted a ScriptReference with the (mapped) name so I used the blank constructor and then set Name. I could then remove the assembly information.
Adjusting the following sorted the problem out:
Public Sub New()
' Add the resource mapping
ScriptManager.ScriptResourceMapping.AddDefinition("MyControlScript", New ScriptResourceDefinition With {
.Path = "Path.To.MyControlScript.minimised.js",
.DebugPath = "Path.To.MyControlScript.original.js"
})
' Set the script reference
_scriptReference = New ScriptReference() With {.Name="MyControlScript"}
End Sub

Resources