Solved

Calling a function of a dynamically loaded usercontrol

Posted on 2008-10-31
13
296 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 1

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
 
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: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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

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 1

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 1

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 1

Author Comment

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

867 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

20 Experts available now in Live!

Get 1:1 Help Now