使用ASP.NET 2.0 Profile存储用户信息[翻译] Level 200

本文深入探讨了ASP.NET 2.0中Profile对象的应用,展示了如何使用Profile对象来跟踪用户属性,创建购物篮等。文章还讲解了如何定义Profile,使用Profile组及复杂的Profile属性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

作者: Stephen Walther
原文地址:http://msdn.microsoft.com/asp.net/default.aspx?pull=/library/en-us/dnvs05/html/UserProfiles.asp
译者:Tony Qu

概要:许多ASP.NET应用程序需要跨访问的用户属性跟踪功能,在ASP.NET1.1中,我们只能人工实现这一功能。但如今,使用 ASP.NET 2.0的Profile对象,这个过程变得异常简单。Stephen Walther将验证该对象,并向你展示如何使用Profile来跟踪用户属性、创建一个购物篮,及其他一些例子。

总目录
介绍
User Profile总揽
定义User Profile
使用Profile组
使用复杂的Profile属性
继承一个Profile
迁移匿名Profile设置
配置Profile Provider
管理Profiles并生成Profile报告
总结
相关书籍

Microsoft ASP.NET 2.0支持被称为Profile的新对象,它可以自动在多个Web应用程序的访问之间存储用户信息。一个User Profile中可以存储各种类型的信息,这些信息既可以是简单的string和integer类型,也可以是复杂的自定义类型。例如,你可以存储用户的姓、购物篮、用户属性或网站使用情况统计。
本文中,你将学习如何在一个应用中定义user profile。我们也会向你演示如何配置使用不同provider的profile。最后,你将学习如何管理和生成user profile的报告。

User Profiles总揽
Profile 对象与Session对象十分相似,但是更好用一些。与Session相似的地方在于,Profile是相对于一个特定的用户的,也就是说,每个Web应用程序的用户都有他们自己的profile对象。与Session不同的是,Profile对象是持久对象。如果你向Session中添加一个项,在你离开网站时,该项就会消失。而Profile则完全不同,当你修改Profile的状态时,修改在多个访问之间均有效。

profile使用provider模式来存储信息,默认情况下,user profile的内容会保存在SQL Server Express数据库中,该数据库位于网站的App_Data目录。然而,在本文的后半部分,你将了解如何使用其他数据提供者(data provider)来存储信息,如完整版的SQL Server中的一个数据库或者一个Oracle数据库。

与Session不同,Profile是强类型的,Session对象仅仅是一个项集合而已,而profile对象则有强类型属性。
使用强类型是有它的道理的。例如,使用强类型,你就可以在Microsoft Visual Web Developer中使用智能感知技术,当你键入Profile和一个点的时候,智能感知会弹出你已经定义过的profile属性列表。

 

定义user profile
你既可以在machine.config中,也可以在web.config中定义一个user profile,由于你不能在应用程序的二级目录中创建一个包含文件profile节的web.config文件,这意味着你将无法在一个应用程序中定义两个以上的profile。
在列表1的web.config中,列举了一个简单的profile定义的实例,该profile有三个属性,FirstName, LastName和PageVisits。

None.gif 表1
None.gif
< configuration >
None.gif 
< system .web >
None.gif   
< authentication  mode ="Forms"   />
None.gif      
None.gif           
< anonymousIdentification  enabled ="true"   />
None.gif        
None.gif   
< profile >
None.gif               
< properties >
None.gif                  
< add 
None.gif        
name ="FirstName"   
None.gif        defaultValue
="??"
None.gif        allowAnonymous
="true"   />
None.gif      
< add 
None.gif        
name ="LastName"  
None.gif        defaultValue
="??"
None.gif        allowAnonymous
="true"   />
None.gif      
< add 
None.gif        
name ="PageVisits"
None.gif        type
="Int32"  
None.gif        allowAnonymous
="true" />
None.gif               
</ properties >
None.gif           
</ profile >
None.gif 
</ system.web >
None.gif
</ configuration >
由于该profile需要同时被匿名用户和已认证用户使用,因此我们在web.config文件中增加包含一个< anonymousIdentification>元素,有了这个元素,系统就会自动为匿名用户生成唯一的ID。仔细看的话我们会发现,每一个 profile属性都有一个allowAnonymous特性,该特性表明这个profile属性是否允许被匿名用户使用。

 

    默认的profile属性类型是System.String类型。列表1中,由于没有为FirstName和LastName这两个profile属性增加type特性,那么系统默认它们是string类型,而PageVisits属性则指定了type特性为Int32,因此该profile属性可用于表示一个整型值。

    最后,注意FirstName和LastName属性都有defaultValue特性。你可以为简单的数据类型设置defaultValue特性,但你不能为复杂类型设置defaultValue特性。
 
    当你定义好一个profile之后,系统会自动在下一次页面被调用时,生成一个与该profile相对应的类。这个类会被保存在"Temporary ASP.NET Files Directory"目录(该目录也用于存放用于动态生成页面的类)。你可以使用HttpContext的Profile属性(Property)调用该类。
    当你定义好一个profile后,你可以使用如下方法为profile属性赋值。

