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
Related
After exposing a public property in Top.Master it can be accessed in any child page that has a master type reference on its page.
How can the same properties be accessed from a nested page?
I tried to cascade the properties down the heirarchy but the child page errors when trying to access it.
I would prefer to access the exposed top.master property directly from the nested content page but am unsure of a good way to do this.
TOP.MASTER
<asp:Label ID="lblMsg" ClientIDMode="Static" runat="Server" />
TOP.MASTER.VB
Partial Public Class TopMaster
Inherits MasterPage
Public Property Msg As String
Get
Return lblMsg.Text
End Get
Set(value As String)
lblMsg.Text = value
End Set
End Property
End Class
CHILD.MASTER
<%# MasterType VirtualPath="~/Top.Master" %>
CHILD.MASTER.VB
Master.Msg = "Success"
CHILD.PAGE
<%# MasterType VirtualPath="~/Child.Master" %>
CHILD.PAGE.VB
Master.Master.Msg = "Success"
In your child.master class you can create a Msg property that would proxy the top master Msg property
You can add the following code in child.master.vb
Public Property Msg As String
Get
Return Master.Msg
End Get
Set(value As String)
Master.Msg = value
End Set
End Property
then in your child.page.vb you can access this property doing
Master.Msg = "Success"
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 %>
I have a web control that looks like this
public class Foo : WebControl
{
[Bindable(true)]
[Category("Default")]
[DefaultValue("")]
[Localizable(true)]
public string Bar { get; set; }
protected override void Render(HtmlTextWriter output)
{
output.WriteLine(Bar);
}
}
I want to put this webcontrol in my aspx page like so:
<cc1:Foo Bar="<%= Fa.La.La %>/otherstuff" runat="server" />
(obviously this code is simplified to show the problem)
In my Render method the variable Fa.La.La is not evaluated. It's coming in as the raw text "<%= Fa.La.La %>" How do I evaluate it?
I'm not particular how the variables are passed in. If the variables can be evaluated if they are passed in as <%# ... %>, that works fine. The point is I have some server-side variables I want evaluated before/while my Render() method is called.
The only thing I can think of is to use a regex to grab the contents of <%= ... %> and use reflection or something, but there has to be a more elegant way to do this.
This question is pretty similar to using server variables in a href <%= xx %> with runat=server, but it's not exactly the same since none of the answers there were useful.
Well, first you should be clear to diff between both tags.
here are some points i have read and used practically..
The <%= expressions are evaluated at render time
The <%# expressions are evaluated at DataBind() time and are not evaluated at all if
DataBind() is not called.
<%# expressions can be used as properties
in server-side controls.<%= expressions cannot.
read more it on MSDN Blog
You should have to use binding expression <%# expr %>.
<cc1:Foo Bar='<%# String.Concat(Fa.La.La,"/otherstuff")%>' runat="server" />
and call DataBind() method in code-behind.
public void page_load()
{
DataBind();
}
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 created a Label control inheriting from Label WebControl in CustomLabel.vb in my project where I want to use it. And I would like to use the code below in source-view as such:
<custom:SettingLabel ID="lblHelloWorld" runat="server"/>
How can I do that without creating a WebContolLibrary and using it as reference?
Namespace InternetLending.Controls
Public Class SettingLabel
Inherits Label
Protected msDefaultText As String
Protected moConfigXML As New ConfigXMLParser()
Public Overridable Property DefaultText() As String
Get
Return Me.msDefaultText
End Get
Set(ByVal vsValue As String)
Me.msDefaultText = vsValue
End Set
End Property
Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
MyBase.OnLoad(e)
If Not String.IsNullOrEmpty(Me.moConfigXML.GetLabelTextByID(Me.ID)) Then
Me.Text = Me.moConfigXML.GetLabelTextByID(Me.ID)
Else
Me.Text = Me.DefaultText
End If
End Sub
End Class
End Namespace
You just need to add this to the top of your page. You don't need to make a separate library or anything.
<%# Register Assembly="Assembly" Namespace="Assembly.Controls" TagPrefix="custom" %>
The problem here is that you are not taking into account the default namespace.
Try using the following directive:
<%# Register Assembly="InternetLending" Namespace="InternetLending.InternetLending.Controls" TagPrefix="custom" %>
Or change your Namespace for the SettingLabel control like so:
Namespace Controls
Public Class SettingLabel
Inherits Label
and then use the following directive:
<%# Register Assembly="InternetLending" Namespace="InternetLending.Controls" TagPrefix="custom" %>
See: Managing Namespaces in VB.Net for more info.
I think you can just register the assembly in the web.config or page directive, even if it's in the same project.
<add tagPrefix="MyControls" namespace="MyProgram.Controls.MyControl" assembly="MyProgram.Controls.MyControl" tagName="MyControl" />