p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

aspdotnet_website_programming thread: Losing Variable Values in Code Behind After Page Renders


Message #1 by "Kirk Gomez" <hed2hed@c...> on Sat, 11 May 2002 04:17:55
Code Behind follows at end of message...

The initial page renders CORRECTLY.  Meaning that "FLeagueID", "FromDate", 
and "ToDate" are initally populated correctly.

However, when the "DateRange_Changed" function is fired based on an event 
in another control ("swamiDateControl") the values 
to "FLeagueID", "FromDate", and "ToDate" are all reset back to their 
original state (NULL basically).

Is this normal behavior?  Shouldn't those values be retained and 
referenceable for the life of the object?  I would like for the dates to 
be what is current when the event fires.  The work around of course would 
be to store in invisible asp:labels but this is a work around in my 
opinion.  I want to do it the correct way!

This is killing me.  Any help would be very much appreciated.

Kirk Gomez
hed2hed@c...

The following is the code behind class def to a Custom User control which 
inherits "User Control":

Public Class PressReleases: Inherits FLeagueUserControl

  'To Be Set By User
  Public Dim FromDate      as Datetime = Datetime.Now.Date
  Public Dim ToDate        as Datetime = FromDate

  'Buttons
  Public Dim btnCreate     as LinkButton

  'Labels
  Public Dim lblMsg        as Label

  'Repeaters
  Public Dim dbRS          as Repeater

  'Swami
  Public Dim titleFLeague  as FLeagueUserControl
  Public Dim swamiViewBets as FLeagueUserControl
  Public Dim swamiDateControl as DateRangeControl

  Sub Page_Load( s as Object, e as EventArgs )
    if not isPostBack then
      FLeagueID = SessionFLeagueID
      if not Request.QueryString( "FromDate" ) = "" then
        FromDate = Request.QueryString( "FromDate" )
      else
        'FromDate = CurrentPeriodBegin
      end if
      if not Request.QueryString( "ToDate" )   = "" then
        ToDate = Request.QueryString( "ToDate" )
      else
        'ToDate = CurrentPeriodEnd
      end if
      swamiDateControl.DisplayedFromDate = FromDate
      swamiDateControl.DisplayedToDate   = ToDate
      BindData
    end if
  End Sub

  Sub BindData
    Dim dbConnection As sqlConnection
    Dim dbCommand    As sqlCommand
    Dim sql          As String

    titleFLeague.DataBind

    swamiViewBets.DataBind
    btnCreate.DataBind

    dbConnection = New sqlConnection(ConfigurationSettings.AppSettings
("Sql_DSN"))
    dbConnection.Open()
    sql = "SELECT PressReleaseID " & _
          "  FROM tblPressReleases " & _
          " WHERE FSeasonID = " & SessionFSeasonID & _
          "   AND ( PublishedInd = -1 OR CustomerID = " & 
SessionCustomerID & " ) " & _
          "   AND PostDate >= '" & FromDate.ToString() & "'" & _
          "   AND PostDate < '" & ToDate.AddDays( 1 ).ToString() & "'" & _
          " ORDER BY PostDate DESC , PressReleaseID DESC "

    dbCommand = New SqlCommand( sql, dbConnection )
    dbRS.DataSource = dbCommand.ExecuteReader()
    dbRS.DataBind()
    dbConnection.Close
  End Sub

  Sub btnCreate_Click( s as Object, e as EventArgs )
    Response.Redirect( "EditPressRelease.aspx" )
  End Sub

  Sub DateRange_Changed( s as Object, e as DateRangeEventArgs )
    'Response.Redirect( String.Format( "PressReleases.aspx?FromDate={0}
&ToDate={1}", e.DisplayedFromDate.Date.ToString(), 
e.DisplayedToDate.Date.ToString() ) )
'FLeagueID should already be defined and remembered by the instantiated 
object!  But it's not!
lblMsg.Text = FromDate.ToString() & " -- " & ToDate.ToString()
    'FromDate = e.AbsoluteFromDate
    'ToDate   = e.AbsoluteToDate
    'BindData
  End Sub
End Class

Message #2 by "Minh T. Nguyen" <nguyentriminh@y...> on Fri, 10 May 2002 21:00:42 -0700
Kirk,

	This is normal behavior. Since HTTP is a stateless protocol
these values are only alive during the execution of your ASP.NET page,
since next time when the user fires another event all these variables
are lost again. You have several alternatives:

	You can store your variables in the Session object which is a
repository of name-value pairs that reside on the server for each
session. You can store variables in the Application object which is a
repository of name-value pairs that reside on the server for all
sessions. You can store it in the viewstate, which is a repository that
is stored on the client side (and is in fact implemented as you have
suggested, namely hidden HTML controls). At last, you can store these
values in client-side cookies.

	I think for you, the Session object is probably the most
