Results 1 to 7 of 7

Thread: [2005] How can I do this?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2005
    Posts
    64

    [2005] How can I do this?

    Say I have a custom control - TestControl.ascx

    if I had code like this on a page

    Code:
    dim oUserControl as UserControl
    
    oUserControl = LoadControl("TestControl.ascx")
    pnlMain.controls.add(oUserControl)
    Say TestControl has a public method called test.. I want to be able to go

    Code:
    CType(oUserControl, TestControl).Test()
    however, it doesn't seem like it'll work... any input?

  2. #2

    Thread Starter
    Lively Member
    Join Date
    Nov 2005
    Posts
    64

    Re: [2005] How can I do this?

    can someone move this to ASP.NET ?

  3. #3
    Fanatic Member space_monkey's Avatar
    Join Date
    Apr 2005
    Location
    神と歩くこと
    Posts
    573

    Re: [2005] How can I do this?

    Have you tried

    oUserControl.test()

    ?
    Using VB6 or VB.net 2008 with .net 3.5
    "Life... death... either way I'll be confined to a small cubicle!" - Hermes Conrad

  4. #4
    C# Aficionado Lord_Rat's Avatar
    Join Date
    Sep 2001
    Location
    Cave
    Posts
    2,497

    Re: [2005] How can I do this?

    Quote Originally Posted by BootsSiR
    however, it doesn't seem like it'll work... any input?
    Have you actually tried it? It seems like you are conjecturing, and from experience, it should work.

    Now, I work in the C# world, not the VB World, but the following works fine:

    UserControlType oUC = new UserControlType();
    this.Controls.Add(oUC);

    [...]

    UserControl oControl = Page.FindControl("oUCIdentifier");
    (oUC)(oControl).Test();
    Need to re-register ASP.NET?
    C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i

    (Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)

  5. #5
    Frenzied Member
    Join Date
    Mar 2004
    Location
    Orlando, FL
    Posts
    1,618

    Re: [2005] How can I do this?

    Is the CTYpe messing you up? I would use:

    DirectCast(oUserControl, TestControl).Test()
    Sean

    Some days when I think about the next 30 years or so of my life I am going to spend writing code, I happily contemplate stepping off a curb in front of a fast moving bus.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Nov 2005
    Posts
    64

    Re: [2005] How can I do this?

    My web user control is delared as follows

    Code:
    Partial Class Controls_TestControl
    
       Dim m_SzTest as String
    
       Public Sub Test()
            m_szTest = "hello"
       End Sub
    
    End Class
    this code

    Code:
    dim oUserControl as UserControl
    
    oUserControl = LoadControl("TestControl.ascx")
    
    DirectCast(oUserControl, Controls_TestControl).Test()
    
    pnlMain.controls.add(oUserControl)
    gives me the error Type 'Controls_TestControl' is not defined

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Nov 2005
    Posts
    64

    Re: [2005] How can I do this?

    I can't even

    Code:
    Dim oUserControl as Controls_TestControl = LoadControl("TestControl.ascx")
    do I have to add a reference to the control?

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