Why the ‘index’ always return value 0 although the ticker running and there is a statement ‘index = index + 1’?
'Dim index as integer = 0
'EDIT
Dim GridRowIndex as integer
Protected Sub TickerAnnouncement_Tick(sender As Object, e As System.EventArgs) Handles TickerAnnouncement.Tick
lblAnnouncementFooter.Text = GridView1.Rows(index).Cells(0).Text
'index = index + 1
If GridRowIndex> GridView1.Rows.Count Then GridRowIndex = 0
End Sub
I have turned the tick enabled but the index still returning the 0 value.
—ADDITION—
I have a property called ‘IndexValue’:
Public Property IndexValue() As Integer
Get
Dim s As Integer = DirectCast(ViewState("GridRowIndex"), Integer)
Return If(ViewState("GridRowIndex") Is Nothing, 0, CInt(ViewState("GridRowIndex")) + 1)
End Get
Set(value As Integer)
ViewState("GridRowIndex") = value
End Set
End Property
The
Timer.Tickwill cause a postback. All objects that belongs to the page(member variables,controls etc.) are disposed at the end of the page’s lifecycle. So when it’s rendered to the client, it doesn’t exist in the Server’s memory anymore. HTTP is stateless. Hence every variable(or dynamically created control) must be reinitialized/recreated on postbacks. This is the reason why yourindexis always 0.So you need to store it elsewhere. I would recommend the
ViewState:Edit:
I’ve just realized that this is a VB.NET question,sorry.
Declare this property:
Then set it in your event-handler:
You will find a complete list of all options on how to persist variables across postbacks here: http://msdn.microsoft.com/en-us/magazine/cc300437.aspx