[Visual Basic .NET]
Profile.FirstName 
=   " Bill "

[C#]
Profile.FirstName 
=   " Bill " ;

 

任何在web.config中定义的profile属性都会在Profile对象中呈现。
列表2演示了你该如何使用profile来持久化保存用户信息。这个页显示了FirstName,LastName, PageVisits三个属性的值,同时它包含了一个能够用于修改这三个属性的表单(form)。在Page_Load中更新PageVisits的值,这意味着每一次刷新页面,PageVisits的值都会改变。

o_userprofiles_fig01.gif

图1 使用简单的profile

None.gif 列表  2 . Simple.aspx (Visual Basic .NET)
None.gif
< %@ Page Language = " VB "  % >
None.gif
< script runat = " server " >
None.gif
ExpandedBlockStart.gifContractedBlock.gif    
Sub Page_Load() Sub Page_Load()
InBlock.gif        Profile.PageVisits 
+= 1
ExpandedBlockEnd.gif    
End Sub

None.gif    
ExpandedBlockStart.gifContractedBlock.gif    
Sub UpdateProfile() Sub UpdateProfile(ByVal s As ObjectByVal e As EventArgs)
InBlock.gif        Profile.FirstName 
= txtFirstName.Text
InBlock.gif        Profile.LastName 
= txtLastName.Text
ExpandedBlockEnd.gif    
End Sub

None.gif    
None.gif
</ script >
None.gif
None.gif
< html >
None.gif
< head >
None.gif    
< title > Simple </ title >
None.gif
</ head >
None.gif
< body >
None.gif    
< form id = " form1 "  runat = " server " >
None.gif    
< b > Name: </ b >   < % =  Profile.FirstName % >   < % =  Profile.LastName % >
None.gif    
< br  />
None.gif    
< b > Page Visits: </ b >   < % =  Profile.PageVisits % >
None.gif    
None.gif    
< hr  />
None.gif    
None.gif    
< b > First Name: </ b >
None.gif    
< asp:TextBox ID = " txtFirstName "  Runat = " Server "   />
None.gif    
< br  />
None.gif    
< b > Last Name: </ b >
None.gif    
< asp:TextBox ID = " txtLastName "  Runat = " Server "   />
None.gif    
< br  />
None.gif    
< asp:Button 
None.gif        Text
= " Update Profile "  
None.gif        OnClick
= " UpdateProfile "  
None.gif        Runat
= " server "   />
None.gif
None.gif    
</ form >
None.gif
</ body >
None.gif
</ html >

 

 

None.gif 列表  2 . Simple.aspx (C#)
None.gif
<% @ Page Language = " C# "   %>
None.gif
< script runat = " server " >
None.gif
ExpandedBlockStart.gifContractedBlock.gif    
void  Page_Load()  dot.gif {
InBlock.gif        Profile.PageVisits 
++;
ExpandedBlockEnd.gif    }

None.gif    
ExpandedBlockStart.gifContractedBlock.gif    
void  UpdateProfile(Object s, EventArgs e)  dot.gif {
InBlock.gif        Profile.FirstName 
= txtFirstName.Text;
InBlock.gif        Profile.LastName 
= txtLastName.Text;
ExpandedBlockEnd.gif    }

None.gif    
None.gif
</ script >
None.gif
None.gif
< html >
None.gif
< head >
None.gif    
< title > Simple </ title >
None.gif
</ head >
None.gif
< body >
None.gif    
< form id = " form1 "  runat = " server " >
None.gif    
< b > Name: </ b >   <%=  Profile.FirstName  %>   <%=  Profile.LastName  %>
None.gif    
< br  />
None.gif    
< b > Page Visits: </ b >   <%=  Profile.PageVisits  %>
None.gif    
None.gif    
< hr  />
None.gif    
None.gif    
< b > First Name: </ b >
None.gif    
< asp:TextBox ID = " txtFirstName "  Runat = " Server "   />
None.gif    
< br  />
None.gif    
< b > Last Name: </ b >
None.gif    
< asp:TextBox ID = " txtLastName "  Runat = " Server "   />
None.gif    
< br  />
None.gif    
< asp:Button ID = " Button1 "  
None.gif        Text
= " Update Profile "  
None.gif        OnClick
= " UpdateProfile "  
None.gif        Runat
= " server "   />
None.gif
None.gif    
</ form >
None.gif
</ body >
None.gif
</ html >

如果你多次访问列表2中的页面,你会注意到PageVisits在不断增大。如果你关闭的浏览器,并在一周之后调用该页面,PageVisits属性仍然会保留原值。从这一点可以看出Profile为每个用户自动保存一个副本。

使用Profile组

尽管你仅可以为一个应用程序定义一个profile,但如果你需要让几个profile属性一起工作,把它们放在组中,会让你觉得它们更易管理。

例如,在列表3中,有一个带有两个组的profile,这两个组分别是AddressPreferences

None.gif 列表3. Web.Config
None.gif
< configuration >
None.gif
< system .web >
None.gif      
None.gif   
< anonymousIdentification  enabled ="true"   />
None.gif        
None.gif   
< profile >
None.gif               
< properties >
None.gif   
< group  name ="Address" >
None.gif                  
< add 
None.gif         
name ="Street"   
None.gif         allowAnonymous
="true"   />
None.gif                 
< add 
None.gif         
name ="City"   
None.gif         allowAnonymous
="true"   />
None.gif   
</ group >
None.gif   
< group  name ="Preferences" >
None.gif      
< add 
None.gif         
name ="ReceiveNewsletter"  
None.gif         type
="Boolean"
None.gif         defaultValue
="false"
None.gif         allowAnonymous
="true"   />
None.gif   
</ group >
None.gif              
</ properties >
None.gif        
</ profile >
None.gif
</ system.web >
None.gif
</ configuration >

当你用组来定义 profile 时,你应该使用组名来设置或读取 profile 属性。例如,在列表 3 中,你可以使用以下一些句子来完成三个 profile 属性的赋值。

[Visual Basic .NET]

Profile.Address.City 
=   " Modesto "
Profile.Address.Street 
=   " 111 King Arthur Ln "
Profile.Preferences.ReceiveNewsletter 
=  False

[C#]

Profile.Address.City 
=   " Modesto " ;
Profile.Address.Street 
=   " 111 King Arthur Ln " ;
Profile.Preferences.ReceiveNewsletter 
=   false ;

一个profile的定义只能包含一层组,换句话说,你不能把其他的组放在一个profile组的下面一层。

使用复杂的profile属性

到目前为止,我们已经介绍了声明包含简单类型(如string或整型)属性的profile,其实你也可以在profile中声明复杂属性。
举个例子,假设你现在需要在profile中存储一个购物篮,如果这样做的话,你就可以在每次访问网站时获得自己的购物篮。
列表4 声明了一个包含profile,这个profile包含一个名为ShoppingCart的属性,而该属性的type特性是一个叫ShoppingCart的类(我们接下来会创建该类),该类名是有效的。
我们还会注意到,该声明中包含一个serializeAs特性,该特性可以帮助ShoppingCart使用二进制序列化器(binary serializer)进行持久化,而不是使用xml序列化器。

None.gif 列表4 Web.config
None.gif
None.gif
< configuration >
None.gif
< system .web >
None.gif
None.gif  
< anonymousIdentification  enabled ="true"   />
None.gif  
None.gif  
< profile >
None.gif    
< properties >
None.gif    
< add 
None.gif       
name ="ShoppingCart"
None.gif       type
="ShoppingCart"
None.gif       serializeAs
="Binary"
None.gif       allowAnonymous
="true"   />
None.gif    
</ properties >
None.gif  
</ profile >
None.gif
</ system.web >
None.gif
</ configuration >

列表5 中有一个简单购物篮的实现代码,该购物篮拥有添加和删除项(item)的方法(method),同时它还拥有两个属性(property),一个是用于获得该购物篮中的所有项的,一个是用于表示所有商品的总价的。

 

 

None.gif 列表5 ShoppingCart (Visual Basic.NET)
None.gif
None.gif
Imports  Microsoft.VisualBasic
None.gif
None.gif
< Serializable() >  _
ExpandedBlockStart.gifContractedBlock.gif
Public   Class ShoppingCart Class ShoppingCart
InBlock.gif    
Public _CartItems As New Hashtable()
InBlock.gif
InBlock.gif    
' Return all the items from the Shopping Cart
ExpandedSubBlockStart.gifContractedSubBlock.gif
    Public ReadOnly Property CartItems()Property CartItems() As ICollection
InBlock.gif        
Get
InBlock.gif            
Return _CartItems.Values
InBlock.gif        
End Get
ExpandedSubBlockEnd.gif    
End Property

InBlock.gif
InBlock.gif    
' The sum total of the prices
ExpandedSubBlockStart.gifContractedSubBlock.gif
    Public ReadOnly Property Total()Property Total() As Decimal
InBlock.gif        
Get
InBlock.gif            
Dim sum As Decimal
InBlock.gif            
For Each item As CartItem In _CartItems.Values
InBlock.gif                sum 
+= item.Price * item.Quantity
InBlock.gif            
Next
InBlock.gif            
Return sum
InBlock.gif        
End Get
ExpandedSubBlockEnd.gif    
End Property

InBlock.gif
InBlock.gif    
' Add a new item to the shopping cart
ExpandedSubBlockStart.gifContractedSubBlock.gif
    Public Sub AddItem()Sub AddItem(ByVal ID As Integer, _
InBlock.gif      
ByVal Name As StringByVal Price As Decimal)
InBlock.gif        
Dim item As CartItem = CType(_CartItems(ID), CartItem)
InBlock.gif        
If item Is Nothing Then
InBlock.gif            _CartItems.Add(ID, 
New CartItem(ID, Name, Price))
InBlock.gif        
Else
InBlock.gif            item.Quantity 
+= 1
InBlock.gif            _CartItems(ID) 
= item
InBlock.gif        
End If
ExpandedSubBlockEnd.gif    
End Sub

InBlock.gif
InBlock.gif    
' Remove an item from the shopping cart
ExpandedSubBlockStart.gifContractedSubBlock.gif
    Public Sub RemoveItem()Sub RemoveItem(ByVal ID As Integer)
InBlock.gif        
Dim item As CartItem = CType(_CartItems(ID), CartItem)
InBlock.gif        
If item Is Nothing Then
InBlock.gif            
Return
InBlock.gif        
End If
InBlock.gif        item.Quantity 
-= 1
InBlock.gif        
If item.Quantity = 0 Then
InBlock.gif            _CartItems.Remove(ID)
InBlock.gif        
Else
InBlock.gif            _CartItems(ID) 
= item
InBlock.gif        
End If
ExpandedSubBlockEnd.gif    
End Sub

InBlock.gif
ExpandedBlockEnd.gif
End Class

None.gif
None.gif
< Serializable() >  _
ExpandedBlockStart.gifContractedBlock.gif
Public   Class CartItem Class CartItem
InBlock.gif
InBlock.gif    
Private _ID As Integer
InBlock.gif    
Private _Name As String
InBlock.gif    
Private _Price As Decimal
InBlock.gif    
Private _Quantity As Integer = 1
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Public ReadOnly Property ID()Property ID() As Integer
InBlock.gif        
Get
InBlock.gif            
Return _ID
InBlock.gif        
End Get
ExpandedSubBlockEnd.gif    
End Property

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Public ReadOnly Property Name()Property Name() As String
InBlock.gif        
Get
InBlock.gif            
Return _Name
InBlock.gif        
End Get
ExpandedSubBlockEnd.gif    
End Property

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Public ReadOnly Property Price()Property Price() As Decimal
InBlock.gif        
Get
InBlock.gif            
Return _Price
InBlock.gif        
End Get
ExpandedSubBlockEnd.gif    
End Property

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Public Property Quantity()Property Quantity() As Integer
InBlock.gif        
Get
InBlock.gif            
Return _Quantity
InBlock.gif        
End Get
InBlock.gif        
Set(ByVal value As Integer)
InBlock.gif            _Quantity 
= value
InBlock.gif        
End Set
ExpandedSubBlockEnd.gif    
End Property

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Public Sub New()Sub New(ByVal ID As Integer, _
InBlock.gif      
ByVal Name As StringByVal Price As Decimal)
InBlock.gif        _ID 
= ID
InBlock.gif        _Name 
= Name
InBlock.gif        _Price 
= Price
ExpandedSubBlockEnd.gif    
End Sub

ExpandedBlockEnd.gif
End Class

 

None.gif 列表5 ShoppingCart (c#)
None.gif
None.gif
using  System;
None.gif
using  System.Collections;
None.gif
None.gif[Serializable]
None.gif
public   class  ShoppingCart
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public Hashtable _CartItems = new Hashtable();
InBlock.gif
InBlock.gif    
// Return all the items from the Shopping Cart
InBlock.gif
    public ICollection CartItems
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
get dot.gifreturn _CartItems.Values; }
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
// The sum total of the prices
InBlock.gif
    public decimal Total
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
get 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
decimal sum = 0;
InBlock.gif            
foreach (CartItem item in _CartItems.Values)
InBlock.gif                sum 
+= item.Price * item.Quantity;
InBlock.gif            
return sum;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
// Add a new item to the shopping cart
InBlock.gif
    public void AddItem(int ID, string Name, decimal Price)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        CartItem item 
= (CartItem)_CartItems[ID];
InBlock.gif        
if (item == null)
InBlock.gif            _CartItems.Add(ID, 
new CartItem(ID, Name, Price));
InBlock.gif        
else
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            item.Quantity
++;
InBlock.gif            _CartItems[ID] 
= item;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
// Remove an item from the shopping cart
InBlock.gif
    public void RemoveItem(int ID)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        CartItem item 
= (CartItem)_CartItems[ID];
InBlock.gif        
if (item == null)
InBlock.gif            
return;
InBlock.gif        item.Quantity
--;
InBlock.gif        
if (item.Quantity == 0)
InBlock.gif            _CartItems.Remove(ID);
InBlock.gif        
else
InBlock.gif            _CartItems[ID] 
= item;
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedBlockEnd.gif}

None.gif
None.gif[Serializable]
None.gif
public   class  CartItem
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
private int _ID;
InBlock.gif    
private string _Name;
InBlock.gif    
private decimal _Price;
InBlock.gif    
private int _Quantity = 1;
InBlock.gif
InBlock.gif    
public int ID
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
get dot.gifreturn _ID; }
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public string Name
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
get dot.gifreturn _Name; }
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public decimal Price
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
get dot.gifreturn _Price; }
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public int Quantity
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
get dot.gifreturn _Quantity; }
ExpandedSubBlockStart.gifContractedSubBlock.gif        
set dot.gif{ _Quantity = value; }
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public CartItem(int ID, string Name, decimal Price)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        _ID 
= ID;
InBlock.gif        _Name 
= Name;
InBlock.gif        _Price 
= Price;
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

