Solved

Don't do a PostBack when Timer.Tick fires

Posted on 2014-02-15
7
514 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:pwashburn1224
Comment Utility
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
Comment Utility
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
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

763 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now