0

Not sure why the labels are not continuously switching.

I break-pointed it and it shows the counter resets everytime and so it should just be continuously swapping through the numbers but it doesn't seem to be working

Thanks!

Public Class WebForm2 Inherits System.Web.UI.Page

Dim d As Integer() = {0, 1, 2, 3, 4}
Dim counter As Integer
Public Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not Page.IsPostBack Then
        Label1.Text = d(0)
        Label2.Text = d(1)
        Label3.Text = d(2)
        Label4.Text = d(3)
        Label5.Text = d(4)
    End If
End Sub

Public Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
    Dim hold As Integer
    counter = 0
    hold = d(0)
    Do While counter < 4
        d(counter) = d(counter + 1)
        counter += 1
    Loop
    Label1.Text = d(0)
    Label2.Text = d(1)
    Label3.Text = d(2)
    Label4.Text = d(3)
    Label5.Text = hold
End Sub

Protected Sub Timer1_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles Timer1.Tick
    Dim hold As Integer
    counter = 0
    hold = d(0)
    Do While counter < 4
        d(counter) = d(counter + 1)
        counter += 1
    Loop

    Label1.Text = d(0)
    Label2.Text = d(1)
    Label3.Text = d(2)
    Label4.Text = d(3)
    Label5.Text = hold
End Sub

