1

I am new to asp.net and i am trying to solve a problem.

I have created a simple aspx page (asp web site) that references a vb.net class. I am handling one class instance using the session context object (don't know if there is a better way). The class has a sub that sets a string value and a function that returns it.

I compile and run the web site project and then set the value "1" from one aspx page and the value "2" from another page (i open a second tab or browser by copying-paste the url from the first page) and then retrieve the values, both pages will show "2".

The same class in a vb.net form application (.exe) works just fine when the exe instances are running, the first returns the value "1" and the second the value "2". This is how i want it to work in my web site project, different pages different dll instances.

Class:
Public Class Class1

    Private sExten As String

    Public Sub setExten(value As String)
        sExten = value
    End Sub

    Public Function getExten() As String
        Return sExten
    End Function

End Class

aspx:
Partial Class _Default
    Inherits System.Web.UI.Page

    'trying to ensure one instance is running
    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            Session.Add("ClassLibrary1", New ClassLibrary1.Class1)
        End If
    End Sub

    'txtSetValue.text contains value "1" or "2"
    Protected Sub btnSet_Click(sender As Object, e As EventArgs) Handles btnSet.Click
        CType(Session.Item("ClassLibrary1"), ClassLibrary1.Class1).setExten(txtSetValue.text)
    End Sub

    'the txtShowValue shows "1" in the first and "2" in the second page
    Protected Sub BtnGet_Click(sender As Object, e As EventArgs) Handles BtnGet.Click
        txtShowValue.Text = CType(Session.Item("ClassLibrary1"), ClassLibrary1.Class1).getExten()
    End Sub

End Class
Spyros
  • 13
  • 4
  • You are overwriting session value in Tab2 with 2 so it is expected to return 2 and not 1. If you want to keep them separate take a look at this [SO Solution](http://stackoverflow.com/a/2844472/125551) – rs. Mar 14 '13 at 21:59

1 Answers1

0

Both pages are sharing the same Session.Item("ClassLibrary1"). You can try to store the value in a hidden field, or a invisible label.

Final Form
  • 318
  • 5
  • 12
  • Information: That is correct. But the same problem appeared in another .net dll (more complex) even thought the session items were correctly named. We find that the cause of the conflict was detected in variables declared inside vb.net modules (or c# static classes). By moving the variables in classes (non static) fixed the problem. – Spyros Mar 15 '13 at 22:35