Handle asp panel hide/unhide

Hello

I'm want to have some sort of custom event handler for a standard asp.panel control.

I want some code to run when the panel visibility is set to true but there is no event handler for that? Can I create a custom one?

Many thanks!
badpilotAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

bitrefCommented:
You cannot capture such events. Alternatively you can:

(1) Create a Sub that sets Panel visibility to True and another Sub that sets it to False.
(2) Replace every line of code with a call to the equivalent Sub (to set visibility to True or False)
(3) Insert wahtever code you need in either Sub.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
badpilotAuthor Commented:
Thanks, I was trying to over complicate that, instead of call panel.visibility = true, create a sub for it which calls that  and then run what ever code after that.
0
käµfm³d 👽Commented:
I guess I don't type fast enough  = )

If you wanted to, you could inherit the already existing Panel class and add your own event:

using System;
using System.Web.UI.WebControls;

namespace WebApplication4
{
    public class MyPanel : Panel
    {
        /// <summary>
        /// Occurs when visibility of the panel changes.
        /// </summary>
        public event VisibilityChangedHandler VisibilityChanged;

        /// <summary>
        /// Handle event signature.
        /// </summary>
        public delegate void VisibilityChangedHandler(object sender, EventArgs e);

        /// <summary>
        /// Gets or sets a value that indicates whether a server control is rendered as UI on the page.
        /// </summary>
        /// <value></value>
        /// <returns>true if the control is visible on the page; otherwise false.
        /// </returns>
        public override bool Visible
        {
            get
            {
                OnVisibilityChanged(new EventArgs());

                return base.Visible;
            }
            set
            {
                OnVisibilityChanged(new EventArgs());

                base.Visible = value;
            }
        }

        /// <summary>
        /// Raises the <see cref="E:VisibilityChanged"/> event.
        /// </summary>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected virtual void OnVisibilityChanged(EventArgs e)
        {
            if (this.VisibilityChanged != null)
            {
                this.VisibilityChanged(this, e);
            }
        }
    }
}

Open in new window


Then, in your markup, you could do this:

<cc1:MyPanel ID="MyPanel1" runat="server" OnVisibilityChanged="MyVisibilityHandler">
</cc1:MyPanel>

Open in new window


then, create a handler for it:

public partial class _Default : System.Web.UI.Page
{
    private void MyVisibilityHandler(object sender, EventArgs e)
    {
        // do something
    }
}

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.