?
Solved

Don't do a PostBack when Timer.Tick fires

Posted on 2014-02-15
7
Medium Priority
?
1,018 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
7 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
Not sure which OpenStack Certification to get?

So you’ve realized you might want to get certified in OpenStack, but you’re not sure what the benefits might be or even which one you should take. You know there are several certification courses you can choose from, but how do you know which one is right for you?

 

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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

770 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