How to Set Dynamic UserControl Property in VB

Posted on 2008-11-19
Medium Priority
Last Modified: 2013-12-26
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!
Question by:Paris1929
  • 3
  • 3
LVL 14

Expert Comment

ID: 22997193
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.
LVL 12

Expert Comment

ID: 22997298
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:

myUC1.id = "_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.

Author Comment

ID: 22997710
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?
Get quick recovery of individual SharePoint items

Free tool – Veeam Explorer for Microsoft SharePoint, enables fast, easy restores of SharePoint sites, documents, libraries and lists — all with no agents to manage and no additional licenses to buy.

LVL 14

Expert Comment

ID: 22998046
Are they all in the same Name Space?  Do you need to reference a different namespace for them?

Author Comment

ID: 22998233
Hi jjardine ~ I'm not using namespaces.  I'm new to .Net with .Net 2.0 - are namespaces still used?

Accepted Solution

Paris1929 earned 0 total points
ID: 22999845
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

LVL 14

Expert Comment

ID: 23000359
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.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

807 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