问题
I have a "Banned" field in the formview. If the value of this field in false I want to hide the label "From" and "To".
Item template control:
<asp:Label ID="BannedCheckBox" runat="server" Text='<%# DisplayTruthValue(Eval("Banned").ToString())%>' />
<asp:Label ID="BannedFromLabel" runat="server" Text='<%# "From: " + Eval("BannedFrom")%>' />
<asp:Label ID="BannedToLabel" runat="server" Text='<%# "To: " + Eval("BannedTo")%>' />
Code behind:
Protected Sub FrmViewPatron_DataBound(sender As Object, e As EventArgs) Handles FrmViewPatron.DataBound
Dim blnBan As String = DirectCast(FrmViewPatron.FindControl("BannedCheckBox"), Label).Text
If blnBan = "False" Then
End If
回答1:
You are already databinding the label so why not set the Visible property the same way. Add Visible='<%# (Eval("Banned") == "True") %>'
to both labels, or just Eval("Banned")
if it is a boolean field.
回答2:
Try
If blnBan = "False" Then
FrmViewPatron.FindControl("BannedFromLabel").Visible = False
FrmViewPatron.FindControl("BannedToLabel").Visible = False
End if
来源:https://stackoverflow.com/questions/21710259/hide-label-in-formview-asp-net