Link to home
Start Free TrialLog in
Avatar of Starr Duskk
Starr DuskkFlag for United States of America

asked on

Calling a function of a dynamically loaded usercontrol

I am loading a user control:

        userControlName = "~/Modules/Login/Controls/RegisterControl.ascx"
        Dim userControl As Control = Page.LoadControl(userControlName)
        userControl.ID = "pvRegister" & e.PageView.ID

        e.PageView.Controls.Add(userControl)


Now I want to call a function of that user control to set some values within it. How do I call a dynamically created control?

I know I could do it with this if it weren't dynamic, but I don't know how to get it dynmically.
Me.pvRegisterControlName.SetUserType(Enums.UserType.Forums)

I have 5 user controls on the page, all the same control, but different display based on what value I passed into them, so I need to send that value.

thanks.
ASKER CERTIFIED SOLUTION
Avatar of ororiole
ororiole
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Starr Duskk

ASKER

Yes, my class name inside the control is RegisterControl.
The first error I get is: RegisterControl_ascx is not a member of ASP.
The second is: mycontrol is not declared.
For Ctype(usercontrol.... : usercontrol is a type and cannot be used as an expression.
Did we want the name of my controlID there?
Sorry, none of it is working. :(
 
 
At the top of your UserControl ascx page is a @Control directive. Post that so I can see it.

As for this one:
For Ctype(usercontrol.... : usercontrol is a type and cannot be used as an expression.
This was the code you had:
Dim userControl As Control = Page.LoadControl(userControlName)
Is "userControl" the name of the variable here?



One of the problems is we shouldnt be using userControl as a variable.

'Instead of this:
Dim userControl As Control = Page.LoadControl(userControlName)
        userControl.ID = "pvRegister" & e.PageView.ID
 
        e.PageView.Controls.Add(userControl)
 
'Do this:
Dim uc As Control = Page.LoadControl(userControlName)
        uc.ID = "pvRegister" & e.PageView.ID
 
        e.PageView.Controls.Add(uc)
 
'then convert it:
ASP.RegisterControl_ascx myControl = CType(uc, ASP.RegisterControl_ascx)
 
myControl.SetUserType(Enums.UserType.Forums) 

Open in new window

I still get the same error:
RegisterControl_ascx is not a member of ASP.
Do I need a specific Import that I am missing?
I am loading each control in a page view. Here's what I have:
       Dim userControlName As String = String.Empty
        userControlName = "~/Modules/Login/Controls/RegisterControl.ascx"
        Dim uc As Control = Page.LoadControl(userControlName)
        uc.ID = "pvRegister" & e.PageView.ID
        e.PageView.Controls.Add(uc)
        ASP.RegisterControl_ascx myControl = CType(uc, ASP.RegisterControl_ascx))
 

 
 
Post your @control directive.
Please be more specific where I might find this?
I'm no longer registering it in the code in front.
 
Do you mean this?
 
<%@ Control Language="VB" AutoEventWireup="false" CodeFile="RegisterControl.ascx.vb"
    Inherits="RegisterControl" %>
 
Yes, thats it. Two questions:
1. Do you have RegisterControl inside of a namespace? If so, what is it.

2. What is the name of your project?
No, I don't believe I do have a RegisterControl inside a namespace.
And this is not a project, it is a website.
I have another question open about the difference between a project and website if you care to answer that one.
http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Studio_.NET_2005/Q_23875122.html
 
Well there is something funny about the namespace that is causing this problem. We did solve the problem of "For Ctype(usercontrol.... : usercontrol is a type and cannot be used as an expression" by changing your variable name from userControl to uc.

As for the namespace problem, if you could post the codebehind to your usercontrol, that would be  a big help.
I'll have to get back on this one. thanks.
 
I'm never going to get back on this one I guess, so I'm just going to close it out.
 
thanks!