?
Solved

Change the src of an IFrame dynamically

Posted on 2005-04-12
3
Medium Priority
?
1,823 Views
Last Modified: 2008-03-10
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?
0
Comment
Question by:wsmith5204
3 Comments
 
LVL 29

Accepted Solution

by:
rdivilbiss earned 1000 total points
ID: 13765581
The C# code executes prior to the page being written to the browser (client).  Once the page is written to the browser, you will need client side code (JavaScript) to change the page content.

The code would be very similar to what you have in C#.  You would need to use the onload event to call your JavaScript function. e.g. <body on;oad="myFuntion();">.

Regards,
Rod





0
 

Author Comment

by:wsmith5204
ID: 13773563
So there is no way to get the IFrame to load the URL from C#?
0
 
LVL 12

Assisted Solution

by:HugoHiasl
HugoHiasl earned 1000 total points
ID: 13774374
Nope.. If the code is visible in the Browser, C# cannot access it again except after a server roundtrip.
Do your required action clientside...

use something like this:

<HTML>
<HEAD>
  <script language="JavaScript">
    var actualPage = 0;
   
    function changePage() {
      switch (actualPage) {
      case 0:
        document.getElementById("myIframe").src =  "http://dc.indymedia.org/";
        actualPage++;
        break;
      case 1:
        document.getElementById("myIframe").src =  "http://www.zmag.org/ZNETTOPnoanimation.html";
        actualPage++;
        break;
      case 2:
        document.getElementById("myIframe").src = "http://www.alternet.org";
        actualPage++;
        break;
      case 3:
        document.getElementById("myIframe").src = "http://www.thenation.com/";
        actualPage = 0;
        break;
      }
      setTimeout('changePage()',10000);  
    }
   
    setTimeout('changePage()',10000);
  </script>
</HEAD>
<BODY>

  <iframe id="myIframe" src=""></frame>

</BODY>
</HTML>



Best regards
Oliver
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

There’s a good reason for why it’s called a homepage – it closely resembles that of a physical house and the only real difference is that it’s online. Your website’s homepage is where people come to visit you. It’s the family room of your website wh…
The first step to building an amazing About page is to figure out what you want the page to say about your company. You then must grab the attention of the reader, boast a bit, tell a story and let others brag about you. With a little bit of thought…
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.
Suggested Courses
Course of the Month13 days, 22 hours left to enroll

809 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