Basically I’m simply trying to implement a timer control that will swap banners for me and then update in an update panel. However the timer only affects my page once, and appears to be firing twice(I put a label on the form and attempted to increment by one each time it would update, and it jumped to 2 then stopped).
<asp:ContentPlaceHolder ID="cphHead" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:Timer ID="Timer1" runat="server" Interval="6000" OnTick="Timer1_Tick">
</asp:Timer>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1" />
</Triggers>
<ContentTemplate>
<asp:Image ID="Image1" runat="server" ImageUrl="~/images/banner_2.jpg" />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
</asp:ContentPlaceHolder>
and there is my code behind
Partial Class ASPTravel
Inherits System.Web.UI.MasterPage
Private intnum As Integer = 0
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Protected Sub LoginStatus1_LoggingOut(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.LoginCancelEventArgs) Handles LoginStatus1.LoggingOut
Session.Abandon()
End Sub
Protected Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
intnum = intnum + 1
Label1.Text = CStr(intnum)
' If intnum = 0 Then
' Image1.ImageUrl = "~/images/banner_1.jpg"
' intnum = 1
' Else
' Image1.ImageUrl = "~/images/banner_2.jpg"
'End If
Dim n As Integer = New Random().Next(1, 2)
Image1.ImageUrl = "~/images/banner_" + n.ToString() + ".jpg"
End Sub
End Class
Remove the
Handles Timer1.Tickfrom your code behind and it will works fine.I have checked it.like this.
Reference: http://forums.asp.net/t/1642896.aspx/1
also user viewstate instead of variables. Variables are initialized on every postback in asp.net. So instead of saving value in a variable
Private intnum As Integer = 0save it in a View state like thisand in your timer click event.