Link to home
Start Free TrialLog in
Avatar of wsmith5204
wsmith5204Flag for United States of America

asked on

Change the src of an IFrame dynamically

I'm trying to change the src of an IFrame dynamically every 30 seconds using C# code behind and a timer.  Here's my web page:

<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="true" Inherits="WebApplication1.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
      <HEAD>
            <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
            <meta name="CODE_LANGUAGE" Content="C#">
            <meta name="vs_defaultClientScript" content="JavaScript">
            <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
      </HEAD>
      <body MS_POSITIONING="GridLayout" bgcolor="#ffffff">
      <form id="Form1" runat="server">
                  <iframe runat=server id='myFrame' align='right' src='about:blank' height='100%' width='75%'></iframe>
      </form>
      </body>
</HTML>

Here's my code behind without the "using" includes:

namespace WebApplication1
{
      public class WebForm1 : System.Web.UI.Page
      {
            protected System.Web.UI.HtmlControls.HtmlGenericControl myFrame;
            private System.Timers.Timer timer1;
            private int state;
            private double startTime;


            private void Page_Load(object sender, System.EventArgs e)
            {
                  theState = 1;
                  startTime = getTime();
            }

            override protected void OnInit(EventArgs e)
            {
                  //
                  // CODEGEN: This call is required by the ASP.NET Web Form Designer.
                  //
                  InitializeComponent();
                  base.OnInit(e);
      
            }
            
            private void InitializeComponent()
            {    
                  this.Load += new System.EventHandler(this.Page_Load);
                  this.components = new System.ComponentModel.Container();
                  this.timer1 = new System.Timers.Timer();
                  //
                  // timer1
                  //
                  this.timer1.Enabled = true;
                  this.timer1.Interval = 1000;
                  this.timer1.Elapsed += new ElapsedEventHandler(this.myTest);
            }      



            private void myTest(object sender, ElapsedEventArgs e)
            {
                  double now = getTime();
                  if(theState == 1 && (now  > startTime + 30))
                  {
                        theState = 2;
                        startTime = getTime();
                        myFrame.Attributes["src"] = "http://dc.indymedia.org/";
                  }
                  else if(theState == 2 && (now  > startTime + 30))
                  {
                        theState = 3;
                        startTime = getTime();
                        myFrame.Attributes["src"] = "http://www.zmag.org/ZNETTOPnoanimation.html";
                  }
                  else if(theState == 3 && (now  > startTime +30))
                  {
                        theState = 4;
                        startTime = getTime();
                        myFrame.Attributes["src"] = "http://www.alternet.org";
                  }
                  else if(theState == 4 && (now  > startTime + 30))
                  {
                        theState = 5;
                        startTime = getTime();
                        myFrame.Attributes["src"] = "http://www.thenation.com/";
                  }
                  else if(theState == 5 && (now  > startTime + 30))
                  {
                        theState = 6;
                  }
            }

            private double getTime()
            {
                  DateTime d1 = DateTime.Now;
                  return((double)d1.Hour * 3600 + (double)d1.Minute * 60 + (double)d1.Second + (double)d1.Millisecond/1000);
            }
            

      }
}

Iframe never changes from the initial setting. Am I missing something?
ASKER CERTIFIED SOLUTION
Avatar of rdivilbiss
rdivilbiss
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of wsmith5204

ASKER

So there is no way to get the IFrame to load the URL from C#?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial