views:

1820

answers:

4

I have a ListView inside another ListView, and I'd like to hide a table column in the inner ListView whenever a particular parameter is passed. Given the setup below, how would I hide the ID column (both the header and the data) if the URL contains "...?id=no"?

<asp:ListView ID="ProcedureListView" runat="server">
    <LayoutTemplate>
        <asp:PlaceHolder ID="itemPlaceHolder" runat="server" />
    </LayoutTemplate>
    <ItemTemplate>
        <h4>
            <%#Eval("PROCEDURE_CODE") %>
        </h4>
        <asp:ListView ID="BenefitListView" runat="server" DataSource='<%#Eval("benefits") %>'>
            <LayoutTemplate>
                <table cellpadding="5" class="indent">
                    <tr class="tableHeader">
                        <td>
                            ID
                        </td>
                        <td>
                            Benefit
                        </td>
                    </tr>
                    <asp:PlaceHolder ID="itemPlaceHolder" runat="server" />
                </table>
            </LayoutTemplate>
            <ItemTemplate>
                <tr>
                    <td>
                        <%#Eval("benefit_id")%>
                    </td>
                    <td>
                        <%#Eval("benefit_name")%>
                    </td>
                </tr>
            </ItemTemplate>
        </asp:ListView>
    </ItemTemplate>
</asp:ListView>
+1  A: 

you could wrap them in a placeholder and then dynamically set the visibility of the placeholder to remove the column... (you will need two placeholders)

flesh
I'm having trouble with the dynamic part. If I wrap it in a placeholder named "myPH", then do ProcedureListView.FindControl("myPH"), it returns nothing.
gfrizzle
can you post your code behind? how are you databinding your nested ListView?
flesh
My bad - instead I should have been stepping through the items collection, since each inner control exists on each row (item).
gfrizzle
A: 

you could do the following:

<% if (Request.QueryString["id"] != "no") { %>
   <td>
     <%#Eval("benefit_id")%>
   </td>
<% } %>
   <td>
     <%#Eval("benefit_name")%>
   </td>

and do the same for the header.

edit: you are not clear but from a previous comment, if you want to do this in the code behind then you should place the id header and the id data in a label server control. then you can check the query string in the code behind, and on data bind you could set the visible property to false.

there are a few options here, it really depends on what you are most comfortable with.

Victor
Unfortunately, when you try to do that in the header, it produces this error: The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).
gfrizzle
Edit reply: The problem is that this is a nested ListView, so I can't access anything inside it from the code behind. If this were just a simple ListView, there would be a number of ways to do it, but they all seem to fall down in the nested ListView.
gfrizzle
you mean the designer throws an error right? so that only means you have to edit the code in the html view. it should run properly, though i havent tried it.
Victor
you might want to indicate what you have tried so far, so the answers are not redundant.
Victor
A: 

add a css class to the HTML tags and from code behind inject the css class onto the page like so

<td id='' class='hideMe'> 
    ID
</td>

code behind, in the pre-render event

if(id==123){
   // please refer to help file for exact syntax
   // but essentially you will be injecting
   // <style type='text/css'>
   // .hideMe{display:none;}
   // </style>
}

Alternatively, you can include the above css class in your stylesheet and only add it to the tags you want hidden based on the ID

rams
+1  A: 

if you are trying to do this from the code behind then you could do this:

On the onBind event for the outer ListView you would find the inner listview control, and then find the label you want and change the visible property to false. i answered this on your other question.

good luck!

Victor