locked
Setting a pannel visible inside of an ItemTemplate RRS feed

  • Question

  • User977421240 posted

    I have a asp:FormView that I am formatting the data in the some table layouts and also using panels to hide/display update fields based on conditions. That rather is the intent.  Given users are given access to submit changes to specific fields based on their role defined in the ACL maintained for the web site.  However as I began to enter the visibility controls in the code behind page.  I get the impression that being that the panels are nested that the code behind page can't see the visibility toggle control  and that is why it tells me that 'it doesn't exist in the current context'.  Is there a work around to this or am I going to have to abandon my asp:formview to do my nesting so that I can make these panels visible from the code behind.

    Here are some sample fragments from the aspx side and the code behind This is in C# backend:

    <asp:TableRow ID="AssetTypeRow" BorderStyle="Solid" BorderColor="Brown" BackColor="Brown" ForeColor="White">
        <asp:TableCell HorizontalAlign="Right">Asset Type:&nbsp;</asp:TableCell>
        <asp:TableCell BorderStyle="Solid" BackColor="White" ForeColor="Brown">
           <asp:Label Text='<%# Bind("AssetType") %>' runat="server" ID="AssetTypeLabel" />
           <asp:Panel runat="server" ID="AssetTypePnl" Visible="false">
               <asp:TextBox ID="AssetTypeTbx" runat="server"></asp:TextBox></asp:Panel>
        </asp:TableCell>
    </asp:TableRow>

    C# code behind

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["SessionUType"].ToString() == "ITSec")
        {
            AssetTypePnl.Visible = true;
        }
    }

    Suggestions?

    ****************************************************

    Updated 1:40 CST US 8/5/16

    Because of the double nesting I opted to take a bit of a different approach and revised as follows which should be easier to deal with...

    ASPX CODE:

    <asp:FormView Width="100%" ID="ChangeFormFV" DefaultMode="Insert" runat="server" DataKeyNames="CAssetID" DataSourceID="UpdateSqlDataSource">
        <InsertItemTemplate>
            <asp:Panel runat="server" ID="AssetTypePnl" Visible="false">
                AssetType:
                <asp:TextBox Text='<%# Bind("AssetType") %>' runat="server" ID="AssetTypeTextBox" /><br />
            </asp:Panel>
        </InsertItemTemplate>
    </asp:FormView>

    C# Code page fragment:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["SessionUType"].ToString() == "ITSec")
        {
            AssetTypePnl.Visible = true";
        }
    

    Now ideally what I want to do is use FindControl to dig into ChangeFormFV to toggle the Visibility from 'false' to 'true' so that the panel is displayed. I have been messing around with this in several different approaches but I can't seem to get the syntax right. I think is should look something like ChangeFormFV.FindControl("AssetTypePnl.Visble") = "true";  but here intelligence doesn't understand what ChangeFormFV is.

    Think I'm getting closer but still off!

    Thursday, August 4, 2016 2:41 PM

Answers

  • User475983607 posted

    The framework does not render controls where the visibility property is set to false. Therefore, the inputs are not sent from the client to the server on a post back.  However, visibility = false controls are always available to the code behind.

    You must drill into the parent control to find the nested child controls.

    https://msdn.microsoft.com/en-us/library/486wc64h(v=vs.110).aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 4, 2016 3:38 PM