End Class
Hitesh
  • 3,449
  • 8
  • 39
  • 57
  • I'm not sure what your question is. What specifically is the behavior you want? What does "not working" mean: do you get an error, how is it behaving? What things have you tried? – xDaevax Jun 10 '14 at 13:17
  • Thanks for the respond @xDaevax my problem is it's not quite doing what it's supposed to. It should (from the code) be displaying (0,1,2,3,4) then (1,2,3,4,0) then (2,3,4,0,1) but instead it runs through it once so it is (1,2,3,4,0) and then won't change from there. Thanks! –  Jun 10 '14 at 13:20
  • Problem is with your `Timer1_Tick` event. How are you triggering this event? Can you post the code? There should be a `Timer1.Start()` part in your code. – Atanu Roy Jun 10 '14 at 13:25
  • See this thread, if it helps : http://stackoverflow.com/questions/4684513/how-to-trigger-a-timer-tick-programmatically – Atanu Roy Jun 10 '14 at 13:28
  • @ChandanRoy I set the interval for the tick ever 3 seconds which is automatic (It's the AJAX timer) –  Jun 10 '14 at 13:32
  • Please post the code if possible, as it seems that the Ajax Timer is not firing the `Timer1_Tick` event at all. – Atanu Roy Jun 10 '14 at 13:33
  • All of my code it up there. _italic_ **bold**Dim hold As Integer hold = Label1.Text d(0) = Label2.Text d(1) = Label3.Text d(2) = hold Label1.Text = d(0) Label2.Text = d(1) Label3.Text = d(2) this code currently works with it. –  Jun 10 '14 at 13:41
  • Yes I know, but when I try to add 5 (with a counter) it doesn't work with it. @ChandanRoy –  Jun 10 '14 at 13:46
  • My guess(And i'm not too sure why this is true) is that it only works when I assign the array to the labels text, but doesn't work when I assign the array to another array element such as d(0) = d(1) but d(0) = label1.text works. Can you help me figure out why? –  Jun 10 '14 at 13:47
  • I have a explaination for that I guess. You trying to assign a value to a memory location by modifying the value of the same. Try storing the value in a temp variable, modify it and then assign. Like `int temp = d(0); d(0) = (temp+1);` – Atanu Roy Jun 10 '14 at 14:03
  • Dim temp As Integer Do While counter < 4 temp = d(counter + 1) d(counter) = temp counter += 1 Loop Tried this for my loop, still no luck, runs through it once then doesn't keep going –  Jun 10 '14 at 14:10
  • Though a better question might be: Why are you trying to do this with ajax and server-side timer events, instead of having a JavaScript timer and coding all of the array logic on the client-side? – xDaevax Jun 10 '14 at 14:26

2 Answers2

1

I am not familiar with vb.net but I've created asp.net pages before so I am taking an educated guess here^^

When clicking the button a postback happens and the whole page gets reloaded and the array d is declared again.

The values you store in your array are lost after the Post-Back. For keeping data you could use Session or ViewState variables:

Session

ViewState

As I said this is for asp.net but it might help anyways.

Edit: I still think the data is somewhere lost in this d-array... What if you just leave out the array and have your button_Click event like this:

        String temp = Label1.Text;

        Label1.Text = Label2.Text;
        Label2.Text = Label3.Text;
        Label3.Text = Label4.Text;
        Label4.Text = Label5.Text;
        Label5.Text = temp;
oHoodie
  • 209
  • 4
  • 13
  • `If Not Page.IsPostBack Then` is already implemented in the code to encounter the situation you are explaining. @oHoodie. – Atanu Roy Jun 10 '14 at 13:36
  • This works but not in the longrun. I want to be able to have 20 or more numbers and have them rotating through 5 labels. Thanks!!! –  Jun 10 '14 at 13:57
  • Ok then you have to use the array, but if you use it like above the array is always reset (the very first line of your code)... I found out you can store whole arrays in a Session-variable [Example here](http://www.shotdev.com/aspnet/aspnet-vbnet-object/aspnet-vbnet-session/aspnet-vbnet-session-array/) – oHoodie Jun 10 '14 at 14:05
1

Your array named d (is there a better name for this?) is being re-declared on every page-load (as it seems like it should be).

The problem is that you're not persisting the adjusted array values each page load. Instead, you're starting from scratch each time as others have suggested.

See the following URL for a lot of useful information about persisting state information in a web-forms application.

http://msdn.microsoft.com/en-us/library/vstudio/z1hkazw7(v=vs.100).aspx

As far as your code, you can try something along these lines to persist the values from the current array while setting it up for the next load.

Public Class WebForm2 Inherits System.Web.UI.Page

    Private _dValues As Integer(19)
    Private _currentValues As Integer(19)
    Private _newValues As Integer(19)
    Private _startIndex As Integer

    Public Sub New()
        _dValues = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19}
        _startIndex = 0
    End Sub

    Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
        If Page.IsPostBack Then 'Load values from form
            For i As Integer = 0 To _currentValues.Length - 1 Step 1
                _currentValues(i) = Request.Form(String.Concat("Hidden", i))
            Next
            _startIndex = Request.Form("StartIndex")
        Else
            _currentValues = _dValues 'First time around
        End If
        Dim position As Integer = _startIndex
        For i As Integer = 0 To _currentvalues.Length - 1 Step 1
            If position >= _newValues.Length Then
                position = 0
            End If
            'Assign the current position in the new array equal to the current sequential value in the previous array
            _newValues(position) = _currentValues(i)
            position += 1
        Next
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'Increment the counter every post back
        If Page.IsPostBack Then
            _startIndex += 1
        End If

        'Don't allow the counter to go outside the bounds of the array
        If _startIndex >= _currentValues.Length Then
            _startIndex = 0
        End If
        Me.StartIndex.Text = _startIndex 'Assign the value of the hidden field
    End Sub

    Public Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
    'Dynamically set label / hidden data
        For i As Integer = 0 To _newValues.Length - 1 Step 1
            CType(Page.FindControl(String.Concat("Label", i), Label).Text = _currentValues(i)
            CType(Page.FindControl(String.Concat("Hidden", i), Hidden).Text = _newValues(i)
        Next
    End Sub

    Protected Sub Timer1_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles Timer1.Tick
    'Dynamically set label / hidden data
        For i As Integer = 0 To _newValues.Length - 1 Step 1
            CType(Page.FindControl(String.Concat("Label", i), Label).Text = _currentValues(i)
            CType(Page.FindControl(String.Concat("Hidden", i), Hidden).Text = _newValues(i)
        Next
    End Sub

End Class

Then in your form:

<asp:Hidden ID="StartIndex" runat="server" Value="" />
<asp:Label ID="Label1" runat="server" Value="" />
<asp:Hidden ID="Hidden1" runat="server" Value="" />

<asp:Label ID="Label2" runat="server" Value="" />
<asp:Hidden ID="Hidden2" runat="server" Value="" />
etc...

This code (or at least a version of it) will build a number of label and hidden input fields that will be used to display data and persist data. Each time the page is posted, the start index will be incremented, which will change the start position of the new values. While the current values will be based on what is posted back from the hidden inputs.

xDaevax
  • 2,012
  • 2
  • 25
  • 36
  • is there anyway I can just use my code and assign it to a viewstate? Thanks! –  Jun 10 '14 at 15:07