• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

[Asp.net]Understand profile

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

Detailed introduction on asp.net 2.0 feature: profile, from how to access it in code both in anonymous and authenticated condition and sql provider for storage, also cover the report part for managibility.
Here is the URL: http://msdn.microsoft.com/asp.net/default.aspx?pull=/library/en-us/dnvs05/html/UserProfiles.asp
Enjoy! :)

A few code piece for quick reference:
1. Add your type to the web.config
<configuration>
<system.web>

  <anonymousIdentification enabled="true" />
 
  <profile>
    <properties>
    <add
       name="ShoppingCart"
       type="ShoppingCart"
       serializeAs="Binary"
       allowAnonymous="true" />
    </properties>
  </profile>
</system.web>
</configuration>

2. Define your type
using System;
using System.Collections;

[Serializable]
public class ShoppingCart
{
    public Hashtable _CartItems = new Hashtable();

    // Return all the items from the Shopping Cart
    public ICollection CartItems
    {
        get { return _CartItems.Values; }
    }

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

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

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

}

[Serializable]
public class CartItem
{
    private int _ID;
    private string _Name;
    private decimal _Price;
    private int _Quantity = 1;

    public int ID
    {
        get { return _ID; }
    }

    public string Name
    {
        get { return _Name; }
    }

    public decimal Price
    {
        get { return _Price; }
    }

    public int Quantity
    {
        get { return _Quantity; }
        set { _Quantity = value; }
    }

    public CartItem(int ID, string Name, decimal Price)
    {
        _ID = ID;
        _Name = Name;
        _Price = Price;
    }
}

3. How to access it in your code:
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Globalization" %>
<script runat="server">

    void Page_Load() {
        if (!IsPostBack)
            BindShoppingCart();
    }
       
    void BindShoppingCart()
    {
        if (Profile.ShoppingCart != null)
        {
            CartGrid.DataSource = Profile.ShoppingCart.CartItems;
            CartGrid.DataBind();
            lblTotal.Text = Profile.ShoppingCart.Total.ToString("c");
        }
    }
  
    void AddCartItem(Object s, EventArgs e)
    {
        GridViewRow row = ProductGrid.SelectedRow;

        int ID = (int)ProductGrid.SelectedDataKey.Value;
        String Name = row.Cells[1].Text;
        decimal Price = Decimal.Parse(row.Cells[2].Text,
          NumberStyles.Currency);
       
        if (Profile.ShoppingCart == null)
            Profile.ShoppingCart = new ShoppingCart();
      
        Profile.ShoppingCart.AddItem(ID, Name, Price);
        BindShoppingCart();
    }
   
    void RemoveCartItem(Object s, EventArgs e)
    {
        int ID = (int)CartGrid.SelectedDataKey.Value;
        Profile.ShoppingCart.RemoveItem(ID);
        BindShoppingCart();
    }
</script>

<html>
<head>
    <title>Products</title>
</head>
<body>
    <form >
    void Profile_MigrateAnonymous(Object s,
      ProfileMigrateEventArgs e)
    {
        ProfileCommon anonProfile =
          Profile.GetProfile(e.AnonymousId);
        Profile.FavoriteColor = anonProfile.FavoriteColor;
    }
</script>

6. Config to use MSSQL provider for storage
step1: run the script comes with the .net framework: aspnet_regsql and it is located in your Windows\Microsoft.NET\Framework\[version] folder.
step 2: Update your web.config looks like this:
<configuration>

<connectionStrings>

<add

name="myConnectionString"

connectionString=

"Server=MyServer;Trusted_Connection=true;database=MyDatabase" />

</connectionStrings>

<system.web>

<anonymousIdentification enabled="true" />

<profile defaultProvider="MyProfileProvider">

<providers>

<add

name="MyProfileProvider"

type="System.Web.Profile.SqlProfileProvider"

connectionStringName="myConnectionString" />

</providers>

<properties>

<add

name="FirstName"

allowAnonymous="true" />

<add

name="LastName"

allowAnonymous="true" />

</properties>

</profile>

</system.web>

</configuration>

7. Delete inactive profile:
using System;
using System.Web.Profile;

public class DeleteInactiveProfiles
{   
    public static void Main()
    {
      int deleted = 0;
      deleted =
        ProfileManager.DeleteInactiveProfiles(
        ProfileAuthenticationOption.All,
        DateTime.Now.AddDays(-7));
      Console.WriteLine("Deleted " +
        deleted.ToString() + " profiles" );
    }
       
}

8 Get stored profile information:
<%@ Page Language="C#" %>

<script runat="server">

    void SaveSurvey(Object s, EventArgs e)
    {   
        Profile.FavoriteLanguage = rdlLanguage.SelectedItem.Text;
        Profile.FavoriteEnvironment = rdlEnvironment.SelectedItem.Text;
        Profile.SurveyCompleted = true;
    }
   
    void Page_PreRender()
    {   
        if (Profile.SurveyCompleted)
        {
            pnlSurvey.Visible = false;
            pnlSurveyCompleted.Visible = true;
        }
        else
        {
            pnlSurvey.Visible = true;
            pnlSurveyCompleted.Visible = false;
        }
    }
   
</script>

<html>
<head>
    <title>Survey</title>
</head>
<body>
    <form >
    Thank you for completing the survey!
    </asp:Panel>
    </form>
</body>
</html>


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
ASP.NET中下载文件的几种方法发布时间:2022-07-10
下一篇:
ASP.NET多线程下使用HttpContext.Current为null解决方案 ...发布时间:2022-07-10
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap