2012-05-08 42 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。內容會改變