ASP - how do I handle when a particular child ASCX is reloaded while visible?

28 Views Asked by At

I have an ASPX page that includes a number of child ASCXs, one of which is to be displayed at any particular time. This is being handled through an ASP Wizard. I want to be able to handle a postback on a particular ASCX from within that ASCX's code. Note that the ASCX's Page_Load is triggered when any of the parent's child ASCXs are loaded.

Checking this.Visible in Page_Load does not work, as it is true when I switch from this ASCX to another one, and false when I switch from a different ASCX to this one.

How do I detect if an ASCX is still "active" after a postback, within its own code?

1

There are 1 best solutions below

0
Albert D. Kallal On

Hum, we will need more details.

Remember, the uc page load events trigger BEFORE your main page code. What this means is that if in main page code, you set visible = false for a uc, then the page load event of the uc will STILL show true!.

However, if you post-back again, it WILL show false, since now your main page button code ran AFTER the page load event of the UC!!

At this point, a post-back by the UC, or a post-back by the page that hosts the UC - they BOTH will show visible = false.

In fact, we can show this effect without even using a UC!!!

Say, I drop in a simple button, and h3 tag.

My button will toggle (hide/show) the h3 tag.

However, note that since page load triggers BEFORE the button code, then what I see/choose/look at/find/observe in page load is the stats of the "thing" we are ABOUT to change - it has not been changed yet!!!

So, this:

        <asp:Button ID="Button1" runat="server" Text="Toggle h3"
            OnClick="Button1_Click"
            />
        <br />

        <h3 id="myh3" runat="server">This is my h3 text</h3>

And then code behind:

    protected void Page_Load(object sender, EventArgs e)
    {
        Debug.Print($"Page load event, h3 visible = {myh3.Visible.ToString()});
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        myh3.Visible = !myh3.Visible;
    }

So, now when we run, we see this:

enter image description here

So, if you toggle, hide, show ANY uc on the page?

Well, be it the page that hosts the UC's, or even the UC them seleves?

They do see, do each retain their OWN visbile status via viewstate.

It just that in most cases, a simple check of "visbile" in the page load event tends to be too late WHEN code behind is about to change/hide that control or element.

And the reason of course is that page load event triggers ALWAYS before any button code behind - that includes the uc's also!!!

So, even in above, when I click on the button to hide the h3 tag, note how the debug.print of the load event STILL shows true when I click on that button.

You have 100% the SAME issue with your uc's. Their page load events are triggering BEFORE your code behind button. (and it don't matter if that code behind is on the main hosting page, or the code behind is for the single UC code - the page load event triggers before.

Now, if you have say hidden a UC, then BOTH code behind (hosting page), or code behind (uc code behind) can most certainly check/test/enjoy and control visible properties of elements, and they will work, and work as "per each" control.

About the only why this whole system would fail is if you have clientidmode="static" for those controls, and you CAN NOT do that for UC's, since then if you drop more then one on a page then you not only have duplicated elements on the one page, you not be able to distinguish between multiple user controls. this OFTEN can be a issue if you using client side JavaScript code against the UC, or even elements of the UC.

But, "this.Somecontrol" in code behind for a uc should work, and the visible properties are per control, and the code behind for the UC will and should reflect this. So, each UC should reflect its own state here.