|
-
Nov 24th, 2004, 05:44 AM
#1
Thread Starter
Hyperactive Member
Calling 'LoadControl' from a Class [RESOLVED]
Hi all,
How can I call the "LoadControl" function from a Class that isn't inside a webform?
Thanks
Last edited by tailz; Nov 25th, 2004 at 03:43 AM.
-
Nov 25th, 2004, 03:42 AM
#2
Thread Starter
Hyperactive Member
ug, why I wanted to do this I don't know.
Kinda got fixated with the idea "I must use this control", instead I just created a seperate class that actually now does a much better job :-)
-
Nov 28th, 2004, 11:49 AM
#3
Hyperactive Member
You could also have just created an instance of that control and used it...no LoadControl required.
-
Nov 28th, 2004, 02:57 PM
#4
Thread Starter
Hyperactive Member
Are you sure?
-
Nov 28th, 2004, 08:17 PM
#5
Hyperactive Member
Positive. Other than inheriting from some derivation of System.Web.UI.Control which isn't special in itself, it's just a class. If it has publicly accessible methods you need, create an instance of it and use them. If I add any public methods to a custom control or user control for some reason, I'll use NUnit to test them out(which means having a separate class that instantiates the control). Obviously if the methods depend on something like session state which requires an HttpContext then you're outta luck. On a page that uses your control, if you need to interact with the control from the page's code-behind, you'd have to declare a variable of the same type and name as your control that's on the page anyway, which is no different than you doing it yourself in another class that's not necessarily another page.
-
Nov 29th, 2004, 04:33 AM
#6
Thread Starter
Hyperactive Member
-
Nov 29th, 2004, 11:24 AM
#7
Hyperactive Member
Ok, here's an example. I did this in TextPad, so no visual studio influence.
Test.ascx is the ascx piece, there's nothing but a control declaration, really this page isn't even needed for the example:
VB Code:
<%@ Control Language="VB" AutoEventWireup="false" Inherits="VBControlTest.Test" %>
Test.ascx.vb is the codebehind for the user control:
VB Code:
Imports System
Imports System.Web
Imports System.Web.UI
Namespace VBControlTest
Public Class Test : Inherits System.Web.UI.UserControl
Private _name As String = ""
Public Property Name As String
Get
Return _name
End Get
Set( ByVal value As String )
_name = value
End Set
End Property
End Class
End Namespace
default.aspx is the test page:
VB Code:
<%@ Import Namespace="VBControlTest" %>
<script runat="server" language="VB">
Protected Overrides Sub OnInit( ByVal e As System.EventArgs )
Dim myTest As Test = New Test()
myTest.Name = "Chris"
lblName.Text = myTest.Name
End Sub
</script>
<html>
<body>
<form runat="server">
Name:<asp:Label ID="lblName" Runat="server" />
</form>
</body>
</html>
all that happens here is you create an instance of your custom user control, assign a value to the name property on that control, then assign the name property to the label on the form. Mustinherit applies to the UserControl class, not your class that inherited from UserControl, meaning you can't just create a System.Web.UI.UserControl, you have to always inherit from that which you do when you roll your own controls.
HTH,
pvb
-
Nov 29th, 2004, 11:53 AM
#8
Thread Starter
Hyperactive Member
Ya I understand inheritance. Just that the default for the Usercontrol Class when created under VB.NET is "Mustinherits" - looks like I can remove that without any issues anyway then it looks same as your code pretty much 
It's all a bit academic as I've opted for the pure class approach so I'll never get to try it out
Thanks for your replies
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|