Search code examples
c#asp.netlistviewvisible

Hide a ListView on a button click


I my ASP.NET 4.5 WebForms app, I have a . Above it I have a LinkButton to Show/Hide the LinkView. But somw how the ListView's Visible state is not changed only, it's always visible. Here's my code :

      <asp:LinkButton runat="server" Visible="true" ID="collapseFloorList" Text="Hide" OnClick="collapseFloorList_Click"></asp:LinkButton>

    <asp:Panel ID="floorPanel" runat="server" >

    <asp:ListView runat="server" ID="floorList" 
         ItemType="VincitoreCRMApplication.Models.FloorPattern"
         UpdateMethod="floorList_UpdateItem" DeleteMethod="floorList_DeleteItem"
         SelectMethod="floorList_GetData" DataKeyNames="FloorPatternId" 
         Visible='<%# ShowFloorList %>' >

In the Code Behind, I have a Property in PAge named ShowFloorList :

    public bool ShowFloorList { get; set; }
    protected void Page_Init(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ShowFloorList = true;
        }

    }

    protected void collapseFloorList_Click(object sender, EventArgs e)
    {
        System.Diagnostics.Debug.WriteLine("COLLAPSE FLOOR BTN Click Floor List State = " + floorList.Visible + "  BTN TEXT = " + collapseFloorList.Text );

        if (collapseFloorList.Text == "Hide") // Requesting to Hide i.e. Visible to make false
        {
            System.Diagnostics.Debug.WriteLine("INSIDE HIDE");
            ShowFloorList = false;
            System.Diagnostics.Debug.WriteLine("SHOWFLOOR LIST = " + ShowFloorList);
        }
        else
            ShowFloorList = true;
        /*
        if (ShowFloorList == false)
        {
            collapseFloorList.Text = "Show";
            ShowFloorList = false;
            //floorPanel.Visible = false;
            //floorList.Visible = false;
        }
        {
            collapseFloorList.Text = "Hide";
            ShowFloorList = true;
            floorPanel.Visible = true;
            //floorList.Visible = true;
        } */
    }

LOGS :

 COLLAPSE FLOOR BTN Click Floor List State = True  BTN TEXT = Hide
 INSIDE HIDE
 SHOWFLOOR LIST = False

I tried making the floorLsit directly visible to false, adding it in a panel & making panel visible, and now alos thru property but Nothing works.

Can you tell me why am not able to hide the Listview ?? Any help is highly appreciated.

Thanks


Solution

  • It could be that Link button is actually a link, then !IsPostBack is always false, have you tried changing the LinkButton into asp Button?

    EDIT

    I've created a sample project for you, open this project NOT as a Project but as a WebSite
    Sample scrollTo and Toggle in jquery
    Please don't hesitate if you have questions.