简体   繁体   中英

Refresh updatepanel after fixed interval

This is my script:

 <script type="text/javascript">
    $(document).ready(
    function (){

        setTimeout('myFun()', 10000);
     });

    function myFun() {
        var btn = document.getElementById('<%=myBtn.ClientID %>');
        alert(btn);
        btn.click();
     }
</script>

My markup:

<asp:UpdatePanel ID="upMain" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
        <asp:Label ID="lblValue" runat="server" Text=""></asp:Label>
    </ContentTemplate>
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="myBtn" EventName="Click" />
     </Triggers>
</asp:UpdatePanel>
<asp:Button ID="myBtn" runat="server" Text="hit" />

Code Behind:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        lblValue.Text = "0";
    }
    else
    {
        lblValue.Text = Convert.ToString(Convert.ToInt32(lblValue.Text) + 1);
    }
}

I need to refresh the updatepanel after every 10 seconds. But after page load, only once I'm able to achieve that. Is there anything I'm missing? Thanks.

删除if (!IsPostBack) ,因此,它只更新一次,

IsPostBack means that the page was post back to itself

!IsPostBack means the first time you load the page of just refresh the page, it was not post from itself

why don't you use the AJAX Timer control instead. It'll serve the same purpose in a neat, concise & efficient manner.

Anyways, Both setTimeout() & setInterval() work the same way.

The key difference is that:

  • setTimeout will wait the specified period of time, run the code we give it, and then stop.
  • setInterval , on the other hand, will wait, run the code—then wait again and run the code again—repeating forever (or until we tell it to stop).

In brief, setTimeout() runs the supplied code only once after the specified interval, whereas setInterval() runs the code again & again after the specified interval.

So use:

<script type="text/javascript">     
$(document).ready(    
     function ()
     {          
             setInterval('myFun()', 10000);      
     });
     function myFun() 
     {         
             var btn = document.getElementById('<%=myBtn.ClientID %>');         
             alert(btn);
             btn.click();      
     } 
</script> 

Source: jQuery - Novice to Ninja

Why don't you try AJAX timer control? It is a simple way to postback in some fixed interval.

try

   __dopostback('<%=myBtn.UniqueID%>',0);

insted of click

 var btn = document.getElementById('<%=myBtn.ClientID %>');         
             btn.click();   

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM