views:

22

answers:

2

I've created a very simple asp.net web page for generating some HTML. The idea is that each time the user clicks the button previous inputs gets stored into an array and is then listed together with the most recent input. The problem is that the array resets for each clicks, the same goes for the counter I've made (using "i" as integer and "i += 1" for each click.)

The result is that Case 0 is chosen every time, which of course isn't the plan.

Provided is the code. In advance, thanks.

Partial Class _Default
Inherits System.Web.UI.Page
Dim name, url, dive As String
Dim i As Integer
Dim rememberme(2) As String

Protected Sub btn_readmetoo_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn_readmetoo.Click
    name = txt_name.Text
    url = txt_url.Text

    If i = 3 Then
        i = 0
    End If

    Select Case i
        Case 0
            rememberme(0) = "Les også: " & "<a href=""" & url & """" & ">" & name & "</a>"
            txt_listurls.Text = "<p>" & rememberme(0) & "</p>"
        Case 1
            rememberme(1) = "Les også: " & "<a href=""" & url & """" & ">" & name & "</a>"
            txt_listurls.Text = "<p><ul><li>" & rememberme(0) & "<li>" & rememberme(1) & "</ul></p>"
        Case 2
            rememberme(2) = "Les også: " & "<a href=""" & url & """" & ">" & name & "</a>"
            txt_listurls.Text = "<p><ul><li>" & rememberme(0) & "<li>" & rememberme(1) & "<li>" & rememberme(2) & "</ul></p>"
    End Select

    i += 1
    lbl_counter.Text = i
End Sub
+1  A: 

The rememberme array is reset because they are not part of the Page ViewState. If you want to persist the array, then you can use the following syntax:

Protected Sub btn_readmetoo_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn_readmetoo.Click
    name = txt_name.Text
    url = txt_url.Text

    If i = 3 Then
        i = 0
    End If

    'recall array from ViewState
    If ViewState("rememberme") IsNot Nothing Then
        rememberme = ViewState("rememberme")
    End If

    Select Case i
        Case 0
            rememberme(0) = "Les også: " & "<a href=""" & url & """" & ">" & name & "</a>"
            txt_listurls.Text = "<p>" & rememberme(0) & "</p>"
        Case 1
            rememberme(1) = "Les også: " & "<a href=""" & url & """" & ">" & name & "</a>"
            txt_listurls.Text = "<p><ul><li>" & rememberme(0) & "<li>" & rememberme(1) & "</ul></p>"
        Case 2
            rememberme(2) = "Les også: " & "<a href=""" & url & """" & ">" & name & "</a>"
            txt_listurls.Text = "<p><ul><li>" & rememberme(0) & "<li>" & rememberme(1) & "<li>" & rememberme(2) & "</ul></p>"
    End Select

    i += 1
    lbl_counter.Text = i

    'Store array in ViewState
    ViewState("rememberme") = rememberme
End Sub
Prutswonder
Thanks a bunch, that worked like a charm :-)
KG Christensen
A: 

Yes, each time the page reloads, the code starts over and creates a new page from scratch.

If you want to persist data from one page load to another, there are basically two solutions:

  1. Put the data in the page (hidden field, cookie, et.c.) so that it's returned to the server when it requests the next page.

  2. Put the data in a Session variable.

Guffa