如果你把列表5中的代码添加到应用程序的App_Code目录中,购物篮会自动被编译。

 

在列表5中有一点值得注意,那就是ShoppingCart和CartItem类都加上了可序列化的特性,这一点对于他们能否被序列化十分重要,只有这样才能保存在Profile对象中。

最后,列表6的页面显示了可以被添加到购物篮中的产品。购物篮是通过BindShoppingCart方法从Profile对象中载入,该方法把购物篮中的对象绑定到一个GridView对象上,这些对象可以通过ShoppingCart类的CartItems属性获得。

o_userprofiles_fig02.gif
图2 在profile中存储购物篮

AddCartItem方法用于在购物篮中添加一个产品,该方法中包含了检测Profile是否存在ShoppingCart的代码。对于Profile中存储的对象,你必须自己实例化这些对象,他们不会自动实例化。

RemoveCartItem方法用于从购物篮中移除一个产品,该方法只是简单地通过调用Profile中的ShoppingCart对象的RemoveItem方法。

 

 

None.gif 列表  6   -  Products.aspx (Visual Basic .NET)
None.gif
None.gif
< %@ Page Language = " VB "  % >
None.gif
None.gif
< script runat = " server " >
None.gif
ExpandedBlockStart.gifContractedBlock.gif    
Sub Page_Load() Sub Page_Load()
InBlock.gif        
If Not IsPostBack Then
InBlock.gif            BindShoppingCart()
InBlock.gif        
End If
ExpandedBlockEnd.gif    
End Sub

