SunfiShie
SunfiShie

Reputation: 416

Need to reference controls/properties in an ascx control (asp.net, written in vb)

I have a user control that I'm adding to a webpage dynamically. The ascx has a couple of controls that I want to have access to at runtime. I can access the ascx itself, but none of the controls on the ascx are available. I have tried adding a simple public variable and also tried adding a public property to the ascx, but I am unable to get access to either of them at design time (compile errors). I would appreciate any ideas - I'm stuck... :-)

I added the following to the code-behind of the ascx control:

Public Property areaCode() As String
        Get
            Return iebEmpPhoneAreacode.Text
        End Get
        Set(ByVal value As String)
            iebEmpPhoneAreacode.Text = value
        End Set
    End Property

Public AreaCodeStr As String = ""

and am trying to use variations of the following to access the property/ascx controls:

For Each ctrl As Control In pnlPhones.Controls
    If ((TypeOf ctrl Is ctrlPhone) And (ctrl.ID = vbNullString)) Then
        (DirectCast(ctrl, ctrlPhone)).AreaCodeStr = "test"

        'or try this

        ctrl.areaCode = "test"
    End If
Next

Upvotes: 0

Views: 1519

Answers (1)

Variant
Variant

Reputation: 17385

The hosting page should have an @Reference Directive pointing to the loaded ascx so it will be compiled with the page.

Something like:

<%@ Reference VirutalPath="YourReferenceControl.ascx" %>

This should go in the directives area somewhere below the @Page directive.

http://msdn.microsoft.com/en-us/library/w70c655a.aspx

Upvotes: 1

Related Questions