Solved

Calling a function of a dynamically loaded usercontrol

Posted on 2008-10-31
13
297 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
  • 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Anyway to make "All" the default in the dropdown? 6 36
Need a starter for ETL protocol? 4 55
No Data for DropDown List 2 23
Linq asp.net mvc 13 7
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…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

776 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