None.gif        
ExpandedBlockStart.gifContractedBlock.gif    
Sub BindShoppingCart() Sub BindShoppingCart()
InBlock.gif        
If Not Profile.ShoppingCart Is Nothing Then
InBlock.gif            CartGrid.DataSource 
= Profile.ShoppingCart.CartItems
InBlock.gif            CartGrid.DataBind()
InBlock.gif            lblTotal.Text 
= Profile.ShoppingCart.Total.ToString("c")
InBlock.gif        
End If
ExpandedBlockEnd.gif    
End Sub

None.gif   
ExpandedBlockStart.gifContractedBlock.gif    
Sub AddCartItem() Sub AddCartItem(ByVal s As ObjectByVal e As EventArgs)
InBlock.gif        
Dim row As GridViewRow = ProductGrid.SelectedRow
InBlock.gif
InBlock.gif        
Dim ID As Integer = CInt(ProductGrid.SelectedDataKey.Value)
InBlock.gif        
Dim Name As String = row.Cells(1).Text
InBlock.gif        
Dim Price As Decimal = CDec(row.Cells(2).Text)
InBlock.gif        
InBlock.gif        
If Profile.ShoppingCart Is Nothing Then
InBlock.gif            Profile.ShoppingCart 
= New ShoppingCart
InBlock.gif        
End If
InBlock.gif        Profile.ShoppingCart.AddItem(ID, Name, Price)
InBlock.gif        BindShoppingCart()
ExpandedBlockEnd.gif    
End Sub

