简体   繁体   English

固定间隔后刷新updatepanel

[英]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. 我需要每隔10秒刷新一次updatepanel。 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意味着页面回发给自己

!IsPostBack means the first time you load the page of just refresh the page, it was not post from itself !IsPostBack意味着您第一次加载刚刚刷新页面的页面时,它不是自己发布的

why don't you use the AJAX Timer control instead. 为什么不使用AJAX Timer控件呢? It'll serve the same purpose in a neat, concise & efficient manner. 它将以一种整洁,简洁和有效的方式起到同样的作用。

Anyways, Both setTimeout() & setInterval() work the same way. 无论如何,setTimeout()和setInterval()都以相同的方式工作。

The key difference is that: 关键的区别在于:

  • setTimeout will wait the specified period of time, run the code we give it, and then stop. setTimeout将等待指定的时间段,运行我们给它的代码,然后停止。
  • 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). 另一方面, setInterval将等待,运行代码 - 然后再次等待并再次运行代码 - 永远重复(或直到我们告诉它停止)。

In brief, setTimeout() runs the supplied code only once after the specified interval, whereas setInterval() runs the code again & again after the specified interval. 简而言之,setTimeout()仅在指定的时间间隔后运行提供的代码一次,而setInterval()在指定的时间间隔后再次运行代码。

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 资料来源:jQuery - Ninja的新手

Why don't you try AJAX timer control? 你为什么不尝试AJAX定时器控制? 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();   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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