Solved

Don't do a PostBack when Timer.Tick fires

Posted on 2014-02-15
7
694 Views
Last Modified: 2016-06-20
Using MS Visual Web Developer Express 2010 for some basic web page construction.

I want to change the page that is displayed in an iFrame on one of my pages programmically.  

I'm trying to use a .Net Timer control to see if the page displayed should be changed every 15 seconds.  That part is working.  The problem is that even when the iFrame's page shouldn't be changed, the entire page the iFrame is on is being recreated every 15 seconds.  

I'm guessing the Timer.Tick event function works by triggering a PostBack to the page?  (I'm new to web programming so forgive me if my understanding of the concepts is incomplete)

How can I check every 15 seconds, but only recreate the page (or better yet, the iFrame) if the contents of the iFrame need to be changed.  Maybe move the code that checks the need to change in the iFrame should go in the PageLoad event rather than the Timer.Tick event?   And then only run the PageLoad event if the iFrame has changed?
0
Comment
Question by:pwashburn1224
  • 3
  • 2
7 Comments
 
LVL 35

Expert Comment

by:Miguel Oz
ID: 39863343
You are better off using Javascript timing methods

In your Javascript timer event you can write: //pseucode
function myTimer()
{

if (condition)
  __dopostback('sender id e.g. a button', '');
else
   alert('Process any non post back action here');
}
0
 

Author Comment

by:pwashburn1224
ID: 39865359
I've been able to accomplish most of what I want by using the .Net UpdatePanel.  Now just have to figure out how to update/redraw the iFrame, which is outside of the UpdatePanel, when i change the src attribute for the iFrame.  The Timer is inside the UpdatePanel and I have a Response.Redirect within the Timer.Tick event that should do a Response.Redirect when the iFrame needs to be changed, but it doesn't happen.
0
 
LVL 35

Expert Comment

by:Miguel Oz
ID: 39865992
Please post your markup and code behind.
If this is server based solution, typically we do use a timer outside the update panel so that the timer acts as a trigger to refresh the panel contents as shown here:
http://msdn.microsoft.com/en-us/library/system.web.ui.timer.tick(v=vs.110).aspx
http://weblogs.asp.net/dmiyamasu/archive/2008/02/15/asp-net-ajax-slideshow-using-updatepanel-and-timer.aspx
Notice that in examples above only the update panet is refreshed.
The iframe is always refreshed when you change the src, but it depends where in the code this change is located, in this case it should be outside the update panel.
Update panel are only used to update part of your page independent of the main page. If you can be more specific regarding your design/requirements I can provide more help.


Note: Also add ASP.NET zone to get more expert's to have a look at your question.
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:pwashburn1224
ID: 39867506
Here's my code.   It's an iFrame who's content is controlled by reading a simple text file on the server.   The GetDocument() function reads the text file.  The previous contents of the iFrame's src is stored in a Session variable.  When there is a change in the text file, update the src on the iFrame and update the page.  That is all working.  The If....Then statement in the Timer_Tick event is branching as it should.  Everything works, except......

The contents of the iFrame are being redrawn on each Tick of the Timer whether the iFrame src has changed or not.   The If.....Then statement with the UpdatePanel1.Update is not causing it except when it should.  What is causing this redrawing (Postback?)

----------------------

Partial Class WebcastDocuments
    Inherits System.Web.UI.Page
    Protected Sub Timer1_Tick(sender As Object, e As System.EventArgs) Handles Timer1.Tick
        Dim strUrlDoc As String
        strUrlDoc = GetDocumentURL()
        If Session("PreviousURL") <> strUrlDoc Then
            Dim iFrame1 As HtmlControl = CType(Me.FindControl("iFrame1"), HtmlControl)
            iFrame1.Attributes("src") = strUrlDoc
            Session("PreviousURL") = strUrlDoc
            UpdatePanel1.Update()
        End If

    End Sub
    Private Function GetDocumentURL() As String
        Dim strFilePath As String = " ----- path to File ----"
        Dim strFile As String = "CurrentDocument.txt"
        If System.IO.File.Exists(strFilePath & strFile) = True Then
            Dim objReader As New System.IO.StreamReader(strFilePath & strFile)
            GetDocumentURL = objReader.ReadLine()
            objReader.Close()
        Else
            GetDocumentURL = "----- default URL ----------"
        End If
    End Function