None.gif    
ExpandedBlockStart.gifContractedBlock.gif    
Sub RemoveCartItem() Sub RemoveCartItem(ByVal s As ObjectByVal e As EventArgs)
InBlock.gif        
Dim ID As Integer = CInt(CartGrid.SelectedDataKey.Value)
InBlock.gif        Profile.ShoppingCart.RemoveItem(ID)
InBlock.gif        BindShoppingCart()
ExpandedBlockEnd.gif    
End Sub

None.gif
</ script >
None.gif
None.gif
< html >
None.gif
< head >
None.gif    
< title > Products </ title >
None.gif
</ head >
None.gif
< body >
None.gif    
< form id = " form1 "  runat = " server " >
None.gif
None.gif    
< table width = " 100% " >
None.gif    
< tr >
None.gif        
< td valign = " top " >
None.gif    
< h2 > Products </ h2 >     
None.gif    
< asp:GridView
None.gif        ID
= " ProductGrid "
None.gif        DataSourceID
= " ProductSource "
None.gif        DataKeyNames
= " ProductID "
None.gif        AutoGenerateColumns
= " false "
None.gif        OnSelectedIndexChanged
= " AddCartItem "
None.gif        ShowHeader
= " false "
None.gif        CellPadding
= " 5 "
None.gif        Runat
= " Server " >
None.gif        
< Columns >
None.gif            
< asp:ButtonField 
None.gif                CommandName
= " select "
None.gif                Text
= " Buy "   />
None.gif            
< asp:BoundField
None.gif                DataField
= " ProductName "   />
None.gif            
< asp:BoundField
None.gif                DataField
= " UnitPrice "  
None.gif                DataFormatString
= " {0:c} "   />
None.gif        
</ Columns >
None.gif    
</ asp:GridView >
None.gif
None.gif
None.gif
None.gif        
None.gif    
< asp:SqlDataSource
None.gif        ID
= " ProductSource "
None.gif        ConnectionString
=
None.gif
" Server=localhost;Database=Northwind;Trusted_Connection=true; "
None.gif        SelectCommand
=  
None.gif          
" SELECT ProductID,ProductName,UnitPrice FROM Products "
None.gif        Runat
= " Server "   />
None.gif        
</ td >
None.gif        
< td valign = " top " >
None.gif        
< h2 > Shopping Cart </ h2 >
None.gif        
< asp:GridView
None.gif            ID
= " CartGrid "
None.gif            AutoGenerateColumns
= " false "
None.gif            DataKeyNames
= " ID "
None.gif            OnSelectedIndexChanged
= " RemoveCartItem "
None.gif            CellPadding
= " 5 "  
None.gif            Width
= " 300 "
None.gif            Runat
= " Server " >
None.gif            
< Columns >
None.gif            
< asp:ButtonField
None.gif                CommandName
= " select "
None.gif                Text
= " Remove "   />
None.gif            
< asp:BoundField
None.gif                DataField
= " Name "  
None.gif                HeaderText
= " Name "   />
None.gif            
< asp:BoundField
None.gif                DataField
= " Price "  
None.gif                HeaderText
= " Price "  
None.gif                DataFormatString
= " {0:c} "   />
None.gif            
< asp:BoundField
None.gif                DataField
= " Quantity "  
None.gif                HeaderText
= " Quantity "   />
None.gif            
</ Columns >
None.gif        
</ asp:GridView >
None.gif        
< b > Total: </ b >  
None.gif        
< asp:Label ID = " lblTotal "  Runat = " Server "   />
None.gif        
</ td >
None.gif     
</ tr >
None.gif     
</ table >
None.gif    
</ form >
None.gif
</ body >
None.gif
</ html >

 

