Collapsible panel in ASP.net

Ok, I think I'm just making a stupid mistake here, but I want to create a Control (derived from System.Web.UI.Control) that is collapsible using the good ol 'ASP.net ViewState / PostBack model.

I have an ImageButton in my class, which I initialize in the OnInit () event:

    private ImageButton _collapseImage;
    protected override void OnInit(EventArgs e)
    {
        if (_collapseImage == null)
        {
            _collapseImage = new ImageButton();
            _collapseImage.Click += CollapseImageClick;
        }
        _collapseImage.ImageUrl = string.Format("/images/{0}", IsCollapsed ? "plus.gif" : "minus.gif");
        _collapseImage.Width = 16;
        _collapseImage.Height = 16;
    }

      

IsCollapsed is boolean and CollapseImageClick just toggles it:

    private void CollapseImageClick(object sender, ImageClickEventArgs e)
    {
        IsCollapsed = !IsCollapsed;
    }

      

Then My CreateChildControls checks this parameter:

 protected override void CreateChildControls()
    {
        Panel pnl = new Panel();

        pnl.Controls.Add(_collapseImage);
        if(!IsCollapsed)
        {
            // Add some more Controls
        }
        Controls.Add(pnl);
    }

      

Unfortunately it doesn't work. I click on the ImageButton, the page does it with a postback, but then it doesn't change its state - if it was expanded before, it still expands after.

In the constructor, I set EnableViewState = true;

Any hints what am I missing to save these changes?

0


a source to share


2 answers


Are you actually keeping your pane state (collapsed boolean) in the viewport?

ViewState("collapsed") = Collapsed

      



Any property / variable is not automatically saved in the view, you have to tell what to do.

+1


a source


If the ViewState doesn't work for you, you can always try to save it as a session.



+1


a source







All Articles