String Values getting reset in VB.Net on ASP.Net 2.0 page

Why am I losing string values that I add to a 2 dimensional String array when I click on another date in a Calendar control? I am able to add strings during the Page_Load event and it works temporarily what I try later in the "butCustomAddEntry_Click" event but then when I click on another date in the calendar, the array seems to be reset to only the values in the Page_Load event. It seems like it is redimensioned every time a date is selected.

I am very new to ASP.Net 2.0. (Probably kinda obvious)
Partial Class Default2
    Inherits System.Web.UI.Page
    Dim holidays(12, 31) As String
 
    Sub Calendar1_DayRender(ByVal sender As Object, ByVal e As DayRenderEventArgs)
        Dim d As CalendarDay
        Dim c As TableCell
 
        d = e.Day
        c = e.Cell
 
        If d.IsOtherMonth Then
            c.Controls.Clear()
        Else
            Try
                Dim Hol As String
                Hol = holidays(d.Date.Month, d.Date.Day)
 
                If Hol <> "" Then
                    c.Controls.Add(New LiteralControl("<br>" + Hol))
                End If
            Catch exc As Exception
                Response.Write(exc.ToString())
            End Try
        End If
    End Sub
 
    Sub Date_Selected(ByVal sender As Object, ByVal e As EventArgs) Handles Calendar1.SelectionChanged
        If pnlCustomEntry.Visible = False Then
            pnlCustomEntry.Visible = True
        End If
        Label1.Text = "Selected date is: " + Calendar1.SelectedDate.ToShortDateString
        Label2.Text = Calendar1.SelectedDate.ToShortDateString
        Label3.Text = holidays(Calendar1.SelectedDate.Date.Month, Calendar1.SelectedDate.Date.Day)
    End Sub
 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim temp As Object = "New Year's Day"
        holidays(1, 1) = temp
        holidays(1, 26) = "Australia Day"
        holidays(2, 2) = "Groundhog Day"
        holidays(2, 14) = "Valentine's Day"
        holidays(3, 17) = "St. Patrick's Day"
        holidays(4, 1) = "April Fool's Day"
        holidays(5, 1) = "May Day"
        holidays(6, 15) = "My Birthday"
        holidays(7, 15) = "My Anniversary"
        holidays(8, 15) = "My Mother's Birthday"
        holidays(9, 24) = "Autumnal Equinox"
        holidays(12, 26) = "Boxing Day"
        ' this line sets the calendar to start on January of next year
        Calendar1.VisibleDate = New Date(Now.Year + 1, 1, 1)
    End Sub
 
    Protected Sub Calendar1_VisibleMonthChanged(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.MonthChangedEventArgs) Handles Calendar1.VisibleMonthChanged
        pnlCustomEntry.Visible = False
        'This portion keeps the calendar restricted to next year
        If Calendar1.VisibleDate.Year = Now.Year Then
            Calendar1.VisibleDate = New Date(Now.Year + 1, Calendar1.VisibleDate.Month, Calendar1.VisibleDate.Day)
        End If
    End Sub
 
    Protected Sub butCustomAddEntry_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles butAddCustomEntry.Click
        holidays(Calendar1.SelectedDate.Date.Month, Calendar1.SelectedDate.Date.Day) = txtCustomEntry.Text()
    End Sub
End Class

Open in new window

Default.aspx.txt
KaiserGuyAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
KaiserGuyConnect With a Mentor Author Commented:
I found an article explaining Viewstate that helped tremendously. This is the simplest ViewState syntax I found.
http://www.dotnetjohn.com/articles.aspx?articleid=71
 Everything else I tried was more complicated and did not work for me.

I ended up breaking my 2 dimensional String into 12 String arrays inside a select case structure.

