【问题标题】:Need to reference controls/properties in an ascx control (asp.net, written in vb)需要在 ascx 控件中引用控件/属性(asp.net,用 vb 编写)
【发布时间】:2011-06-27 14:07:48
【问题描述】:

我有一个动态添加到网页的用户控件。 ascx 有几个我想在运行时访问的控件。我可以访问 ascx 本身,但 ascx 上的控件都不可用。我尝试添加一个简单的公共变量,也尝试向 ascx 添加一个公共属性,但我无法在设计时访问它们中的任何一个(编译错误)。我会很感激任何想法 - 我被困住了...... :-)

我在 ascx 控件的代码隐藏中添加了以下内容:

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 = ""

我正在尝试使用以下变体来访问属性/ascx 控件:

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

【问题讨论】:

  • 你尝试了什么? (请出示代码。)有什么错误?

标签: asp.net vb.net


【解决方案1】:

托管页面应该有一个@Reference 指令指向加载的 ascx,以便它与页面一起编译。

类似:

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

这应该放在@Page 指令下方的指令区域中。

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

【讨论】:

  • 感谢 Variant - 你的回答加上你提供的链接让我解决了我的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-11-28
  • 2013-11-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-30
相关资源
最近更新 更多