Solved

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

Posted on 2008-06-24
7
590 Views
Last Modified: 2013-11-07
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
0
Comment
Question by:KaiserGuy
7 Comments
 
LVL 8

Assisted Solution

by:Chumad
Chumad earned 50 total points
ID: 21858783
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
 
LVL 18

Assisted Solution

by:jcoehoorn
jcoehoorn earned 100 total points
ID: 21858784
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
 
LVL 8

Assisted Solution

by:Chumad
Chumad earned 50 total points
ID: 21858803
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:KaiserGuy
ID: 21859503
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
 
LVL 4

Expert Comment

by:CSLEEDS
ID: 21861997
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
 

Author Comment

by:KaiserGuy
ID: 21862412
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
 

Accepted Solution

by:
KaiserGuy earned 0 total points
ID: 21862728
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

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now