How can I access a const in a module from an aspx page?
I use this to access const in the code behind:
<%=MY_CONST%>
What is the difference when the Const is in a different class (a module in the project)?
First edit:
So I tried the solutions "use <%= Myclass.Myconst %>" but I got the error "Name Myclass is not declared".
Here is more details:
The class is in the same project.
It is a module wich contains multiple Const.
I use VB.net
.NET 3.5
any ideas?
Solution:
I found the solution here :
I need to add an import statement:
<%# Import Namespace="Mynamespace.Myclass" %>
and then I can use:
<%=MyConst%>
Public Class TheClass
Public Const MY_CONST As String = "123456"
End Class
You can access it like:
<%=TheClass.MY_CONST%>
If you are using c# make sure the class is in the same namespace.
Make sure the constant value is a public value and reference it like this:
<%= YourClassName.MY_CONST %>
Related
I have a VB.NET WebForms app and am trying to use inline server tags in the markup to call a function that's located in a module. No matter what I do, I cannot get intellisense to show the method when I use <%= %> or <%# %> tags. Here's my module:
Module TestModule
Function test() As String
Return String.Empty
End Function
End Module
However, when I convert the module into a class and convert the methods into shared methods, I can do this:
Public Class TestClass
Shared Function test() As String
Return String.Empty
End Function
End Class
I can place this within my form:
<%= MyApp.TestClass.test%>
I can use this in control binding:
<asp:Button ID="cmWhatever" Text='<%#MyApp.TestClass.test%>' runat="server" />
How can I reference module methods from the markup?
'Use this instead:
Public Module TestModule
Public Function test() As String
Return String.Empty
End Function
End Module
'More over you have to use <%# Import Namespace="Your root namespace" %> on the start of the aspx page
We have ascx user controls written in VB.NET for an ASP.NET 4.0 project. The built-in VS page validator always displays the message from the question for all custom properties of our user control.
For instance, here is the beginning of the code of one of our controls:
<%# Control Language="VB" ClassName="PicView" %>
<%# Import Namespace="System.Drawing" %>
<script runat="server">
Public ImageUrl As String
When we try to use this control using code like this
<%# Register TagPrefix="foo" TagName="PicView" src="~/ascx/PicView.ascx" %>
<foo:PicView ImageUrl="screenshots/image.gif" runat="server" />
, the "Error List" pane displays this message:
Attribute 'ImageUrl' is not a valid attribute of element 'PicView'
The property works fine in compiled aspx pages, but how to get rid of this in the VS IDE? And enable IntelliSense for such properties if it's possible?
Got answer here:
ImageUrl needs to be a property, rather than a field, for example:
Public Property ImageUrl As String
Get
Return _imageUrl
End Get
Set(value As String)
_imageUrl = value
End Set
End Property
Private _imageUrl As String
I have the following two pages:
Default.aspx
Default.aspx.cs
How do I access variables in the code-behind file (Default.aspx.cs) from my embedded code in (Default.aspx) with the <% %> syntax?
Any public or protected (but not private, the "page" itself inherits from the code-behind Page class) class-level member can be accessed in this way. For example, if your code-behind class has a property:
protected string SomeValue { get; set; }
Then in your aspx code you can refer to it:
<% =SomeValue %>
Simply reference them as if they are part of the current class.
<%= this.Foo %>
If you don't specify the access modifier for the variable the default is private and hence you cannot access it inside your page. It works for public, protected and friend. I prefer to use protected variables than public ones.
I have an ascx which I want to load and cast from a class sitting within App_Code. I can't get it to work from the App_Code class, although I can get it to work from an aspx page.
The ASPX page technique works fine with the following code:
pc = LoadControl("enquirycapture.ascx");
((ASP.enquirycapture_ascx)pc).CustomProperty = customObject;
(Note: I have the following in the aspx page:)
<%# Reference VirtualPath="~/enquirycapture.ascx" %>
However, when I try casting the control from within the App_Code class then it can't 'see' the ascx class, and therefore I am unable to cast to it to set the custom properties(I can load it, but not cast it). I don't know how to replicate the <% Reference...> thing from within the App_Code class. Anyone know how I can reference (and thus cast) my ascx from the App_Code class? Thanks.
App_Code compiles to a seperate assembly that can't reference types in a CodeFile.
But you can add interface/base class to your App_Code folder that identifies the custom properties and methods that you intend to implement in your usercontrol:
public class EnquiryCaptureBase : System.Web.UI.UserControl
{
public object CustomProperty { get; set; }
}
and then
public partial class EnquiryCapture : EnquiryCaptureBase
{
}
and finally somewhere in App_Code:
pc = LoadControl("enquirycapture.ascx");
((EnquiryCaptureBase)pc).CustomProperty = customObject;
I have a UserControl that is working fine. It is declared like this.
public partial class DynamicList : System.Web.UI.UserControl
{
protected static BaseListController m_GenericListController = null;
public DynamicList()
{
m_GenericListController = new GenericListController(this);
}
}
Now I want to override this control so I can change some of the properties. I have created a class like this.
public partial class JobRunningList : DynamicList
{
public JobRunningList()
{
m_GenericListController = new JobListController(this);
(m_GenericListController as GenericListController).ModuleId = 14;
}
}
It appears that the controls in the DynamicList are not getting created though when I use the JobRunningList control now causing predictably bad results. The DynamicList UserControl has a ListView on it and a few other controls. It appears these are not created when using the JobRunningList. Is there any secret to this?
The boring workaround would be to make JobRunningList as plain old user control that contains a DynamicList and just sets the properties of the inner control in its OnLoad. That's awkward if DynamicList has many other properties that you want to access from the page though, as JobRunningList would have to define matching properties of its own. Getting back to the inheritance approach, then...
The DynamicList class just contains the code behind logic, so what you're doing works nicely if you want the second control to reuse the logic behind the first but provide a new UI of its own.
The markup in your .ascx file gets compiled into another class that inherits DynamicList, so if you can get your JobRunningList class to inherit that class instead of DynamicList, you'll get the result you want. This class gets a default name derived from the filename, but you can avoid guessing that by setting a ClassName in the control directive to use instead of the automatic name.
Take a simple base control like
<%# Control Language="C#" AutoEventWireup="true"
CodeFile="HelloControl.ascx.cs" Inherits="HelloControlBase"
ClassName="MyControls.HelloControl" %>
Hello <%= Name %>
with an unexciting code-behind like
public partial class HelloControlBase : System.Web.UI.UserControl
{
public string Name
{
get;
set;
}
}
Now we want to override the Name property in a new control. First we need HelloAlice.ascx
<%# Control Language="C#" AutoEventWireup="true"
CodeFile="HelloAliceControl.ascx.cs"
Inherits="HelloAliceControl" %>
Not much to see here, since we're leaving all the work to the original ascx. Now in the code-behind,
public partial class HelloAliceControl : MyControls.HelloControl
{
protected void Page_Load(object sender, EventArgs e)
{
this.Name = "Alice";
}
}
We just inherit MyControls.HelloControl and set the Name property, and it looks like we're done.
The problem is knowing when MyControls.HelloControl is visible. As long as your derived control is in the same directory as the parent control you'll probably be OK, otherwise it's quite easy to run into build errors complaining that the class doesn't exist because the parent control hasn't been compiled yet.
If I understand correctly, you want the interface to be the same. In that case, I would create some properties instead. Perhaps just a simple enumeration i.e. ListType.