locked
Visibility of DynamicField RRS feed

  • Question

  • User-461351699 posted

    I am having a heck of a time discovering how to manipulate the visible attribute of the DynamicField object within a DetailsView. I use the declarative attribute "Visible" to hide the field and of course when the page is displayed the entire detail row is not visible as I was hoping. What I need is to manipulate the detail row visibility with code-behind for the page. I will also use code-behind in a field template to manipulate the same. What I am considering is abandoning the declarative approach and use code-behind exclusively to set visibility of the rows, but if I understood where to access the correct visibile property with code-behind, the declarative approach would be preferred I think. Is it possible to access the same visible attribute using the syntax DetailsView1.FindFieldTemplate("myFld") and navigating the control hierarchy?

    Custom Page Markup:

    ...
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
                <asp:DetailsView ID="DetailsView1" runat="server" DataSourceID="DetailsDataSource" DefaultMode="Insert"
                    AutoGenerateInsertButton="False" CssClass="detailstable"
                    FieldHeaderStyle-CssClass="bold" AutoGenerateRows="false">
                    <Fields>
                        <asp:DynamicField DataField="myFld" Visible="false" UIHint="myFldTemplate" />
                    </Fields>
                </asp:DetailsView>
    ...
            </ContentTemplate>
        </asp:UpdatePanel>

     Update:

    I investigated abandoning the declarative approach entirely relying only on code-behind to manipulate visibility. This was successful, but I am still curious about the declarative approach and how to access this attribute from code-behind. The code to manipulate the detail row visibility uses DetailsView1.FindFieldTemplate("myFld") and traverses control parents to find the DetailsViewRow. The code manipulates the visibility of the detail row from the Load event of the page and also the OnTextChanged event of another control on the same page.

    Thursday, April 25, 2013 12:44 PM

Answers

  • User281315223 posted

    The Visible attribute is actually a bit misleading.

    When it is set to "False" the object itself will not be rendered at all and thus will not be accessible through client-side or server-side code. If you want to effectively "hide" this object in hopes of displaying it later or accessing properties of it, your best bet would be to hide it through CSS.

    You could use the CssClass property and create a simple property that would hide your element and then remove the class your through your server-side code later on : 

    <!-- Your Style -->
    <style type='text/css'>
        .hidden { display: none; }
    </script>
    
    <!-- Updated Field -->
    <asp:DynamicField DataField="myFld" UIHint="myFldTemplate" CssClass="hidden" />
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 25, 2013 1:27 PM

All replies

  • User281315223 posted

    The Visible attribute is actually a bit misleading.

    When it is set to "False" the object itself will not be rendered at all and thus will not be accessible through client-side or server-side code. If you want to effectively "hide" this object in hopes of displaying it later or accessing properties of it, your best bet would be to hide it through CSS.

    You could use the CssClass property and create a simple property that would hide your element and then remove the class your through your server-side code later on : 

    <!-- Your Style -->
    <style type='text/css'>
        .hidden { display: none; }
    </script>
    
    <!-- Updated Field -->
    <asp:DynamicField DataField="myFld" UIHint="myFldTemplate" CssClass="hidden" />
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 25, 2013 1:27 PM
  • User-461351699 posted

    While changing to CSS style to manipulate the visibility is certainly an option, I am not sure that it would resolve the code-behind accessibility. My real issue is finding the control that is being manipulated by the declared attribute (CssClass or Visible). The field did not become inaccessible in code-behind when the "Visible" attribute was "false", but any manipulation of the field and parents of the field with code-behind would not make the field visible on the page.

    I appreciate your reply, but for my vote as an answer I need the code-behind that will manipulate the visibility (CssClass or Visible).

    Thursday, April 25, 2013 2:15 PM