Thursday, February 17, 2011

Hiding and showing content in ASP.Net based on value

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

I have some blocks of data on a page that contain some sub-sections with individual values in them. If no data has been set for one of the values I need to hide it (so it does not take up space). Also, if none of the values within a sub-section have been set, it needs to be hidden as well. Finally if none of the sub-sections are visible within the block/panel, then I need to hide the entire thing.

The 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 am wondering if anyone has any decent ideas on implementing something like this without writing a bunch of nested If..Else statements, and without creating a bunch of custom controls. Whatever I implement needs to be robust enough to handle changes in the markup without constantly manipulating the codebehind.

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

Any help is greatly appreciated.

EDIT:

Ok so what makes this tricky is that there might be other controls inside the sub-sections that do not factor into the hiding and showing of the controls. And each value panel could potentially have controls in it that do not factor into whether or not it is shown. 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 an over simplified example, but not far off from what I have to worry about.

From stackoverflow
  • Use recursion. Traverse the control tree in node first order. Use the visible property of the node as appropriate based on control values. Don't visit children if the parent is set to not visible

  • I think you're on the right track with recursion. But I'd stay away from custom attributes - stick to standards. All you really need is to set the Visible property on each panel via your recursive method.

  • it may be possible to avoid recursive traversal if you can write functions to return true/false for each group, e.g.

    <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>
    
    Dillie-O : I did the same thing on a project a while back and it worked great. It makes it really easy to implement and your code behind function becomes rather simple as well.
  • If you have a strict hierarchy block/sub/value and the data comes from a database, I suggest nested Repeaters and a stored procedure which returns 3 result sets

  • I think we need to understand more about what you are trying to achieve in order to determine if it's the right approach in the first place.

0 comments:

Post a Comment