How to Set Dynamic UserControl Property in VB

Posted on 2008-11-19
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
    LVL 14

    Expert Comment

    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

    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.

    Author Comment

    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?
    LVL 14

    Expert Comment

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

    Author Comment

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

    Accepted Solution

    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

    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

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Join & Write a Comment

    Article by: Martin
    Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
    Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
    Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
    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…

    754 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