我正在尝试使用代码隐藏中的属性来填充文本框,而不是在代码隐藏中使用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
当我运行页面时,文本框为空。
答案 0 :(得分:0)
我的代码中没有看到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为空。
答案 1 :(得分:0)
据我所知,你不能绑定像这样的控件的属性(我希望你可以,但我从来没有能够弄清楚或找到一个例子如何)。我一直这样做的方法是创建一个受保护的函数来返回,例如
Protected Function GetCurrentRoleName() As String
Return CurrentRole.Name
End Function
在你的标记绑定中如此
Text='<%# GetCurrentRoleName() %>'
答案 2 :(得分:0)
您必须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代码来设置TextBox'-text属性。