?
Solved

Don't do a PostBack when Timer.Tick fires

Posted on 2014-02-15
7
Medium Priority
?
1,300 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
6 Comments
 
LVL 36

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 36

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
Restore individual SQL databases with ease

Veeam Explorer for Microsoft SQL Server delivers an easy-to-use, wizard-driven interface for restoring your databases from a backup. No expert SQL background required. Web interface provides a complete view of all available SQL databases to simplify the recovery of lost database

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses
Course of the Month16 days, 14 hours left to enroll

864 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