None.gif 列表  6 . Products.aspx (C#)
None.gif
None.gif
<% @ Page Language = " C# "   %>
None.gif
<% @ Import Namespace = " System.Globalization "   %>
None.gif
< script runat = " server " >
None.gif
ExpandedBlockStart.gifContractedBlock.gif    
void  Page_Load()  dot.gif {
InBlock.gif        
if (!IsPostBack)
InBlock.gif            BindShoppingCart();
ExpandedBlockEnd.gif    }

None.gif        
None.gif    
void  BindShoppingCart() 
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
if (Profile.ShoppingCart != null
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            CartGrid.DataSource 
= Profile.ShoppingCart.CartItems;
InBlock.gif            CartGrid.DataBind();
InBlock.gif            lblTotal.Text 
= Profile.ShoppingCart.Total.ToString("c");
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

None.gif   
None.gif    
void  AddCartItem(Object s, EventArgs e) 
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        GridViewRow row 
= ProductGrid.SelectedRow;
InBlock.gif
InBlock.gif        
int ID = (int)ProductGrid.SelectedDataKey.Value;
InBlock.gif        String Name 
= row.Cells[1].Text;
InBlock.gif        
decimal Price = Decimal.Parse(row.Cells[2].Text, 
InBlock.gif          NumberStyles.Currency);
InBlock.gif        
InBlock.gif        
if (Profile.ShoppingCart == null)
InBlock.gif            Profile.ShoppingCart 
= new ShoppingCart();
InBlock.gif       
InBlock.gif        Profile.ShoppingCart.AddItem(ID, Name, Price);
InBlock.gif        BindShoppingCart();
ExpandedBlockEnd.gif    }

None.gif    
None.gif    
void  RemoveCartItem(Object s, EventArgs e) 
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
int ID = (int)CartGrid.SelectedDataKey.Value;
InBlock.gif        Profile.ShoppingCart.RemoveItem(ID);
InBlock.gif        BindShoppingCart();
ExpandedBlockEnd.gif    }

