Solved

Calling a function of a dynamically loaded usercontrol

Posted on 2008-10-31
13
301 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

691 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