2011-10-30 20 views
0

何らかの理由で私のコードは顧客をセッションに追加していません。私はASP.NETに慣れていないので、なぜこれが起こっているのか、おそらくいくつかのコードの例を与えることができるように誰もが入力を持っています。ありがとう!顧客データがセッションに渡されたり保存されたりしていませんか?

コードの下にあります。

Imports System.Data 

Partial Class _Default 
    Inherits System.Web.UI.Page 

    Private SelectedCustomer As Customer 

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) _ 
      Handles Me.Load 
     If Not IsPostBack Then 
      ddlCustomers.DataBind() 
     End If 
     SelectedCustomer = Me.GetSelectedCustomer 
     Me.DisplayCustomer() 
    End Sub 

    Private Function GetSelectedCustomer() As Customer 
     Dim dvTable As dataview = CType(AccessDataSource1.Select(_ 
      DataSourceSelectArguments.Empty), dataview) 
     dvTable.RowFilter = "CustomerID = " & ddlCustomers.SelectedValue 
     Dim drvRow As DataRowView = dvTable(0) 

     Dim customer As New Customer 
     customer.CustomerID = CInt(drvRow("CustomerID")) 
     customer.Name = drvRow("Name").ToString 
     customer.Address = drvRow("Address").ToString 
     customer.City = drvRow("City").ToString 
     customer.State = drvRow("State").ToString 
     customer.ZipCode = drvRow("ZipCode").ToString 
     customer.Phone = drvRow("Phone").ToString 
     customer.Email = drvRow("Email").ToString 
     Return customer 
    End Function 

    Private Sub DisplayCustomer() 
     lblAddress.Text = SelectedCustomer.Address 
     lblCityStateZip.Text = SelectedCustomer.City & ", " _ 
          & SelectedCustomer.State & " " _ 
          & SelectedCustomer.ZipCode 
     lblPhone.Text = SelectedCustomer.Phone 
     lblEmail.Text = SelectedCustomer.Email 
    End Sub 


    Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAdd.Click 
     If Page.IsValid Then 
      Dim customerItem As New Customer 
      customerItem.Name = SelectedCustomer.ToString 
      Me.AddToCustomer(customerItem) 
      Response.Redirect("CustomerList.aspx") 
     End If 
    End Sub 

    Private Sub AddToCustomer(ByVal customerItem As Customer) 
     Dim customer As SortedList = Me.GetCustomer 
     Dim customerID As String = SelectedCustomer.CustomerID 
     If customer.ContainsKey(customerID) Then 
      customerItem = CType(customer(customerID), Customer) 
     Else 
      customer.Add(customerID, customerItem) 
     End If 
    End Sub 

    Private Function GetCustomer() As SortedList 
     If Session("Customer") Is Nothing Then 
      Session.Add("Customer", New SortedList) 
     End If 
     Return CType(Session("Customer"), SortedList) 
    End Function 

End Class 

次のコードの2ページ目:

Partial Class Default2 
    Inherits System.Web.UI.Page 
    Private Customer As SortedList 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 
     Customer = Me.GetCustomer 
     If Not IsPostBack Then Me.DisplayCustomer() 

    End Sub 
    Private Function GetCustomer() As SortedList 
     If Session("Customer") Is Nothing Then 
      Session.Add("Customer", New SortedList) 
     End If 
     Return CType(Session("Customer"), SortedList) 
    End Function 
    Private Sub DisplayCustomer() 
     lstCustomer.Items.Clear() 
     Dim customerItem As Customer 
     For Each customerEntry As DictionaryEntry In Customer 
      customerItem = CType(customerEntry.Value, Customer) 
      lstCustomer.Items.Add(customerItem.Name) 
     Next 
    End Sub 

    Protected Sub lstCustomer_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstCustomer.SelectedIndexChanged 

    End Sub 

    Protected Sub Clearbtn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Clearbtn.Click 
     If Customer.Count > 0 Then 
      Customer.Clear() 
      lstCustomer.Items.Clear() 
     Else 
      clrErr.Text = "ERROR: NO CUSTOMERS STORED" 
     End If 
    End Sub 
End Class 

答えて

1

は私は実際にあなたが実際に更新され、顧客のオブジェクトを格納するための行を追加する必要があり、あなたの質問

の他に、このに答えたと思いますセッションに戻す例:

Private Sub AddToCustomer(ByVal customerItem As Customer) 
    Dim customer As SortedList = Me.GetCustomer 
    Dim customerID As String = SelectedCustomer.CustomerID 
    If customer.ContainsKey(customerID) Then 
     customerItem = CType(customer(customerID), Customer) 
    Else 
     customer.Add(customerID, customerItem) 
    End If 
    Session("Customer") = customer 
End Sub 

私はこれを別のあなたが後でそれを変更したい場合、あなたのデザインはSessionに依存しません。あなたが後にリダイレクトを行うように、より多くの瞬間

Private Sub AddToCustomer(ByVal customerItem As Customer) 
    Dim customer As SortedList = Me.GetCustomer 
    Dim customerID As String = SelectedCustomer.CustomerID 
    If customer.ContainsKey(customerID) Then 
     customerItem = CType(customer(customerID), Customer) 
    Else 
     customer.Add(customerID, customerItem) 
    End If 
    StoreCustomer(customer) 
End Sub 

Private Sub StoreCustomer(ByVal customer As SortedList) 
    Session("Customer") = customer 
End Sub 

のように顧客にすべてのローカル変更は単にコールトトAddToCustomer後に失われます。

Me.AddToCustomer(customerItem) 
Response.Redirect("CustomerList.aspx") 
関連する問題