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" />
Related
I have a simple UserControl in ASP.NET (VB) with the following code pages:
ASCX:
<%# Control Language="VB" AutoEventWireup="false" CodeFile="DistPOLine.ascx.vb" Inherits="DistPOLine" %>
<asp:Label ID="lbDistSKU" runat="server" Text="Label"></asp:Label>
ASCX.VB
Partial Class DistPOLine
Inherits System.Web.UI.UserControl
Public Property DistSKU As String
Get
DistSKU = lbDistSKU.Text
End Get
Set(value As String)
lbDistSKU.Text = value
End Set
End Property
End Class
If I reference the control directly in an aspx page, I can set its DistSKU property without a problem.
<%# Register src="DistPOLine.ascx" tagname="POline" tagprefix="POL" %>
<POL:POline ID="POline1" DistSKu="test Here" runat="server" /><br />
But in my code behind, where I create the user controls within a loop, I can't access the property directly, even by using FindControl and casting the control as a label. The controls get created, but I need to manipulate them after creation. (I've simplified the control and the code so only one property remains)
Imports System.IO
Imports Globals
Imports Approver
Imports System.Data.SqlClient
Imports System.Data
Partial Class OrderEntry_Entry
Inherits System.Web.UI.Page
Protected Sub Page_PreRender(sender As Object, e As EventArgs) Handles Me.PreRender
Dim custPOLine As UserControl, lLoop As Long
for lLoop=1 to 5
custPOLine = Page.LoadControl("~/DistPOLine.ascx")
custPOLine.ID = "poLine" & Format(lLoop + 1, "000")
Me.plPOLines.Controls.Add(custPOLine)
'ctrlLabel = Me.FindControl("poLine" & Format(lLoop + 1, "000") & "_lbDistSKU") 'This is not working
'ctrlLabel.Text = dr("CustSKU").ToString
next
End Sub
End Class
To access the property of your user control, you should declare your user control as the type of your custom user control.
Dim custPOLine As DistPOLine
custPOLine.DistSKU = "YourString"
And if you did want to find the control and edit the label that way, you should just be able to do a find control on the user control you created rather than the page you are adding them to.
Dim lbl As Label = custPOLine.FindControl("lbDistSKU")
lbl.Text = "YourString"
I'm having trouble with using my vb.net code in asp.net webpage.
<%# Page Language="vb" AutoEventWireup="false" MasterPageFile="~/Site.Master" CodeBehind="WebForm1.aspx.vb" Inherits="Banking_Application.WebForm1" CodeFile="~/WebForm1.aspx.vb" %>
class name is WebForm1. I've written code in a separate file and I want to use it in page.
Like
<% Dim total As Integer
Dim val As tests.WebForm1
val = New tests.WebForm1
total = val.TotalBranches()
total.ToString()
%>
I'm new to vb.net and asp.net.
All suggestions are welcome.
Thanks!
Well first of all i need to clarify a few things for you:
in Asp.net, you can embed code in your aspx page using <% %> code block, or write it in a separate file and use it from there.
in your page declaration, you specify the code behind of your page using the CodeBehind= attribute, so if you want to place your code in WebForm1.vb, your page declaration should include CodeBehind="WebForm1.vb" .
note: CodeFile="~/WebForm1.aspx.vb" is not needed.
the structure of the code behind for you aspx page, should look like that:
Public Class WebForm1
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Public Function test1()
Return "test"
End Function
End Class
and you can add more functions as needed. in the example above, i have added a function called test1 for the purpose of this example.
now, after you have created your code behind in the correct structure, you can call its methods from the aspx page as if it was the same page:
<% =test1()
%>
this will return "test" as specified by the function in code behind.
you do not need to instantiate the class.
I think you are missing what is happening with the mechanics of asp.net.
You either:
Write code directly in the aspx page (within the html)
Or, you wrtie code in the code behind page... your WebForm1.aspx.vb page. From the code behind page, you can access and manipulate the server controls that is on the aspx (html) side...
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
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 around 40 aspx pages in my website.
I want to use a javascript function which needs to be called when any of the 40 pages is loaded.
something like
I could have this function in the "head" section of each of the 40 aspx pages and then call in the body onload event. But I would like to have this function at a single place.
This is an existing app so I cannot create a master page and have all the pages derive from it.
Any ideas?
You will need to put the function in a .js file and call from your pages, but you will need to link to the script in all your 40 pages, since you can't add master page.
You can create a base class that it will be inherited by all these pages and write the logic of inserting javascript there.
So, do something like it:
Create a base class:
[Serializable]
public class RequiresFunctionBasePage : System.Web.UI.Page
{
public RequiresFunctionBasePage()
{
this.Load+= new delegate
{
this.ClientScript.RegisterClientScriptInclude("yourScript", "http://yoursite.com/yourJs.js");
this.ClientScript.RegisterStartupScript(this.GetType(),
"functionOnload", "functionName();", true);
}
}
}
And into your aspx codebehind:
public partial class yourPageNameGoesHere : RequiresFunctionBasePage
{
(...)
I suppose if you wanted to make it difficult/elegant, you could make an HttpModule that injects the script. That way it's only in one place and you can wire it up in the web.config.
Here's a sample httpModule
Public Class JavascriptInjector
Implements IHttpModule
Public Sub Init(ByVal context As System.Web.HttpApplication) Implements System.Web.IHttpModule.Init
AddHandler context.PreRequestHandlerExecute, AddressOf PreRequestHandlerExecute
End Sub
Private Sub PreRequestHandlerExecute(ByVal sender As Object, ByVal e As EventArgs)
Dim myPage = TryCast(HttpContext.Current.CurrentHandler, Page)
If myPage Is Nothing Then Exit Sub
AddHandler myPage.InitComplete, AddressOf Page_Init
End Sub
Sub Page_Init()
Dim myPage = TryCast(HttpContext.Current.CurrentHandler, Page)
If myPage Is Nothing Then Exit Sub
Dim path = myPage.ResolveUrl("~/js/jscript.js")
myPage.ClientScript.RegisterClientScriptInclude(myPage.GetType, "common", path)
End Sub
Public Sub Dispose() Implements System.Web.IHttpModule.Dispose
End Sub
End Class
Here's an entry in the web.config
<httpModules>
<add name="javascriptInjector" type="JavascriptInjector"/>
</httpModules>
You will need to modify the masterpage to include a new JS file with your new function or place your JS function in one of the JS files already included in the masterpage.
For a fully client side solution you could create a javascript file (possibly named script.js) that you link to from the head of your master page. In that file put your function.
So, the javascript in the script.js file would be something like this:
function SampleFunction(text) {
alert(text);
// or whatever code you want
}
and then in the head of your pages
<script type="text/javascript" src="script.js"></script>
and then your onload can be
onload="SampleFunction('hi there');"
have fun :)
robb