Imports System.Data
Partial Class Default2
    Inherits System.Web.UI.Page
    Dim jan(31) As String
    Dim feb(31) As string
    Dim mar(31) As String
    Dim apr(31) As String
    Dim may(31) As String
    Dim jun(31) As String
    Dim jul(31) As String
    Dim aug(31) As String
    Dim sep(31) As String
    Dim oct(31) As String
    Dim nov(31) As String
    Dim dec(31) As String
 
    Sub storeDate(ByVal m As Integer, ByVal d As Integer, ByVal app As String)
        Select Case m
            Case 1
                jan(d) = app
                Me.ViewState.Add("janInViewState", jan)
            Case 2
                feb(d) = app
                Me.ViewState.Add("febInViewState", feb)
            Case 3
                mar(d) = app
                Me.ViewState.Add("marInViewState", mar)
            Case 4
                apr(d) = app
                Me.ViewState.Add("aprInViewState", apr)
            Case 5
                may(d) = app
                Me.ViewState.Add("mayInViewState", may)
            Case 6
                jun(d) = app
                Me.ViewState.Add("junInViewState", jun)
            Case 7
                jul(d) = app
                Me.ViewState.Add("julInViewState", jul)
            Case 8
                aug(d) = app
                Me.ViewState.Add("augInViewState", aug)
            Case 9
                sep(d) = app
                Me.ViewState.Add("sepInViewState", sep)
            Case 10
                oct(d) = app
                Me.ViewState.Add("octInViewState", oct)
            Case 11
                nov(d) = app
                Me.ViewState.Add("novInViewState", nov)
            Case 12
                dec(d) = app
                Me.ViewState.Add("decInViewState", dec)
        End Select
    End Sub
    Function getDate(ByVal m As Integer, ByVal d As Integer) As String
        Select Case m
            Case 1
                Return jan(d)
            Case 2
                Return feb(d)
            Case 3
                Return mar(d)
            Case 4
                Return apr(d)
            Case 5
                Return may(d)
            Case 6
                Return jun(d)
            Case 7
                Return jul(d)
            Case 8
                Return aug(d)
            Case 9
                Return sep(d)
            Case 10
                Return oct(d)
            Case 11
                Return nov(d)
            Case 12
                Return dec(d)
            Case Else
                Return ""
        End Select
    End Function
 
    Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        If (Me.ViewState("janInViewState") IsNot Nothing) Then
            jan = ViewState("janInViewState")
        End If
        If (Me.ViewState("febInViewState") IsNot Nothing) Then
            feb = ViewState("febInViewState")
        End If
        If (Me.ViewState("marInViewState") IsNot Nothing) Then
            mar = ViewState("marInViewState")
        End If
        If (Me.ViewState("aprInViewState") IsNot Nothing) Then
            apr = ViewState("aprInViewState")
        End If
        If (Me.ViewState("mayInViewState") IsNot Nothing) Then
            may = ViewState("mayInViewState")
        End If
        If (Me.ViewState("junInViewState") IsNot Nothing) Then
            jun = ViewState("junInViewState")
        End If
        If (Me.ViewState("julInViewState") IsNot Nothing) Then
            jul = ViewState("julInViewState")
        End If
        If (Me.ViewState("augInViewState") IsNot Nothing) Then
            aug = ViewState("augInViewState")
        End If
        If (Me.ViewState("sepInViewState") IsNot Nothing) Then
            sep = ViewState("sepInViewState")
        End If
        If (Me.ViewState("octInViewState") IsNot Nothing) Then
            oct = ViewState("octInViewState")
        End If
        If (Me.ViewState("novInViewState") IsNot Nothing) Then
            nov = ViewState("novInViewState")
        End If
        If (Me.ViewState("decInViewState") IsNot Nothing) Then
            dec = ViewState("decInViewState")
        End If
        storeDate(1, 1, "New Year's Day")
        storeDate(1, 26, "Australia Day")
        'this line sets the calendar to start on January of next year
        Calendar1.VisibleDate = New Date(Now.Year + 1, 1, 1)
    End Sub
 
    Sub Calendar1_DayRender(ByVal sender As Object, ByVal e As DayRenderEventArgs)
 
        Dim d As CalendarDay
        Dim c As TableCell
 
        d = e.Day
        c = e.Cell
 
        If d.IsOtherMonth Then
            c.Controls.Clear()
        Else
            Try
                Dim Hol As String = getDate(d.Date.Month, d.Date.Day)
                If Hol <> "" Then
                    c.Controls.Add(New LiteralControl("<br>" + Hol))
                End If
            Catch exc As Exception
                Response.Write(exc.ToString())
            End Try
        End If
    End Sub
 
    Sub Date_Selected(ByVal sender As Object, ByVal e As EventArgs) Handles Calendar1.SelectionChanged
        If pnlCustomEntry.Visible = False Then
            pnlCustomEntry.Visible = True
        End If
        Label1.Text = "Selected date is: " + Calendar1.SelectedDate.ToShortDateString
        Label2.Text = Calendar1.SelectedDate.ToShortDateString
        Label3.Text = getDate(Calendar1.SelectedDate.Date.Month, Calendar1.SelectedDate.Date.Day)
    End Sub
 
    Protected Sub Calendar1_VisibleMonthChanged(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.MonthChangedEventArgs) Handles Calendar1.VisibleMonthChanged
        pnlCustomEntry.Visible = False
        'This portion keeps the calendar restricted to next year
        If Calendar1.VisibleDate.Year = Now.Year Then
            Calendar1.VisibleDate = New Date(Now.Year + 1, Calendar1.VisibleDate.Month, Calendar1.VisibleDate.Day)
        End If
    End Sub
 
    Protected Sub butCustomAddEntry_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles butAddCustomEntry.Click
        storeDate(Calendar1.SelectedDate.Date.Month, Calendar1.SelectedDate.Date.Day, txtCustomEntry.Text)
    End Sub
