locked
Object Scope RRS feed

  • Question

  • User681854982 posted

    If I declare my object at the beggining of my page class, and instantiate it in a dropdownlist selected_index change event.  Shouldn't this object be available thru out the entire page and also persist thru a postback ?  I lose the object in my selected date change event in my calendar.  Any ideas?

     

    Imports recordRequestDB
    Imports MiscFunctions
    Imports thisuser
    Imports System.Collections.Generic
    Imports System.Data
    Imports System.Data.SqlClient
    Imports Employee
    
    
    
    Partial Class Admin_request_a
        Inherits System.Web.UI.Page
    
    
        Dim oEmployee As Employee
        Dim myDates As New List(Of Date)
        Dim blnAllOrNothing As Boolean = False
        Dim intThisEmployee As Integer
    
        Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
    
            
        End Sub
    
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
            If IsPostBack Then
    
                'I have tried with and without this line
                oEmployee = CType(Session("thisEmployee"), Employee)
    
    
                MasterPageErrorMessage()
    
                'Check if an employee is selected.  We cannot continue, if an employee is not selected
                If DropDownList1.SelectedIndex = 0 Then
    
                    Session("MyErrorLabelString") = "You Must Choose An Employee From The List"
    
    
    
                    'Response.Write("You Must Select An Employee")
                    Exit Sub
                End If
    
    
    
    
                intThisEmployee = CInt(DropDownList1.SelectedValue)
    
    
            End If
    
    
    
            If Not IsPostBack Then
    
    
    
                Session("theDates") = myDates
                Calendar1.SelectedDate = Today
                Calendar1.SelectedDate = Nothing
    
            Else
                myDates = CType(Session("theDates"), List(Of Date))
            End If
       Calendar1.SelectedDate = Nothing
    
    
        End Sub
    
    
    
    Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
    
    
            oEmployee = New Employee(DropDownList1.SelectedValue)
            Session("thisEmployee") = oEmployee
       
    
        End Sub
    
    
        Public Sub Calendar1_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Calendar1.SelectionChanged
    
            'Check if an employee is selected.  We cannot continue, if an employee is not selected
            If DropDownList1.SelectedIndex = 0 Then
    
                MasterPageErrorMessage()
    
                'Dim lblErrorM As Label
                'lblErrorM = CType(Master.FindControl("lblErrorMessage"), Label)
                'lblErrorM.Text = "You Must Select An Employee"
    
                Exit Sub
            End If
          
            'We need to check if employee is a shift 11,12,13,14
            'If they are we can skip Is Sat or Sun Chosen
    
            'Check if any of the dates selected are Sat or Sun and if so remove
    
            'If CheckVacationDaysSelectedQty(myDates.Count, oEmployee.ClockNo) = False Then
            If myDates.Count > oEmployee.VacationDaysRemaining Then
                lblTooManyDays.Visible = True
                'Response.Write("<h3 style='font-color: red:'>You are trying to select more vacation days than you have available</h3>")
                Exit Sub
    
            End If
    
            'Added 07/26/2010
            RemoveDatesFromNextYear()
            RemoveSaturdaysAndSundays()
            'RemovePastDates()
            RemoveHolidays()
    
    
    
            WriteDatesToHtml()
            'Response.Write(myDates.Count.ToString)
    
            If myDates.Count > 1 Then
                RadioButtonList1.Enabled = True
                RadioButtonList1.Items.Item(0).Enabled = True
                RadioButtonList1.Items.Item(1).Enabled = True
    
            End If
    
            If myDates.Count > 2 Then
                cbxAdvance.Enabled = True
                lblAdvance.Enabled = True
            Else : cbxAdvance.Enabled = False
                lblAdvance.Enabled = False
            End If
    
            CheckIfFridayOrMonday()
    
    
        End Sub
    
    End Class


     

    Monday, October 18, 2010 12:10 PM

Answers

  • User-231977777 posted

    hi

    declare  your object as shared object :

    Shared Strvariable As String = ""
    

    hope this helps

    Ahmed
     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 18, 2010 3:52 PM
  • User849259894 posted

    You have to understand that web is a stateless architecture.  Pages are created on the server (objects, controls etc), sent to the client and destroyed.  So each time (post back or not) page is created and destroyed.  You need to understand the page life cycle to determine when the objects should be created.  Following article goes in detail about page life cycle

    http://www.dotnetheaven.com/UploadFile/ShanthiM/ASPNETPageLifeCycle03292006041330AM/ASPNETPageLifeCycle.aspx 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 18, 2010 4:00 PM
  • User-952121411 posted

    Here is a response I typed up on this VB.NET forum regarding an identical question.  This is a common question like yours that typically comes from developers used to designing thick client Windows Forms applications and then move over to the world of web apps and ASP.NET.  Read through it for a good explanation of the web being stateless and techniques to persist your objects as you will need to do:

    http://forums.asp.net/p/1476789/3435889.aspx#3435862

    Hope this helps! Smile

     

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 18, 2010 4:29 PM

All replies

  • User-231977777 posted

    hi

    declare  your object as shared object :

    Shared Strvariable As String = ""
    

    hope this helps

    Ahmed
     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 18, 2010 3:52 PM
  • User849259894 posted

    You have to understand that web is a stateless architecture.  Pages are created on the server (objects, controls etc), sent to the client and destroyed.  So each time (post back or not) page is created and destroyed.  You need to understand the page life cycle to determine when the objects should be created.  Following article goes in detail about page life cycle

    http://www.dotnetheaven.com/UploadFile/ShanthiM/ASPNETPageLifeCycle03292006041330AM/ASPNETPageLifeCycle.aspx 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 18, 2010 4:00 PM
  • User-952121411 posted

    Here is a response I typed up on this VB.NET forum regarding an identical question.  This is a common question like yours that typically comes from developers used to designing thick client Windows Forms applications and then move over to the world of web apps and ASP.NET.  Read through it for a good explanation of the web being stateless and techniques to persist your objects as you will need to do:

    http://forums.asp.net/p/1476789/3435889.aspx#3435862

    Hope this helps! Smile

     

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 18, 2010 4:29 PM