Solved

Calling a function of a dynamically loaded usercontrol

Posted on 2008-10-31
13
299 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:Starr Duskk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2

Author Comment

by:Starr Duskk
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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
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 2

Author Comment

by:Starr Duskk
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
 
LVL 2

Author Comment

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

Author Comment

by:Starr Duskk
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 2

Author Comment

by:Starr Duskk
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 2

Author Comment

by:Starr Duskk
ID: 22889228
I'll have to get back on this one. thanks.
 
0
 
LVL 2

Author Comment

by:Starr Duskk
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

696 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