Results 1 to 8 of 8

Thread: Calling 'LoadControl' from a Class [RESOLVED]

  1. #1

    Thread Starter
    Hyperactive Member tailz's Avatar
    Join Date
    Jul 2002
    Posts
    306

    Resolved 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.

  2. #2

    Thread Starter
    Hyperactive Member tailz's Avatar
    Join Date
    Jul 2002
    Posts
    306
    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 :-)

  3. #3
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fort Collins, CO
    Posts
    366
    You could also have just created an instance of that control and used it...no LoadControl required.

  4. #4

    Thread Starter
    Hyperactive Member tailz's Avatar
    Join Date
    Jul 2002
    Posts
    306
    Are you sure?

  5. #5
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fort Collins, CO
    Posts
    366
    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.

  6. #6

    Thread Starter
    Hyperactive Member tailz's Avatar
    Join Date
    Jul 2002
    Posts
    306
    Hi Pvb,

    Thanks for replying. I'm a fairly new to ASP.NET so excuse me if im writing bollox

    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

    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

    I've no idea what NUnit is

    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.

  7. #7
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fort Collins, CO
    Posts
    366
    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:
    1. <%@ Control Language="VB" AutoEventWireup="false" Inherits="VBControlTest.Test" %>

    Test.ascx.vb is the codebehind for the user control:
    VB Code:
    1. Imports System
    2. Imports System.Web
    3. Imports System.Web.UI
    4. Namespace VBControlTest
    5.     Public Class Test : Inherits System.Web.UI.UserControl
    6.         Private _name As String = ""
    7.         Public Property Name As String
    8.             Get
    9.                 Return _name
    10.             End Get
    11.             Set( ByVal value As String )
    12.                 _name = value
    13.             End Set
    14.         End Property
    15.     End Class
    16. End Namespace

    default.aspx is the test page:
    VB Code:
    1. <%@ Import Namespace="VBControlTest" %>
    2. <script runat="server" language="VB">
    3. Protected Overrides Sub OnInit( ByVal e As System.EventArgs )
    4.     Dim myTest As Test = New Test()
    5.     myTest.Name = "Chris"
    6.     lblName.Text = myTest.Name
    7. End Sub
    8. </script>
    9. <html>
    10.     <body>
    11.         <form runat="server">
    12.             Name:<asp:Label ID="lblName" Runat="server" />     
    13.         </form>
    14.     </body>
    15. </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

  8. #8

    Thread Starter
    Hyperactive Member tailz's Avatar
    Join Date
    Jul 2002
    Posts
    306
    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
  •  



Click Here to Expand Forum to Full Width