2011-10-30 45 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 

下面的代碼第二頁:

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 

目前客戶的所有本地更改只是通話TOTO AddToCustomer丟失後,你再經過做一個重定向。

Me.AddToCustomer(customerItem) 
Response.Redirect("CustomerList.aspx")