Solved

Calling a function of a dynamically loaded usercontrol

Posted on 2008-10-31
13
295 Views
Last Modified: 2012-06-21
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.
0
Comment
Question by:BobCSD
  • 7
  • 6
13 Comments
 
LVL 16

Accepted Solution

by:
ororiole earned 500 total points
ID: 22855711
You will need to cast your userControl reference to its type. Its type is composed party of its Class name. Since your file was RegisterControl.ascx I am going to assume the class name was RegisterControl. However, the framework puts a prefix of ASP. and a suffix of _ascx on the Class name to create the type.

So, in this case the type of the control is ASP.RegisterControl_ascx
ASP.RegisterControl_ascx myControl = CType(userControl, ASP.RegisterControl_ascx)
 

myControl.SetUserType(Enums.UserType.Forums)

Open in new window

0
 
LVL 1

Author Comment

by:BobCSD
ID: 22861989
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. :(
 
 
0
 
LVL 16

Expert Comment

by:ororiole
ID: 22863047
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?



0
 
LVL 16

Expert Comment

by:ororiole
ID: 22863269
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

0
 
LVL 1

Author Comment

by:BobCSD
ID: 22868593
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))
 

 
 
0
 
LVL 16

Expert Comment

by:ororiole
ID: 22868681
Post your @control directive.
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 1

Author Comment

by:BobCSD
ID: 22869268
Please be more specific where I might find this?
I'm no longer registering it in the code in front.
 
0
 
LVL 1

Author Comment

by:BobCSD
ID: 22869274
Do you mean this?
 
<%@ Control Language="VB" AutoEventWireup="false" CodeFile="RegisterControl.ascx.vb"
    Inherits="RegisterControl" %>
 
0
 
LVL 16

Expert Comment

by:ororiole
ID: 22869361
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?
0
 
LVL 1

Author Comment

by:BobCSD
ID: 22880044
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
 
0
 
LVL 16

Expert Comment

by:ororiole
ID: 22880753
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.
0
 
LVL 1

Author Comment

by:BobCSD
ID: 22889228
I'll have to get back on this one. thanks.
 
0
 
LVL 1

Author Comment

by:BobCSD
ID: 23154157
I'm never going to get back on this one I guess, so I'm just going to close it out.
 
thanks!
 
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
This video discusses moving either the default database or any database to a new volume.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now