Click here to Skip to main content
15,917,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In asp.net with C#, I am loading an external website into an iframe on button click using the following code:
protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e)
C#
taskpage.Attributes["src"] = "somedynamicurlfromdatabase"; 
taskpage.Visible = true;

I want an Asp.net timer control to be enabled when the website loading in iframe completes.
Note:the button, iframe and timer are in update panel. i am using Visual Web Developer 2010 Express

What I have tried:

i have tried to call the iframe and checked through its properties but could not find any solution.
Posted
Updated 4-Mar-17 16:52pm

 
Share this answer
 
Comments
Member 8057273 4-Mar-17 12:59pm    
<script>
    $(function(){
        $('#taskpage').ready(function(){
            var timer = $find('<%= Timer1.ClientID %>');
            //starts the timer:
            timer._startTimer();  
        });
    }
  
</script>



<iframe id="taskpage" width="90%" style="overflow:scroll;width:100%;height:800px;" runat="server" ></iframe>


 <asp:Timer ID="Timer1" runat="server" Enabled="False" Interval="10" 
        ontick="Timer1_Tick">
    </asp:Timer>


protected void Timer1_Tick(object sender, EventArgs e)
    {
        Response.Write("Running");
    }


This is my complete code, but nothing worked
Graeme_Grant 4-Mar-17 19:34pm    
Nice list Bryian. I am amazed at how well my 75+-year-old parents can use it day to day... :)
Bryian Tan 4-Mar-17 22:22pm    
Thanks :)
Here is an example.

1. Wrap the timer in the updatepanel
2. Don't disable the timer at element level
ASP.NET
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <asp:Timer ID="Timer1" runat="server" OnTick="Timer1_Tick" Enabled="true" Interval="10"></asp:Timer>
            <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
        </ContentTemplate>
    </asp:UpdatePanel>

    <iframe id="taskpage" src="http://www.c-sharpcorner.com/blogs/start-stop-asp-net-timer-control-using-javascript1" >
        <p>Your browser does not support iframes.</p>
    </iframe>


3. on page load, stop the timer
4. Once the iframe is loaded, start the timer. You need to test out the code to detect if iframe loaded completely for cross browser compatibility
JavaScript
<script>
        $(document).ready(function () {
            //stop the time
            var timer = $find('<%= Timer1.ClientID %>');
            timer._stopTimer();
        });

        $('#taskpage').on('load', function () {
            var timer = $find('<%= Timer1.ClientID %>');
           //starts the timer:
            timer._startTimer();
        });
    </script>


Update the label every time the timer tick
C#
protected void Timer1_Tick(object sender, EventArgs e)
        {
            Label1.Text = DateTime.Now.ToString("yyyy/MM/dd/ HH:mm:ss");
        }
 
Share this answer
 
Comments
Member 8057273 5-Mar-17 2:30am    
The timer starts working when the page loads irrespective of iframe status and the Console debugging says:
Uncaught TypeError: Cannot read property '_stopTimer' of null
    at HTMLDocument.<anonymous> (tasklist.aspx:24)
    at c (jquery.min.js:3)
    at Object.fireWith [as resolveWith] (jquery.min.js:3)
    at Function.ready (jquery.min.js:3)
    at HTMLDocument.H (jquery.min.js:3)
(anonymous) @ tasklist.aspx:24
c @ jquery.min.js:3
fireWith @ jquery.min.js:3
ready @ jquery.min.js:3
H @ jquery.min.js:3

Bryian Tan 5-Mar-17 17:15pm    
what browser you using? Do you have <asp:ScriptManager runat="server"> on the page?
 
Share this answer
 
Comments
Member 8057273 4-Mar-17 11:53am    
This link shows a javascript function. What i need is to enable the asp timer when iframe loading finishes. On timer tick, i will further execute some code but timer should start when iframe loading gets finished.
Graeme_Grant 4-Mar-17 11:56am    
Yes, you will need to use javascript/jquery/etc... to do what you want.
Member 8057273 4-Mar-17 12:00pm    
please provide a little snippet which can enable the asp timer through JS/JQ ... I am not able to get this working.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900