Question

I'm writing a dashboard that is set to run by itself on a TV screen. I am fairly new to asp.net (normally do php) so simply I have 3 place holders which I would like to roll through on a asp timer. I am trying to do this through the visibility attribute on a placeholder. I'm currently having trouble getting it to actually roll through on the timer I think I need some way to pass back the values to the code behind but I am unsure of how to do this. Any help would be appreciated.

Note: The onpage_load works fine (showing panel1) and the first tick_event also works (hides panel1 and shows panel2) but then I get stuck at panel 2 and the page does not move from there.

Page

<asp:Content ID="MainContent" runat="server" ContentPlaceHolderID="MainContent">
    <div class='container_12' id='content'>
    <asp:UpdatePanel ID="UPmaincontent" runat="server" visible="true" UpdateMode="Conditional">
        <ContentTemplate>   
            <asp:PlaceHolder ID="Panel1" visible="false" runat="server"> 
                    <div class='grid_12' id='Emailbreakdown'>
                    </div>
            </asp:PlaceHolder>
            <asp:PlaceHolder ID="Panel2" visible="false" runat="server">   
                    <div class='grid_12' id='WObreakdown'>
                    </div>
            </asp:PlaceHolder>
            <asp:PlaceHolder ID="Panel3" visible="false" runat="server">
                    <div class='grid_12' id='IRbreakdown'>
                    </div>
            </asp:PlaceHolder>
         </ContentTemplate>
    </asp:UpdatePanel>  
    <div class='clear'>&nbsp;</div>   
<asp:Timer ID="Tmaincontent" runat="server" Interval="10000" OnTick="Tmaincontent_Tick"></asp:Timer>
</div>

Code Behind

protected void Page_Load(object sender, EventArgs e)
{
    Panel1.Visible = true;
    Panel2.Visible = false;
    Panel3.Visible = false;
}
public void Tmaincontent_Tick(object sender, EventArgs e)
{
    if (Panel1.Visible == true)
    {
        Panel1.Visible = false;
        Panel2.Visible = true;
        Panel3.Visible = false;
        UPmaincontent.Update();
    }
    else if (Panel2.Visible == true)
    {
        Panel1.Visible = false;
        Panel2.Visible = false;
        Panel3.Visible = true;
        UPmaincontent.Update();
    }
    else
    {
        Panel1.Visible = true;
        Panel2.Visible = false;
        Panel3.Visible = false;
        UPmaincontent.Update();
    }
}
Was it helpful?

Solution

You're almost there. The timer code and everything works properly, but remember that it's going to cause a PostBack event each time the timer fires and that causes the Page_OnLoad method to fire before the Tmaincontent_Tick thus resetting your panels each time. Basically, you're stuck in a loop where Panel1 is always set to Visible and then Panel2 is set to Visible, so it can never get to three.

Here's the simple fix:

protected void Page_Load(object sender, EventArgs e)
{
    if (Page.IsPostBack)
        return;

    Panel1.Visible = true;
    Panel2.Visible = false;
    Panel3.Visible = false;
}


public void Tmaincontent_Tick(object sender, EventArgs e)
{
    if (Panel1.Visible)
    {
        Panel1.Visible = false;
        Panel2.Visible = true;
        Panel3.Visible = false;
        UPmaincontent.Update();
        return;
    }

    if (Panel2.Visible)
    {
        Panel1.Visible = false;
        Panel2.Visible = false;
        Panel3.Visible = true;
        UPmaincontent.Update();
        return;
    }

    Panel1.Visible = true;
    Panel2.Visible = false;
    Panel3.Visible = false;
    UPmaincontent.Update();

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top