appropriate.

Good luck,
Minh.

-----Original Message-----
From: Kirk Gomez [mailto:hed2hed@c...] 
Sent: Saturday, May 11, 2002 4:18 AM
To: Website Programming with ASP.NET
Subject: [aspdotnet_website_programming] Losing Variable Values in Code
Behind After Page Renders


Code Behind follows at end of message...

The initial page renders CORRECTLY.  Meaning that "FLeagueID",
"FromDate", 
and "ToDate" are initally populated correctly.

However, when the "DateRange_Changed" function is fired based on an
event 
in another control ("swamiDateControl") the values 
to "FLeagueID", "FromDate", and "ToDate" are all reset back to their 
original state (NULL basically).

Is this normal behavior?  Shouldn't those values be retained and 
referenceable for the life of the object?  I would like for the dates to

be what is current when the event fires.  The work around of course
would 
be to store in invisible asp:labels but this is a work around in my 
opinion.  I want to do it the correct way!

This is killing me.  Any help would be very much appreciated.

Kirk Gomez
hed2hed@c...

The following is the code behind class def to a Custom User control
which 
inherits "User Control":

Public Class PressReleases: Inherits FLeagueUserControl

  'To Be Set By User
  Public Dim FromDate      as Datetime = Datetime.Now.Date
  Public Dim ToDate        as Datetime = FromDate

  'Buttons
  Public Dim btnCreate     as LinkButton

  'Labels
  Public Dim lblMsg        as Label

  'Repeaters
  Public Dim dbRS          as Repeater

  'Swami
  Public Dim titleFLeague  as FLeagueUserControl
  Public Dim swamiViewBets as FLeagueUserControl
  Public Dim swamiDateControl as DateRangeControl

  Sub Page_Load( s as Object, e as EventArgs )
    if not isPostBack then
      FLeagueID = SessionFLeagueID
      if not Request.QueryString( "FromDate" ) = "" then
        FromDate = Request.QueryString( "FromDate" )
      else
        'FromDate = CurrentPeriodBegin
      end if
      if not Request.QueryString( "ToDate" )   = "" then
        ToDate = Request.QueryString( "ToDate" )
      else
        'ToDate = CurrentPeriodEnd
      end if
      swamiDateControl.DisplayedFromDate = FromDate
      swamiDateControl.DisplayedToDate   = ToDate
      BindData
    end if
  End Sub

  Sub BindData
    Dim dbConnection As sqlConnection
    Dim dbCommand    As sqlCommand
    Dim sql          As String

    titleFLeague.DataBind

    swamiViewBets.DataBind
    btnCreate.DataBind

    dbConnection = New sqlConnection(ConfigurationSettings.AppSettings
("Sql_DSN"))
    dbConnection.Open()
    sql = "SELECT PressReleaseID " & _
          "  FROM tblPressReleases " & _
          " WHERE FSeasonID = " & SessionFSeasonID & _
          "   AND ( PublishedInd = -1 OR CustomerID = " & 
SessionCustomerID & " ) " & _
          "   AND PostDate >= '" & FromDate.ToString() & "'" & _
          "   AND PostDate < '" & ToDate.AddDays( 1 ).ToString() & "'" &
_
          " ORDER BY PostDate DESC , PressReleaseID DESC "

    dbCommand = New SqlCommand( sql, dbConnection )
    dbRS.DataSource = dbCommand.ExecuteReader()
    dbRS.DataBind()
    dbConnection.Close
  End Sub

  Sub btnCreate_Click( s as Object, e as EventArgs )
    Response.Redirect( "EditPressRelease.aspx" )
  End Sub

  Sub DateRange_Changed( s as Object, e as DateRangeEventArgs )
    'Response.Redirect( String.Format( "PressReleases.aspx?FromDate={0}
&ToDate={1}", e.DisplayedFromDate.Date.ToString(), 
e.DisplayedToDate.Date.ToString() ) )
'FLeagueID should already be defined and remembered by the instantiated 
object!  But it's not!
lblMsg.Text = FromDate.ToString() & " -- " & ToDate.ToString()
    'FromDate = e.AbsoluteFromDate
    'ToDate   = e.AbsoluteToDate
    'BindData
  End Sub
End Class


Message #3 by "Kirk Gomez" <hed2hed@c...> on Tue, 13 Nov 2001 09:03:41 -0600
Thx Minh.  I thought I was losing my mind.