None.gif
</ script >
None.gif
None.gif
< html >
None.gif
< head >
None.gif    
< title > Products </ title >
None.gif
</ head >
None.gif
< body >
None.gif    
< form id = " form1 "  runat = " server " >
None.gif
None.gif    
< table width = " 100% " >
None.gif    
< tr >
None.gif        
< td valign = " top " >
None.gif    
< h2 > Products </ h2 >     
None.gif    
< asp:GridView
None.gif        ID
= " ProductGrid "
None.gif        DataSourceID
= " ProductSource "
None.gif        DataKeyNames
= " ProductID "
None.gif        AutoGenerateColumns
= " false "
None.gif        OnSelectedIndexChanged
= " AddCartItem "
None.gif        ShowHeader
= " false "
None.gif        CellPadding
= " 5 "
None.gif        Runat
= " Server " >
None.gif        
< Columns >
None.gif            
< asp:ButtonField 
None.gif                CommandName
= " select "
None.gif                Text
= " Buy "   />
None.gif            
< asp:BoundField
None.gif                DataField
= " ProductName "   />
None.gif            
< asp:BoundField
None.gif                DataField
= " UnitPrice "  
None.gif                DataFormatString
= " {0:c} "   />
None.gif        
</ Columns >
None.gif    
</ asp:GridView >
None.gif
None.gif
None.gif
None.gif        
None.gif    
< asp:SqlDataSource
None.gif        ID
= " ProductSource "
None.gif        ConnectionString
=
None.gif
" Server=localhost;Database=Northwind;Trusted_Connection=true; "
None.gif        SelectCommand
=
None.gif          
" SELECT ProductID,ProductName,UnitPrice FROM Products "
None.gif        Runat
= " Server "   />
None.gif        
</ td >
None.gif        
< td valign = " top " >
None.gif        
< h2 > Shopping Cart </ h2 >
None.gif        
< asp:GridView
None.gif            ID
= " CartGrid "
None.gif            AutoGenerateColumns
= " false "
None.gif            DataKeyNames
= " ID "
None.gif            OnSelectedIndexChanged
= " RemoveCartItem "
None.gif            CellPadding
= " 5 "  
None.gif            Width
= " 300 "
None.gif            Runat
= " Server " >
None.gif            
< Columns >
None.gif            
< asp:ButtonField
None.gif                CommandName
= " select "
None.gif                Text
= " Remove "   />
None.gif            
< asp:BoundField
None.gif                DataField
= " Name "  
None.gif                HeaderText
= " Name "   />
None.gif            
< asp:BoundField
None.gif                DataField
= " Price "  
None.gif                HeaderText
= " Price "  
None.gif                DataFormatString
= " {0:c} "   />
None.gif            
< asp:BoundField
None.gif                DataField
= " Quantity "  
None.gif                HeaderText
= " Quantity "   />
None.gif            
</ Columns >
None.gif        
</ asp:GridView >
None.gif        
< b > Total: </ b >  
None.gif        
< asp:Label ID = " lblTotal "  Runat = " Server "   />
None.gif        
</ td >
None.gif     
</ tr >
None.gif     
</ table >
None.gif    
</ form >
None.gif
</ body >
None.gif
</ html >

继承一个profile
你也可以通过从一个已经存在的profile类中继承一个profile来完成对profile的定义,这种特性能够帮助你在多个应用程序中使用相同的profile。
例如,列表7中列出了一个拥有多个用户属性的类,该类是从ProfileBase类继承而来的(你可以在System.Web.Profile中找到)

 

在列表8中的Web.config包含一个从UserInfo类继承而来的profile,通过该声明,新的profile可以获得UserInfo类的所有属性。

 

None.gif 列表  7 . UserInfo (Visual Basic .NET)
None.gif
None.gif
Imports  Microsoft.VisualBasic
None.gif
Imports  System.Web.Profile
None.gif
ExpandedBlockStart.gifContractedBlock.gif
Public   Class UserInfo Class UserInfo
InBlock.gif    
Inherits ProfileBase
InBlock.gif
InBlock.gif    
Private _FirstName As String
InBlock.gif    
Private _LastName As String
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Public Property FirstName()Property FirstName() As String
InBlock.gif        
Get
InBlock.gif            
Return _FirstName
InBlock.gif        
End Get
InBlock.gif        
Set(ByVal value As String)
InBlock.gif            _FirstName 
= value
InBlock.gif        
End Set
ExpandedSubBlockEnd.gif    
End Property

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Public Property LastName()Property LastName() As String
InBlock.gif        
Get
InBlock.gif            
Return _LastName
InBlock.gif        
End Get
InBlock.gif        
Set(ByVal value As String)
InBlock.gif            _LastName 
= value
InBlock.gif        
End Set
ExpandedSubBlockEnd.gif    
End Property

