Hide TemplateField

  • Thread starter Thread starter David C
  • Start date Start date
D

David C

I have a DetailsView using TemplateFields and would like to know if there is
a way to hide them in code. I have a Databound event where I am doing other
things and would like to hide some TemplateFields based on whether a
checkbox is checked. Below is a sample of 2 TemplateFields in my page and
below that is my Databound code where I am hiding controls. Any help is
appreciated.

David

<asp:TemplateField HeaderText="Retail business, shopping center"
SortExpression="">
<EditItemTemplate>
<asp:CheckBox ID="ckRetailBusShopping" runat="server"
Checked='<%# Bind("RetailBusShopping") %>' />
</EditItemTemplate>
<ItemTemplate>
<asp:CheckBox ID="ckRetailBusShopping" runat="server"
Checked='<%# Bind("RetailBusShopping") %>' Enabled="false" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Distance from Retail (meters)"
SortExpression="">
<EditItemTemplate>
<asp:TextBox ID="txtMetersFromRetail" runat="server"
Text='<%# Bind("MetersFromRetail") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="LblMetersFromRetail" runat="server"
Text='<%# Bind("MetersFromRetail") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>

In the code page I have sub below.

Protected Sub dvPropertyAndBusiness_DataBound(ByVal sender As Object,
ByVal e As System.EventArgs) Handles dvPropertyAndBusiness.DataBound
Dim ck As CheckBox =
dvPropertyAndBusiness.FindControl("ckRetailBusShopping")
Dim tx As TextBox
Dim lbl As Label

Select Case dvPropertyAndBusiness.CurrentMode
Case DetailsViewMode.ReadOnly
tx =
dvPropertyAndBusiness.FindControl("txtMetersFromRetail")
lbl =
dvPropertyAndBusiness.FindControl("LblMetersFromRetail")
If ck.Checked Then
tx.Visible = True
lbl.Visible = True
Else
tx.Visible = False
lbl.Visible = False
End If

Case DetailsViewMode.Edit
tx =
dvPropertyAndBusiness.FindControl("txtMetersFromRetail")
lbl =
dvPropertyAndBusiness.FindControl("LblMetersFromRetail")
If ck.Checked Then
tx.Visible = True
lbl.Visible = True
Else
tx.Visible = False
lbl.Visible = False
End If

Case Else

End Select

End Sub
 
I have a DetailsView using TemplateFields and would like to know if thereis
a way to hide them in code.  I have a Databound event where I am doing other
things and would like to hide some TemplateFields based on whether a
checkbox is checked.  Below is a sample of 2 TemplateFields in my page and
below that is my Databound code where I am hiding controls.  Any help is
appreciated.

David

            <asp:TemplateField HeaderText="Retail business,shopping center"
SortExpression="">
                <EditItemTemplate>
                    <asp:CheckBox ID="ckRetailBusShopping" runat="server"
Checked='<%# Bind("RetailBusShopping") %>' />
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:CheckBox ID="ckRetailBusShopping" runat="server"
Checked='<%# Bind("RetailBusShopping") %>' Enabled="false" />
                </ItemTemplate>
            </asp:TemplateField>
             <asp:TemplateField HeaderText="Distance fromRetail (meters)"
SortExpression="">
                <EditItemTemplate>
                    <asp:TextBox ID="txtMetersFromRetail" runat="server"
Text='<%# Bind("MetersFromRetail") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="LblMetersFromRetail" runat="server"
Text='<%# Bind("MetersFromRetail") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>

In the code page I have sub below.

    Protected Sub dvPropertyAndBusiness_DataBound(ByVal sender As Object,
ByVal e As System.EventArgs) Handles dvPropertyAndBusiness.DataBound
        Dim ck As CheckBox =
dvPropertyAndBusiness.FindControl("ckRetailBusShopping")
        Dim tx As TextBox
        Dim lbl As Label

        Select Case dvPropertyAndBusiness.CurrentMode
            Case DetailsViewMode.ReadOnly
                tx =
