Hi all,
How can I call the "LoadControl" function from a Class that isn't inside a webform?
Thanks
Printable View
Hi all,
How can I call the "LoadControl" function from a Class that isn't inside a webform?
Thanks
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 :-)
You could also have just created an instance of that control and used it...no LoadControl required.
Are you sure? :bigyello:
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.
Hi Pvb,
Thanks for replying. I'm a fairly new to ASP.NET so excuse me if im writing bollox :D
My Usercontrols by default are getting declared "MustInherits", therefore I cant just instantiate them. Not sure if I can change this and get away with it? If I can then that solves all I guess :D
Also, as you mentioned, Im not creating the control on a page; which is the problem. If I was I'd just use the LoadControl function and I understand that you can drop the controls onto a page and create a variable that is bound to it :thumb:
I've no idea what NUnit is :blush:
I hate Mondays. I've just deleted my DB class and thanks to .NET security being a pain the arse, it was on my HDD so no backup. :mad:
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
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 :D
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