【问题标题】:Populate TextBox From Public Property From Code-Behind从代码隐藏的公共属性填充文本框
【发布时间】:2010-07-12 22:39:08
【问题描述】:

我正在尝试使用代码隐藏中的属性来填充文本框,而不是在代码隐藏中使用 textbox.text=。我正在使用 vb.net。这是aspx页面的代码:

<asp:Content ID="Content2" ContentPlaceHolderID="MainContentPlaceHolder" runat="server">
    <asp:TextBox runat="server" ID="roleTextBox" Text='<%# CurrentRole.Name%>'></asp:TextBox>
</asp:Content>

下面是代码背后的代码:

Imports Compass.UI.components
Imports Compass.Core.Domain
Imports Compass.Core.Domain.Model

Namespace app.administration.Roles
    Partial Public Class edit
        Inherits ClaimUnlockPage

        Private _roleRepository As IRoleRepository
        Private _roleId As Integer
        Private _role As Role

        Public Property CurrentRole() As Role
            Get
                Return _role
            End Get
            Set(ByVal value As Role)
                _role = value
            End Set
        End Property

        Public Property RoleRepository() As IRoleRepository
            Get
                Return _roleRepository
            End Get
            Set(ByVal value As IRoleRepository)
                _roleRepository = value
            End Set
        End Property

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

            LoadRole()

        End Sub

        Private Sub LoadRole()
            _roleId = Config.RequestVal("id", Request) 
            _role = _roleRepository.GetById(_roleId)
        End Sub
    End Class
End Namespace

当我运行页面时,文本框是空的。

【问题讨论】:

    标签: asp.net controls properties code-behind


    【解决方案1】:

    我没有在您的代码中看到roleTextBox.text=value!在LoadRole 或任何地方。
    如果你尝试绑定它,你需要一个 Role 的静态类。

    只是为了测试尝试在LoadRole中添加以下行

        Private Sub LoadRole()
          _roleId = Config.RequestVal("id", Request) 
          _role = _roleRepository.GetById(_roleId)
          roleTextBox.text =CrrentRole.Name;
        End Sub 
    

    如果 roleTextBox 仍然为空,则 CurrentRole.Name 为空。

    【讨论】:

      【解决方案2】:

      据我所知,您无法像这样绑定控件的属性(我希望您可以绑定,但我一直无法弄清楚或找到如何绑定的示例)。我一直这样做的方式是创建一个受保护的函数来返回,例如

      Protected Function GetCurrentRoleName() As String
          Return CurrentRole.Name
      End Function
      

      在你的标记绑定中像这样

      Text='<%# GetCurrentRoleName() %>'
      

      【讨论】:

        【解决方案3】:

        您必须 DataBind 包含您的文本框的容器控件(例如 GridView、UserControl 等)。所以至少你的 aspx 页面必须是数据绑定的。 “当在服务器控件上调用时,此方法会解析服务器控件及其任何子控件中的所有数据绑定表达式。”

           Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
                Me.CurrentRole = New Role("Administrator")
                Me.DataBind() '!!!!!!!
            End Sub
        
            Private _currentRole As Role
        
            Protected Property CurrentRole() As Role
                Get
                    Return _currentRole
                End Get
                Set(ByVal value As Role)
                    _currentRole = value
                End Set
            End Property
        
          Public Class Role
            Public Sub New(ByVal name As String)
                Me.Name = name
            End Sub
            Public Name As String
          End Class
        

        然后你可以使用你的 aspx-code 来设置 TextBox'-text 属性。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-09-09
          • 1970-01-01
          • 1970-01-01
          • 2011-01-21
          • 2012-07-29
          • 1970-01-01
          相关资源
          最近更新 更多