Unpossible
Unpossible

Reputation: 10697

Hiding a table row (the ItemPlaceholderID row) in a ListView

I have a table row that holds my paging controls in a ListView as follows (partial layout):

<asp:ListView ID="lvOrderItems" runat="server" 
    DataSourceID="odsShoppingCart"
    DataKeyNames="ProductNumber"
    ItemPlaceholderID="lvItemContainer">
    <LayoutTemplate>
        <table id="lvCart" runat="server">
            <tr id="lvHeader" runat="server">
            ...
            </tr>
            <tr id="lvItemContainer" runat="server"></tr>
            <tr id="lvPaging" runat="server">
            ...
            </tr>
        </table>
    </LayoutTemplate>

In my code-behind, I handle the DataBound event as follows, and I am planning on hiding the entire lvItemContainer row conditionally (for now, I am just trying to hide the row itself without conditions):

Protected Sub lvOrderItems_DataBound(ByVal sender As Object, ByVal e As EventArgs) Handles lvOrderItems.DataBound
    Dim lvItemContainer As HtmlTableRow = CType(lvOrderItems.FindControl("lvItemContainer"), HtmlTableRow)
    If Not lvItemContainer Is Nothing Then
        Response.Write("hit1")
        lvItemContainer.Visible = False
    End If

    Dim lvPaging As HtmlTableRow = CType(lvOrderItems.FindControl("lvPaging"), HtmlTableRow)
    If Not lvPaging Is Nothing Then
        Response.Write("hit2")
        lvPaging.Visible = False
    End If
End Sub

So when this runs on DataBound, hit1 is never fired, but hit2 is... any ideas what is happening here?

Upvotes: 1

Views: 7294

Answers (2)

Unpossible
Unpossible

Reputation: 10697

Instead of attempting to (unsuccessfully) adjust the visibility of the ItemPlaceholderID container, I've set the visibility of the individual rows in the container.

Upvotes: 0

James Johnson
James Johnson

Reputation: 46047

HTML purists don't like it, but this approach works:

In the OnLayoutCreated event, try one of the following approaches:

Take the runat="server" out of the table and child rows, and do this:

<asp:Panel ID="pnlItemContainer" runat="server">
    <tr id="lvItemContainer"></tr>
</asp:Panel>

pnlItemContainer.Visible = false;

Or you can do this:

<tr id="lvItemContainer" runat="server"></tr>

EDIT: Embedded style element because setting visible to false does not work in the layout template.

lvItemContainer.Style["display"] = "none";

I'm not sure how it will work with a layout template, but it's worked for me in other situations.

Upvotes: 3

Related Questions