-----Original Message-----
From: Minh T. Nguyen [mailto:nguyentriminh@y...]
Sent: Friday, May 10, 2002 11:01 PM
To: Website Programming with ASP.NET
Subject: [aspdotnet_website_programming] RE: Losing Variable Values in Code
Behind After Page Renders

Kirk,

        This is normal behavior. Since HTTP is a stateless protocol
these values are only alive during the execution of your ASP.NET page,
since next time when the user fires another event all these variables
are lost again. You have several alternatives:

        You can store your variables in the Session object which is a
repository of name-value pairs that reside on the server for each
session. You can store variables in the Application object which is a
repository of name-value pairs that reside on the server for all
sessions. You can store it in the viewstate, which is a repository that
is stored on the client side (and is in fact implemented as you have
suggested, namely hidden HTML controls). At last, you can store these
values in client-side cookies.

        I think for you, the Session object is probably the most
appropriate.

Good luck,
Minh.

-----Original Message-----
From: Kirk Gomez [mailto:hed2hed@c...]
Sent: Saturday, May 11, 2002 4:18 AM
To: Website Programming with ASP.NET
Subject: [aspdotnet_website_programming] Losing Variable Values in Code
Behind After Page Renders


Code Behind follows at end of message...

The initial page renders CORRECTLY.  Meaning that "FLeagueID",
"FromDate",
and "ToDate" are initally populated correctly.

However, when the "DateRange_Changed" function is fired based on an
event
in another control ("swamiDateControl") the values
to "FLeagueID", "FromDate", and "ToDate" are all reset back to their
original state (NULL basically).

Is this normal behavior?  Shouldn't those values be retained and
referenceable for the life of the object?  I would like for the dates to

be what is current when the event fires.  The work around of course
would
be to store in invisible asp:labels but this is a work around in my
opinion.  I want to do it the correct way!

This is killing me.  Any help would be very much appreciated.

Kirk Gomez
hed2hed@c...

The following is the code behind class def to a Custom User control
which
inherits "User Control":

Public Class PressReleases: Inherits FLeagueUserControl

  'To Be Set By User
  Public Dim FromDate      as Datetime = Datetime.Now.Date
  Public Dim ToDate        as Datetime = FromDate

  'Buttons
  Public Dim btnCreate     as LinkButton

  'Labels
  Public Dim lblMsg        as Label

  'Repeaters
  Public Dim dbRS          as Repeater

  'Swami
  Public Dim titleFLeague  as FLeagueUserControl
  Public Dim swamiViewBets as FLeagueUserControl
  Public Dim swamiDateControl as DateRangeControl

  Sub Page_Load( s as Object, e as EventArgs )
    if not isPostBack then
      FLeagueID = SessionFLeagueID
      if not Request.QueryString( "FromDate" ) = "" then
        FromDate = Request.QueryString( "FromDate" )
      else
        'FromDate = CurrentPeriodBegin
      end if
      if not Request.QueryString( "ToDate" )   = "" then
        ToDate = Request.QueryString( "ToDate" )
      else
        'ToDate = CurrentPeriodEnd
      end if
      swamiDateControl.DisplayedFromDate = FromDate
      swamiDateControl.DisplayedToDate   = ToDate
      BindData
    end if
  End Sub

  Sub BindData
    Dim dbConnection As sqlConnection
    Dim dbCommand    As sqlCommand
    Dim sql          As String

    titleFLeague.DataBind

    swamiViewBets.DataBind
    btnCreate.DataBind

    dbConnection = New sqlConnection(ConfigurationSettings.AppSettings
("Sql_DSN"))
    dbConnection.Open()
    sql = "SELECT PressReleaseID " & _
          "  FROM tblPressReleases " & _
          " WHERE FSeasonID = " & SessionFSeasonID & _
          "   AND ( PublishedInd = -1 OR CustomerID = " &
SessionCustomerID & " ) " & _
          "   AND PostDate >= '" & FromDate.ToString() & "'" & _
          "   AND PostDate < '" & ToDate.AddDays( 1 ).ToString() & "'" &
_
          " ORDER BY PostDate DESC , PressReleaseID DESC "

    dbCommand = New SqlCommand( sql, dbConnection )
    dbRS.DataSource = dbCommand.ExecuteReader()
    dbRS.DataBind()
    dbConnection.Close
  End Sub

  Sub btnCreate_Click( s as Object, e as EventArgs )
    Response.Redirect( "EditPressRelease.aspx" )
  End Sub

  Sub DateRange_Changed( s as Object, e as DateRangeEventArgs )
    'Response.Redirect( String.Format( "PressReleases.aspx?FromDate={0}
