0
votes

I have two Webpartzones on my page in given below manner:

<asp:WebPartZone ID="Left" WebPartVerbRenderMode="TitleBar" runat="server"  CssClass="PartZone" > 
     <MinimizeVerb  Text="Minimize" Enabled="true" />
     <RestoreVerb  Text="Restore" Enabled="true" />
     <CloseVerb Text="Close" Enabled="true" />
     <ZoneTemplate>
        <REAMS:TotoalEnergyGenerated runat="server" ID="TotEneryGen" />
        <REAMS:NoOfSystemsOnline runat="server" ID="OnlineSys" />
        <REAMS:WeatherInfo Title=" " ID="WeatherInfoExec" runat="server" />
        <REAMS:AssetMap ID="AssetMapExec" runat="server" />
     </ZoneTemplate>
</asp:WebPartZone>

<asp:WebPartZone ID="Bottom" WebPartVerbRenderMode="TitleBar" runat="server"  CssClass="PartZone" > 
     <MinimizeVerb  Text="Minimize" Enabled="true" />
     <RestoreVerb  Text="Restore" Enabled="true" />
     <CloseVerb Text="Close" Enabled="true" />
     <ZoneTemplate>
        <REAMS:ProjectSummary ID="Project" runat="server" />
     </ZoneTemplate>
</asp:WebPartZone>

as it is evident, webpartzone left has four gadgets(controls) in it and webpartzone bottom has one. So for the first time, for any given user, left shows four and bottom shows one gadget. Now, suppose user closes one of the 4 gadgets and now left has just three gadgets. Next time he comes and finds the page layout as per his previous modifications.

I have these gadgets as UserControls. These usercontrols have public methods to hit the database and perform some logic and get data and I call these public methods on the webpart page(and not on the uc itself). i.e. ucGadget1.LoadData();

here's my problem: I want to load only those gadgets(UCs) which are not closed by the user i.e. which are visible when the webpart page loads. Am unable to find difference between a webpart kept on the page but not visible and a webpart kept on the page and visible. Because all the properties of these webparts are the ones that is being written on the designer.I need those which get rendered at the runtime(and through the ASP.NET Personalization Tables)

(please consider gadget = webpart)

3

3 Answers

1
votes

here's my problem: I want to load only those gadgets(UCs) which are not closed by the user i.e. which are visible when the webpart page loads. Am unable to find difference between a webpart kept on the page but not visible and a webpart kept on the page and visible

When user closes the window or moves to another page, you can check the visibility of the control using following code...

var ID = document.getElementById('ID of the control');

This code will be written in OnBeforeUnLoad Event


Reference

OnBeforeUnload event of Javascript


In case this ID is returning you the memory that means control is present otherwise it is hidden and the control is not rendered as HTML.


Now you can call the server method using AJAX/JQuery. This method will update the control visibility Bit(High/Low) in database or where ever you want....

Hope this will help you...


Query

By closing the control means setting the

  1. Style display - none
  2. visibility = false
  3. not loading the control

?

0
votes

User set properties of any webpart on a page are available inside OnLoadComplete. so I did this

 protected override void OnLoadComplete(EventArgs e)
        {
            base.OnLoadComplete(e);

            foreach (WebPart webprt in WebPartManagerDashBoard.WebParts)
            {

                if (!webprt.IsClosed)
                {
                   //Action
                }

            }
        }

Now custom logic can be implemented instead of //Action up there. Few WebPartManager events can also be handled as per usage.

 WebPartManagerDashBoard.WebPartClosing += new WebPartCancelEventHandler(WebPartManagerDashBoard_WebPartClosing);
0
votes

Here's what I do:

in the 'host' page with the WebPartManager in it, override the OnPreRender() event and create a Session variable unique to each UC/Wp. I prefix mine with UI_ .

So,

protected override void OnPreRender( EventArgs e )
{
    base.OnPreRender( e );

   WebPartZoneCollection Zones = WebPartManager1.Zones;   
   foreach ( WebPartZone z in Zones )
    {

        foreach ( WebPart wp in z.WebParts )
        {
            Control cntrl;

            // get the Control / Webpart
            cntrl = wp.Controls[0];

            // Initialise a Session variable with the ChromeState of the control.
            Session["UI_" + cntrl.ClientID] = wp.ChromeState;

        }
    }
}

This has to be done by the WebPartManager.

Now, within each UserControl/Webpart, its overridden OnPreRender can pick up the ChromeState from the uniquely named Session var pertaining to the usercontrol's ClientID.

protected override void OnPreRender( EventArgs e )
{
    base.OnPreRender( e );
    PartChromeState pcs = PartChromeState.Minimized;

    try
    {
        pcs = (PartChromeState)Session["UI_" + this.ClientID];
    }
    catch
    {
        Session["UI_" + this.ClientID] = pcs;
    }

    bool canRefresh = pcs == PartChromeState.Normal;

    if ( canRefresh )
        getData();   // do work only if WP/UC is visible and not 'minimised' or Closed.

}

The reason for the try/catch is on returning to the page with UC/Wp's 'closed' there will be no Session var set up by the WebPartManager so the Session will be null. There may be a way of having the WebPartManager find out what WP/UCs have been Closed but I ran out of time digging around!

Anyway, the result of this is any Webpart/UC that has been Closed or Minimised will not hit the database (or do whatever) resulting in a much snappier Webpart 'desk'.

I'm considering bunging a more complex object into the Session var along with its ChromeState (so as to cache a chart, for example etc), but the key is to have the WebPartManager configure state as it alone 'knows' the state the UC/Wps are in!

Hope this helps

C