这是中继器:
<asp:Repeater ID="rptrReports" runat="server">
<ItemTemplate>
<div style="margin: 2">
<asp:Label ID="lblAccount" runat="server" Text='<%#Eval("Account").FullName%>' />
<asp:TextBox ID="txtDescription" runat="server" MaxLength="256" Text='<%#Eval("Description")%>'
ReadOnly="<%# Not Account.IsAdmin %>" BackColor="<%# If(Account.IsAdmin,Color.White, Color.Transparent) %>"
BorderStyle="<%# If(admin, BorderStyle.NotSet, BorderStyle.None) %>"
/>
<asp:TextBox ID="txtNote" runat="server" MaxLength="1024" Text='<%#Eval("Note")%>'
ReadOnly="<%# Not Account.IsAdmin %>" BackColor="<%# If(Account.IsAdmin,Color.White, Color.Transparent) %>"
BorderStyle="<%# If(admin, BorderStyle.NotSet, BorderStyle.None) %>" />
<!-- Here I have many more controls which I want to apply same rules !-->
</div>
</ItemTemplate>
</asp:Repeater>
我想在代码中动态设置 itemtemplate 的这些控件,这样 asp.net 代码不应该看起来那么难看:
Private Sub HandleTextBoxes()
Dim admin = Account.IsAdmin
For Each tb As TextBox In _
From c In rptrReports.Controls _
Where TypeOf c Is TextBox 'ItemTemplate doesn't expose the properties :(
With tb
.ReadOnly = Not admin
.BackColor = If(admin, Color.White, Color.Transparent)
.BorderStyle = If(admin, BorderStyle.NotSet, BorderStyle.None)
End With
Next
End Sub
另一方面,我不想单独设置它或每个绑定的 ItemTemplate,我想通过父控件(中继器)本身来设置它。
C# 中的答案也将受到欢迎!