dvPropertyAndBusiness.FindControl("txtMetersFromRetail")
                lbl =
dvPropertyAndBusiness.FindControl("LblMetersFromRetail")
                If ck.Checked Then
                    tx.Visible = True
                    lbl.Visible = True
                Else
                    tx.Visible = False
                    lbl.Visible = False
                End If

            Case DetailsViewMode.Edit
                tx =
dvPropertyAndBusiness.FindControl("txtMetersFromRetail")
                lbl =
dvPropertyAndBusiness.FindControl("LblMetersFromRetail")
                If ck.Checked Then
                    tx.Visible = True
                    lbl.Visible = True
                Else
                    tx.Visible = False
                    lbl.Visible = False
                End If

            Case Else

        End Select

    End Sub

Hi David

It may be helpful to mention that the 'Visible' property of any
control in a template can be DataBound to boolean field values just as
the 'Checked' property of a checkbox control can. If the conditions
for hiding or showing are a little more complex then you can build
expressions in the language of the page (VB.NET in this case)
including the option of calling functions coded in script or in the
page's code file (give the function a public attribute).

HTH
 
I have a DetailsView using TemplateFields and would like to know if there
is
a way to hide them in code. I have a Databound event where I am doing
other
things and would like to hide some TemplateFields based on whether a
checkbox is checked. Below is a sample of 2 TemplateFields in my page and
below that is my Databound code where I am hiding controls. Any help is
appreciated.

David

<asp:TemplateField HeaderText="Retail business, shopping center"
SortExpression="">
<EditItemTemplate>
<asp:CheckBox ID="ckRetailBusShopping" runat="server"
Checked='<%# Bind("RetailBusShopping") %>' />
</EditItemTemplate>
<ItemTemplate>
<asp:CheckBox ID="ckRetailBusShopping" runat="server"
Checked='<%# Bind("RetailBusShopping") %>' Enabled="false" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Distance from Retail (meters)"
SortExpression="">
<EditItemTemplate>
<asp:TextBox ID="txtMetersFromRetail" runat="server"
Text='<%# Bind("MetersFromRetail") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="LblMetersFromRetail" runat="server"
Text='<%# Bind("MetersFromRetail") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>

In the code page I have sub below.

Protected Sub dvPropertyAndBusiness_DataBound(ByVal sender As Object,
ByVal e As System.EventArgs) Handles dvPropertyAndBusiness.DataBound
Dim ck As CheckBox =
dvPropertyAndBusiness.FindControl("ckRetailBusShopping")
Dim tx As TextBox
Dim lbl As Label

Select Case dvPropertyAndBusiness.CurrentMode
Case DetailsViewMode.ReadOnly
tx =
dvPropertyAndBusiness.FindControl("txtMetersFromRetail")
lbl =
dvPropertyAndBusiness.FindControl("LblMetersFromRetail")
If ck.Checked Then
tx.Visible = True
lbl.Visible = True
Else
tx.Visible = False
lbl.Visible = False
End If

Case DetailsViewMode.Edit
tx =
dvPropertyAndBusiness.FindControl("txtMetersFromRetail")
lbl =
dvPropertyAndBusiness.FindControl("LblMetersFromRetail")
If ck.Checked Then
tx.Visible = True
lbl.Visible = True
Else
tx.Visible = False
lbl.Visible = False
End If

Case Else

End Select

End Sub

Hi David

It may be helpful to mention that the 'Visible' property of any
control in a template can be DataBound to boolean field values just as
the 'Checked' property of a checkbox control can. If the conditions
for hiding or showing are a little more complex then you can build
expressions in the language of the page (VB.NET in this case)
including the option of calling functions coded in script or in the
page's code file (give the function a public attribute).

HTH

Yes, that is helpful for the controls, but what about the entire (table row)
line of the DetailsView (TemplateField)? If I make the Visible property
false for the control, won't the row still show as a blank line? Thanks.

David
 
Back
Top