2

How should I hide the Attach div if the ListView2 is Empty.

<div id="Attach" class="AttachHead col-lg-12">
                    <h2>
                        Attachments</h2>
                </div>

<asp:ListView ID="ListView2" runat="server">
                    <ItemTemplate>
                        <a href='<%# String.Format("Download.aspx?Title={0}",Container.DataItem) %>' target="_blank">
                            <asp:Label ID="attach" runat="Server" Text='<%#Container.DataItem%>' CssClass="col-lg-4" />
                        </a>
                    </ItemTemplate>
                </asp:ListView>
4
  • I am assuming this is in a repeater correct ? Commented Apr 13, 2014 at 16:12
  • listview contains a list of items. If its empty i want to hide the div Commented Apr 13, 2014 at 16:20
  • Can't you check in the code behind if list is empty, visible=false. Or are you asking just particular index of a list ? Commented Apr 13, 2014 at 16:40
  • yes i can.. but i only want not to display the Attach div if the listview is emty Commented Apr 13, 2014 at 16:47

2 Answers 2

3

Add runat="server" attribute to the div first.

<div runat="server" id="Attach" class="AttachHead col-lg-12"> <h2> Attachments</h2></div>

Then register the databound event of listview.

<asp:ListView ID="ListView2" runat="server" ondatabound="ListView2_DataBound">
                    <ItemTemplate>
                    <a href='<%# String.Format("Download.aspx?Title=    {0}",Container.DataItem) %>' target="_blank">
                        <asp:Label ID="attach" runat="Server" Text='<%#Container.DataItem%>' CssClass="col-lg-4" />
                    </a>
                </ItemTemplate>
            </asp:ListView>

and check with ListView2.items.count inside the databound method in codebehind like:

protected void ListView2_DataBound(object sender, EventArgs e)
{
  if (ListView2.Items.Count > 0)
        Attach.Visible = true;
    else
        Attach.Visible = false;
}
Sign up to request clarification or add additional context in comments.

Comments

0

If you do not want to alter your markup and do it in client-side, try this

$(document).ready(function () {
    $("#Attach").css("display", $(".col-lg-4").length ? "block" : "none");
});

The above code assumes that there is a PostBack on DataBind of the ListView. If you are using the unholy UpdatePanel change the DOM Ready event to pageLoad function like this

function pageLoad() {
    $("#Attach").css("display", $(".col-lg-4").length ? "block" : "none");
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.