End Class

Open in new window

0
 
ChumadConnect With a Mentor Commented:
Actually, it is being reset... Anything in the page_load is executed EVERY time a button is clicked that talks back to the server.

Check page.IsPostBack in page_load - if it's true, don't re-initialize the array.
0
 
Joel CoehoornConnect With a Mentor Director of Information TechnologyCommented:
Two important things you have to remember:

1) A brand new instance of your Default2 class is created for each and every request from the browser.  2) Every time any control raises an event is a new request.

Therefore, anytime someone clicks on your calendar, it's working with a brand new holidays array.  If you want it to persist between request you have to keep it somewhere that is persisted, such as viewstate, session, a database, a cache, or some other persisted object.
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
ChumadConnect With a Mentor Commented:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
     if Not Page.IsPostBack Then
        Dim temp As Object = "New Year's Day"
        holidays(1, 1) = temp
        holidays(1, 26) = "Australia Day"
        holidays(2, 2) = "Groundhog Day"
        holidays(2, 14) = "Valentine's Day"
        holidays(3, 17) = "St. Patrick's Day"
        holidays(4, 1) = "April Fool's Day"
        holidays(5, 1) = "May Day"
        holidays(6, 15) = "My Birthday"
        holidays(7, 15) = "My Anniversary"
        holidays(8, 15) = "My Mother's Birthday"
        holidays(9, 24) = "Autumnal Equinox"
        holidays(12, 26) = "Boxing Day"
        ' this line sets the calendar to start on January of next year
        Calendar1.VisibleDate = New Date(Now.Year + 1, 1, 1)
      End If
    End Sub
0
 
KaiserGuyAuthor Commented:
I read http://www.dotnetuncle.com/Aspnet/72_global_variables.aspx and I've attempted to implement a viewstate solution and a session solution as those seemed to be the most appropriate but to no avail.

The Page.IsPostBack is nice to know about but it isn't overwriting the entire array every time. It is resetting portions of the array but that isn't my problem.

A sample of how to implement a viewstate or session solution is what I'm looking for I guess.
0
 
CSLEEDSCommented:
hi,

double check if you have set the calender to auto postback in the properties window? if so then try disable it from there so that page wont post back after u click at the control.


regards.
0
 
KaiserGuyAuthor Commented:
I am not able to find any auto postback property or any property involving posting. That would be a nice solution. I've been thrashing around trying to get Viewstate to work but I'm not having much luck. Apparently its difficult to get a 2 dimensional String array through viewstate. This can't be as hard as I'm making it.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.