Hiding and showing content in ASP.Net based on value

I am looking for an elegant solution to remove content from an ASP.Net page if no data is set. Let me explain this a little more.

I have several blocks of data on a page that contain some subsections with separate values ​​in them. If no data has been specified for one of the values, I need to hide it (so it doesn't take up space). Also, if none of the values ​​in the sub-series have been set, it should also be hidden. Finally, if none of the subsections appear inside the block / panel, then I need to hide everything.

Layout is implemented using nested panels / DIVs

<Panel id="block">
    <Panel id="sub1">
        <Panel id="value1-1">blah</Panel>
        <Panel id="value1-2">blah</Panel>
    </Panel>
    <Panel id="sub2">
        <Panel id="value2-1">blah</Panel>
        <Panel id="value2-2">blah</Panel>
    </Panel>
</panel>

      

I'm wondering if anyone has any decent ideas for implementing something like this without creating a bunch of nested If..Else statements and creating a bunch of custom controls. No matter what I implement, it needs to be robust enough to handle the markup changes without constantly manipulating the code.

I hope there is a way to do this through some simple markup changes (custom attribute) and a recursive function call on the PageLoad or PreRender page.

Any help is greatly appreciated.

EDIT:

Ok, so what makes this tricky is that there may be other controls in the subsectors that don't affect hiding and showing the controls. And each value bar could potentially have controls in it that don't affect the display or not. Example:

<Panel id="sub2">
    <Image id="someImage" src="img.png" />
    <Panel id="value2-1">
        <Label>blah</Label>
        <TextBox id="txtValue" />
    </Panel>
    <Panel id="value2-2">blah</Panel>
</Panel>

      

This is a more simplified example, but just around the corner, which I should be worried about.

0


source to share


5 answers


recursive traversal can be avoided if you can write functions to return true / false for each group, eg.



<Panel id="block" runat="server" visible="<%=IsBlockVisible%>">
    <Panel id="sub1" runat="server" visible="<%=IsSubVisible(1)%>">
        <Panel id="value1-1">blah</Panel>
        <Panel id="value1-2">blah</Panel>
    </Panel>
    <Panel id="sub2" runat="server" visible="<%=IsSubVisible(2)%>">
        <Panel id="value2-1">blah</Panel>
        <Panel id="value2-2">blah</Panel>
    </Panel>
</panel>

      

+1


source


Use recursion. Traverse the tree in a node in first order. Use the visible node property as needed based on control values. Don't visit children if parent is set to invisible



0


source


I think you are on the right track with recursion. But I would stay away from custom attributes - stick to standards. All you really need is to set the Visible property on each panel using a recursive method.

0


source


If you have a strict hierarchical block / sub / value and the data is coming from a database I suggest nested repeaters and a stored procedure that returns 3 result sets

0


source


I think we need to understand more about what you are trying to achieve in order to determine if it fits right.

0


source







All Articles