InBlock.gif
ExpandedBlockEnd.gif
End Class

 

None.gif 列表  7 . UserInfo (C#) 
ExpandedBlockStart.gifContractedBlock.gif
using  System; using  System.Web.Profile; public   class  UserInfo : ProfileBase dot.gif {    private string _FirstName;    private string _LastName;    public string FirstName     dot.gif{        get dot.gifreturn _FirstName; }        set dot.gif{ _FirstName = value; }    }    public string LastName    dot.gif{        get dot.gifreturn _LastName; }        set dot.gif{ _LastName = value; }    }}
None.gif
None.gif
using  System;
None.gif
using  System.Web.Profile;
None.gif
None.gif
public   class  UserInfo : ProfileBase
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
private string _FirstName;
InBlock.gif    
private string _LastName;
InBlock.gif
InBlock.gif    
public string FirstName 
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
get dot.gifreturn _FirstName; }
ExpandedSubBlockStart.gifContractedSubBlock.gif        
set dot.gif{ _FirstName = value; }
ExpandedSubBlockEnd.gif    }

InBlock.gif    
public string LastName
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
get dot.gifreturn _LastName; }
ExpandedSubBlockStart.gifContractedSubBlock.gif        
set dot.gif{ _LastName = value; }
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}


 

None.gif 列表 8. Web.Config
None.gif
None.gif
< configuration >
None.gif    
< system .web >
None.gif           
< anonymousIdentification  enabled ="true"   />
None.gif   
< profile  inherits ="UserInfo"   />
None.gif    
</ system.web >
None.gif
</ configuration >


迁移匿名Profile设置
Profile对象既可用于匿名用户也可以用于已认证用户。然而,当用户从匿名用户状态转换为已认证用户状态时,Profile对象能够以一种令人难以理解的方式完成任务。
当匿名用户使用Profile对象时,用户profile是与一个随机生成的号码相关联的,该号码是根据每个用户唯一生成的,它保存在浏览器的cookie中,无论何时该用户返回应用程序,该用户的Profile设置会被自动加载。
如果匿名用户通过认证的话,所有与该用户相关的profile就会丢失,同时系统会生成一个新的profile。这时该Profile信息将与用户名相关联,而非唯一识别号。
要想理解所有这些工作,最好的方法就是看看下面的例子。列表9中的web.config定义了一个profile,该profile只有一个FavoriteColor属性。

 

None.gif 列表 9 Web.config
None.gif
None.gif
< configuration >
None.gif
< system .web >
None.gif
None.gif   
< authentication  mode ="Forms"   />
None.gif      
None.gif           
< anonymousIdentification  enabled ="true"   />
None.gif        
None.gif   
< profile >
None.gif   
< properties >
None.gif                  
< add 
None.gif         
name ="FavoriteColor"
None.gif         allowAnonymous
="true"  
None.gif         defaultValue
="Red"   />
None.gif               
</ properties >
None.gif           
</ profile >
None.gif
</ system.web >
None.gif
</ configuration >

转载于:https://www.cnblogs.com/jhobo/archive/2007/01/08/614856.html

内容概要:本文详细介绍了C语言指针和字符串操作的基础知识与高级技巧。指针部分涵盖了指针作为数据类型的特点,包括指针变量的定义、间接赋值的应用场景及其重要性,以及不同级别的指针如何在函数间传递并修改实参的值。同时强调了指针操作的安全性问题,如不允许向NULL或未知地址拷贝内存,并讲解了`void*`指针的作用及其转换规则。字符串操作部分则重点讨论了字符串初始化、`sizeof`与`strlen`的区别、字符`\0`的作用及其与其他符号的区别,还展示了数组法和指针法两种操作字符串的方式,并给出了几个常见的字符串处理算法实例,如统计子串出现次数、去除字符串两端空白字符等。 适用人群:具有初步C语言基础的学习者,特别是对指针和字符串操作有进一步需求的编程人员。 使用场景及目标:①帮助读者深入理解指针的工作机制,掌握通过指针间接访问和修改内存的技术;②使读者能够熟练运用字符串操作的基本函数,并能编写高效的字符串处理代码;③培养读者的安全意识,避免因不当使用指针而导致程序崩溃或产生未定义行为。 阅读建议:由于指针和字符串是C语言中较为复杂的概念,建议读者在学习过程中多做笔记,动手实践书中的示例代码,尤其要注意理解指针间接赋值的原理,以及字符串处理函数的具体实现细节。此外,对于`void*`指针的理解和使用,应特别留意其类型转换的要求,确保代码的安全性和正确性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值