本博客文章为转载,请勿用于商业目的!
本博客文章为转载,请勿用于商业目的!
快速回顾

 

1.分为2步:允许匿名用户,和匿名用户的购物车在登陆的时候保存
  1.允许匿名用户
      1.新建一个网站,右键网站添加新项(Web.config),设置3个地方  

        1<authentication mode="Forms" />
        
2.<anonymousIdentification enabled="true"/>
        
3.<profile automaticSaveEnabled="true" enabled="true">
          
<properties>
            
<group name="grpUserName">
              
<add name="UserName" allowAnonymous="true" type="String" serializeAs="Binary"/>
            
</group>
          
</properties>
         
</profile>

      2.在页面Default.aspx里拖一个Label,在页面的Load事件里写如下代码后,保存运行,看到匿名Id      

       if (Profile.IsAnonymous){ lbl_AnonUser.Text = Profile.UserName;}
        
else{lbl_AnonUser.Text = "";}

   2.允许匿名购物:即当用户admin登陆的时候,匿名选的购物车会自动加到admin的购物车
     条件:需要有数据库,购物车,会员用户,需要一个方法将匿名购物车加到登陆用户购物车里
      1.数据库(添加一个NorthWind,并在Web.config里配置好连接字符串)
      2.购物车:在App_Code里加CartItem和ShoppingCart 2个类,并声明[Serializable]特性

CartItem
ShoppingCart

      3.会员用户:用Asp.net配置创建用户,这样系统会自动创建一个数据库AspNetSqlProfileProvider
        然后配置好Web.config,profile的defaultProvider不设置会默认AspNetSqlProfileProvider数据库          

Asp.net配置创建用户

<?xml version="1.0"?>
<!-- 
    注意: 除了手动编辑此文件以外,您还可以使用 
    Web 管理工具来配置应用程序的设置。可以使用 Visual Studio 中的
     “网站”
->“Asp.Net 配置”选项。
    设置和注释的完整列表在 
    machine.config.comments 中,该文件通常位于 
    \Windows\Microsoft.Net\Framework\v2.x\Config 中
-->
<configuration>
    
<appSettings/>
    
<connectionStrings>
        
<add name="NorthwindCNConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\NorthwindCN.mdf;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient"/>
    
</connectionStrings>
    
<system.web>
        
<!-- 
            设置 compilation debug
="true" 将调试符号插入
            已编译的页面中。但由于这会 
            影响性能,因此只在开发过程中将此值 
            设置为 
true
        
-->
        
<compilation debug="true">
            
<assemblies>
                
<add assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/></assemblies></compilation>
        
<!--
            通过 
<authentication> 节可以配置 ASP.NET 使用的 
            安全身份验证模式,
            以标识传入的用户。 
        
-->
    
<authentication mode="Forms">
      
<forms defaultUrl="Default.aspx" loginUrl="Default.aspx"/>
    
</authentication>
        
<anonymousIdentification enabled="true"/>
        
<profile automaticSaveEnabled="true" enabled="true">
            
<properties>
                
<group name="grpUserName">
                    
<add name="UserName" allowAnonymous="true" type="String" serializeAs="Binary"/>
                    
<add name="PassWord" allowAnonymous="true" type="String" serializeAs="Binary"/>
                    
<add name="Email" allowAnonymous="true" type="String" serializeAs="Binary"/>
                
</group>
                
<group name="grpShoppingCart">
                    
<add name="MyCart" allowAnonymous="true" type="ShoppingCart" serializeAs="Binary"/>
                
</group>
            
</properties>
        
</profile>
        
<!--
            如果在执行请求的过程中出现未处理的错误,
            则通过 
<customErrors> 节可以配置相应的处理步骤。具体说来,
            开发人员通过该节可以配置
            要显示的 html 错误页
            以代替错误堆栈跟踪。

        
<customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
            
<error statusCode="403" redirect="NoAccess.htm" />
            
<error statusCode="404" redirect="FileNotFound.htm" />
        
</customErrors>
        
-->
    
</system.web>
</configuration>

      4.转移购物车的方法,在Global.asax里(右键WebUI,新建全局应用程序)写Profile_OnMigrateAnonymous
        需要在最上面导入命名空间<%@ Import Namespace="System.Web.Profile" %>

Global.asax代码

      5.最后实现Default页面,页面的界面和后台
        1.界面中的用户名和登陆(是工具箱-->登陆(拖LoginName和LoginStatus))
        2.选购下面的GridView1是直接拖一张Product表进来的
        3.购物车下面的GridView2的数据是在后台绑定的,然后在点右边小三角(点 添加新列)
          -->选ButtonField,把文本改为删除
        4.下面的登陆控件(是工具箱-->登陆(拖Login))

页面后台代码

      6.代码  下载

posted on 2010-01-17 22:07  刘季  阅读(420)  评论(0编辑  收藏  举报