2012-05-08 43 views
0

为什么JQuery功能在股票更新时不起作用更新面板?请参阅下面的代码。JQuery功能在股票更新时不起作用更新面板

<asp:Timer ID="Schedule_Timer" runat="server" Interval="10000"></asp:Timer>  
<asp:UpdatePanel ID="Schedule_UpdatePanel" runat="server"> 
    <Triggers> 
     <asp:AsyncPostBackTrigger ControlID="Schedule_Timer" EventName="Tick" /> 
    </Triggers> 
    <ContentTemplate> 
     <asp:Panel ID="Schedule_Panel" runat="server" Height="250px" Width="250px"> 
      <asp:Literal ID="Schedule_Label" runat="server" Text=""></asp:Literal> 
     </asp:Panel> 
    </ContentTemplate> 
</asp:UpdatePanel> 

<script> 
    $('#test').cycle({ 
     fx: 'scrollUp', 
     timeout: 6000, 
     delay: -2000 
    }); 
</script> 

Schedule_Label经由代码隐藏填充:

Protected Sub Schedule_Timer_Tick(sender As Object, e As System.EventArgs) Handles Schedule_Timer.Tick 
    PrintTheSchedule() 
End Sub 

Private Sub PrintTheSchedule() 
    //This is just for example, the actual data is taken from gridview based on the realtime 
    Schedule_Label.Text = "<div id='test'>" & _ 
       "<div>test 1</div>" & _ 
       "<div>test 2</div>" & _ 
       "<div>test 3</div>" & _ 
       "<div>"   
End Sub 

对于第一个10秒,JQuery的做循环所述test DIV。但是在UpdatePanel刷新后,JQuery不再运行,并且它导致页面上显示的所有test div值。如何解决这个问题?非常感谢你。

回答

4

有一个preety简单的答案。

在页面上执行的jquery函数仅加载自身。所以当计时器打勾时,jquery函数不再保持有效。

所以,你需要做的是:

在PrintTheSchedule功能再次调用javascript函数一样

在C#我这样做像

ScriptManager.RegisterClientScriptBlock(Page, typeof(Page), "runScript", "RunScriptAgain();", true); 

而在aspx页面做

<script> 

function RunScriptAgain() 
{ 
    $('#test').cycle({ 
    fx: 'scrollUp', 
    timeout: 6000, 
    delay: -2000 
    }); 
} 

//首次需要

$(document).ready(function() { RunScriptAgain(); }) 

+0

嗨,它的作品。但为什么前10秒JQuery不运行? 10秒后,代码适用于我.. –

+0

@ mrjimoy_05在页面加载中,您还可以运行ScriptManager代码。因为需要在第一次运行RunScript函数或者使用Jquery Document ready加载此函数 – Moons

+0

@ mrjimoy_05检查更新的代码 – Moons

0

更新面板刷新后,然后老id与id测试被破坏,因此你将不得不绑定每个面板更新的循环功能。
一个这样做的另一种方法是使用jquerys的liveQuery插件 http://docs.jquery.com/Plugins/livequery 和修改脚本如下

$('#test').livequery(function(){ 
$(this).cycle({ 
     fx: 'scrollUp', 
     timeout: 6000, 
     delay: -2000 
    }); 
}); 
0

可能,你需要重新初始化周期BEC。内容会改变