How to Set Dynamic UserControl Property in VB

I'm building a table dynamically, and in one of the cells, I'd like to place a dynamically-created user control as well as set a property of that user control. My user control consists of a drop-down list (for grade levels in public school, and all I want to do is to set the SelectedItem property.  I'm also doing this in VB.

The user control is in "~/UserControls/GradeDropDown.ascx", and "myPage" is a reference to the calling page that was passed in.

Here's the line of code
Dim myControl As UserControl = CType(myPage.LoadControl("~\UserControls\GradeDropDown.ascx"), UserControl).

In my Watch window, I see the value of "myControl" is {ASP.usercontrols_gradedropwdown.ascx}.  If I expand the tree in the Watch window, I see the first item is also ASP.usercontrols_gradedropdown_ascx, and expanding that, I can see the properties I want to set.  However, I don't know how to get there from here.

So, how do I set up and/or define "myControl" in such a way that I can access its properties?  And what is the syntax, in VB, for that?

Thank you for ANY help!
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

What is the type of the user control?     If it is GradeDropDown  then you might be able to do something like this:

Dim tempControl as GradeDropDown = CType(myControl,GradeDropDown)

Then access the properties.  

I have not tried this, but might be a start in a right direction.
Hi here's the steps you'll need

1: make sure your usercontrol's namespace and your class file (where you are trying to load the control)
is same. This is not a requirement, but sometimes you might not just find the control if namespace is different.

2: I assume after doing step 1, you will be able to get access to your user control's class which may be
GradeDropDown (basically you should get it via intellisense if you start typing it in your code)

Now get a refernce to your control

Dim myUC1 as new GradeDropDown

Now set its properties including the id like this: = "_uc001"

myUC1.YourPropertyName1 = "property value here"
Then add it to wherever you like.

This was an overview, let me know if you have issues, or
you wnat to do it in some specific way.
Paris1929Author Commented:
Maybe this is something that's not going to work the way it's set up(?).  The user control is in a folder called "UserControls", and the class that's trying to do the dynamic table is in "App_Code", and the page that calls the class methods is in a foldercalled "Pages".

So, if I try and use "GradeDropDown" as a type in my class method, I get the error "type 'gradedropdown' is not defined".  

Any thoughts on this arrangement and whether anything can be moved or adjusted architecturally so it would work?
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Are they all in the same Name Space?  Do you need to reference a different namespace for them?
Paris1929Author Commented:
Hi jjardine ~ I'm not using namespaces.  I'm new to .Net with .Net 2.0 - are namespaces still used?
Paris1929Author Commented:
I think I found a way that works.  Chalk it up to my being rather new to .Net and not understanding all the terminology yet (stub classes, base classes, helper classes, etc).  Here's what I did from what I cobbled together from other posts here and on the net...

1. Made a new class, put it in App_Code, and called it "DropDownBase.vb", and expose the property I want to set from within my other class (the one that's making the dynamic table and user control). (see code snippet)

2. Then I changed my user control to inherit from this base class and override the property.

3. Then, in my main class, I was able to use the type "DropDownBase" instead of "UserControl" (from my original question)
Dim mycontrol As DropDownBase = CType(myPage.LoadControl("~\UserControls\GradeDropDown.ascx"), DropDownBase)

4. And now I can reference the property using "myControl.myTopChoice()" in my code.


Thank you so much to jjardine and techExtreme for trying to help me!!

Public MustInherit Class DropDownBase
    Inherits System.Web.UI.UserControl
    Protected myTopChoice As String
    Public Overridable Property TopChoice() As String
            Return myTopChoice
        End Get
        Set(ByVal value As String)
            myTopChoice = value
        End Set
    End Property
End Class
Partial Class UserControls_GradeDropDown
    Inherits DropDownBase
 Public Overrides Property SelectedGrade() As String
 End Property

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
I am glad you figured out a way to make it work.   All versions of .Net rely heavily on Namespaces.   VB does a pretty good job of encapsulating that by not showing it on every code page whereas C# would have every class wrapped in a Namespace.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.