Solved

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

Posted on 2008-06-24
7
595 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…

679 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