End Class

-------------------------

<%@ Page Language="VB" AutoEventWireup="true" CodeFile="WebcastDocuments.aspx.vb" Inherits="WebcastDocuments" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div style="font-size: x-large; text-align: left;">
        Webcast Supporting Documents<br />
   
    </div>
<asp:ScriptManager ID="ScriptManger1" runat="Server">
</asp:ScriptManager>
           <br />
           <asp:Timer ID="Timer1" runat="server" Interval="3000">
          </asp:Timer>
          <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
              <ContentTemplate>
                <iframe  runat="server" style="border: 2px black solid" width="90%" height="800"
                    frameborder="2" scrolling="Yes" ID="iFrame1" name="I1"
                    src="https:\\--- actual domain ---\DefaultWebcastDocument.htm"></iframe></td>
                  <br />
                  <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
              </ContentTemplate>
              <Triggers>
                  <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick">
                  </asp:AsyncPostBackTrigger>
              </Triggers>
          </asp:UpdatePanel>
   </form>
</body>
</html>
0
 
LVL 11

Expert Comment

by:SAMIR BHOGAYTA
ID: 39869739
hi..
I got the solution for both problems.

    I placed asp:Timer inside an asp:UpdatePanel control. This is the right thing to do in my case since I don't need to refresh entire page before the next question. But I discovered that in this case ticks from timer don't interfere with each other.

    As described
var b = $find(<%= Timer1.ClientID %>);
      if(b){
      b._stopTimer();
      b._startTimer();
      }

, I can disable asp:Timer using javascript like this:

    $find("TimerMaxJudgeTime")._stopTimer();

    Then I bind this to my buttons' onclick event and I'm done. Since timer is placed inside an UpdatePanel, it is autmatically recreated and enabled after a postback.
0
 

Accepted Solution

by:
pwashburn1224 earned 0 total points
ID: 39870301
I've solved the problem.   It looks like the Timer_Tick always causes a PostBack that will force a rewrite of the UpdatePanel.  My solution was to create a second UpdatePanel and let the Timer be the <Trigger> for the second UpdatePanel.  There's no other items in the <ContentTemplate> other than the Timer.  So there's no visible flash of the screen being redrawn on the Timer_Tick event as there's no visible controls.    The Timer_Tick method works as intended.   If the src of the iFrame has changed, then do an Update on the 1st UpdatePanel:
UpdatePanel1.Update()

Everything works as I want.   The aspx.vb code has not changed from the above posting.  Here's the changed .aspx code:

----------

<%@ Page Language="VB" AutoEventWireup="true" CodeFile="WebcastDocuments.aspx.vb" Inherits="WebcastDocuments" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div style="font-size: x-large; text-align: left;">
        Webcast Supporting Documents<br />
   
    </div>
<asp:ScriptManager ID="ScriptManger1" runat="Server">
</asp:ScriptManager>
           <br />
          <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
              <ContentTemplate>
                <iframe  runat="server" style="border: 2px black solid" width="90%" height="800"
                    frameborder="2" scrolling="Yes" ID="iFrame1" name="I1"
                    src="https:\\---- actual domain -------\WebcastApp\DefaultWebcastDocument.htm"></iframe></td>
                  <br />
              </ContentTemplate>
           </asp:UpdatePanel>
  <asp:UpdatePanel ID="UpdatePanel2" runat="server">
        <ContentTemplate>
            <asp:Timer ID="Timer1" runat="server" Interval="3000">
            </asp:Timer>
        </ContentTemplate>
               <Triggers>
                  <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick">
                  </asp:AsyncPostBackTrigger>
              </Triggers>
    </asp:UpdatePanel>
   </form>
</body>
</html>
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question