&ToDate={1}", e.DisplayedFromDate.Date.ToString(),
e.DisplayedToDate.Date.ToString() ) )
'FLeagueID should already be defined and remembered by the instantiated
object!  But it's not!
lblMsg.Text = FromDate.ToString() & " -- " & ToDate.ToString()
    'FromDate = e.AbsoluteFromDate
    'ToDate   = e.AbsoluteToDate
    'BindData
  End Sub
End Class




Message #4 by "Lewis" <lewis@t...> on Mon, 13 May 2002 14:35:02 -0600
Have you tried the ispostback question to stop the variables from being
cleared?


-----Original Message-----
From: Kirk Gomez [mailto:hed2hed@c...]
Sent: Saturday, May 11, 2002 4:18 AM
To: Website Programming with ASP.NET
Subject: [aspdotnet_website_programming] Losing Variable Values in Code
Behind After Page Renders


Code Behind follows at end of message...

The initial page renders CORRECTLY.  Meaning that "FLeagueID", "FromDate",
and "ToDate" are initally populated correctly.

However, when the "DateRange_Changed" function is fired based on an event
in another control ("swamiDateControl") the values
to "FLeagueID", "FromDate", and "ToDate" are all reset back to their
original state (NULL basically).

Is this normal behavior?  Shouldn't those values be retained and
referenceable for the life of the object?  I would like for the dates to
be what is current when the event fires.  The work around of course would
be to store in invisible asp:labels but this is a work around in my
opinion.  I want to do it the correct way!

This is killing me.  Any help would be very much appreciated.

Kirk Gomez
hed2hed@c...

The following is the code behind class def to a Custom User control which
inherits "User Control":

Public Class PressReleases: Inherits FLeagueUserControl

  'To Be Set By User
  Public Dim FromDate      as Datetime = Datetime.Now.Date
  Public Dim ToDate        as Datetime = FromDate

  'Buttons
  Public Dim btnCreate     as LinkButton

  'Labels
  Public Dim lblMsg        as Label

  'Repeaters
  Public Dim dbRS          as Repeater

  'Swami
  Public Dim titleFLeague  as FLeagueUserControl
  Public Dim swamiViewBets as FLeagueUserControl
  Public Dim swamiDateControl as DateRangeControl

  Sub Page_Load( s as Object, e as EventArgs )
    if not isPostBack then
      FLeagueID = SessionFLeagueID
      if not Request.QueryString( "FromDate" ) = "" then
        FromDate = Request.QueryString( "FromDate" )
      else
        'FromDate = CurrentPeriodBegin
      end if
      if not Request.QueryString( "ToDate" )   = "" then
        ToDate = Request.QueryString( "ToDate" )
      else
        'ToDate = CurrentPeriodEnd
      end if
      swamiDateControl.DisplayedFromDate = FromDate
      swamiDateControl.DisplayedToDate   = ToDate
      BindData
    end if
  End Sub

  Sub BindData
    Dim dbConnection As sqlConnection
    Dim dbCommand    As sqlCommand
    Dim sql          As String

    titleFLeague.DataBind

    swamiViewBets.DataBind
    btnCreate.DataBind

    dbConnection = New sqlConnection(ConfigurationSettings.AppSettings
("Sql_DSN"))
    dbConnection.Open()
    sql = "SELECT PressReleaseID " & _
          "  FROM tblPressReleases " & _
          " WHERE FSeasonID = " & SessionFSeasonID & _
          "   AND ( PublishedInd = -1 OR CustomerID = " &
SessionCustomerID & " ) " & _
          "   AND PostDate >= '" & FromDate.ToString() & "'" & _
          "   AND PostDate < '" & ToDate.AddDays( 1 ).ToString() & "'" & _
          " ORDER BY PostDate DESC , PressReleaseID DESC "

    dbCommand = New SqlCommand( sql, dbConnection )
    dbRS.DataSource = dbCommand.ExecuteReader()
    dbRS.DataBind()
    dbConnection.Close
  End Sub

  Sub btnCreate_Click( s as Object, e as EventArgs )
    Response.Redirect( "EditPressRelease.aspx" )
  End Sub

  Sub DateRange_Changed( s as Object, e as DateRangeEventArgs )
    'Response.Redirect( String.Format( "PressReleases.aspx?FromDate={0}
&ToDate={1}", e.DisplayedFromDate.Date.ToString(),
e.DisplayedToDate.Date.ToString() ) )
'FLeagueID should already be defined and remembered by the instantiated
object!  But it's not!
lblMsg.Text = FromDate.ToString() & " -- " & ToDate.ToString()
    'FromDate = e.AbsoluteFromDate
    'ToDate   = e.AbsoluteToDate
    'BindData
  End Sub
End Class



  Return to Index