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)
{< br> cartitem item = (cartitem) _ cartitems [ID];
If (item = NULL)
_ cartitems. add (ID, new cartitem (ID, name, price);
else
{< br> item. quantity ++;
_cartitems [ID] = item;
}< BR >}
// 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 ID = "form1" runat = "server">
& Lt; table width = "100%" & gt;
<Tr>
<TD valign = "TOP">
<H2> Products </H2>
<Asp: gridview
Id = "productgrid"
Performanceid = "productsource"
Datakeynames = "productid"
Autogeneratecolumns = "false"
Onselectedindexchanged = "addcartitem"
Showheader = "false"
Cellpadding = "5"
Runat = "server">
<Columns>
<Asp: buttonfield
Commandname = "select"
TEXT = "buy"/>
<Asp: boundfield
Datafield = "productname"/>
<Asp: boundfield
Datafield = "unitprice"
Dataformatstring = "{0: c}"/>
</Columns>
</ASP: gridview>
<Asp: sqldatasource
Id = "productsource"
Connectionstring =
"Server = localhost; database = northwind; trusted_connection = true ;"
Selectcommand =
"Select productid, productname, unitprice from products"
Runat = "server"/>
</TD>
<TD valign = "TOP">
<H2> shopping cart </H2>
<Asp: gridview
Id = "cartgrid"
Autogeneratecolumns = "false"
Datakeynames = "ID"
Onselectedindexchanged = "removecartitem"
Cellpadding = "5"
Width = "300"
Runat = "server">
<Columns>
<Asp: buttonfield
Commandname = "select"
TEXT = "Remove"/>
<Asp: boundfield
Datafield = "name"
Headertext = "name"/>
<Asp: boundfield
Datafield = "price"
Headertext = "price"
Dataformatstring = "{0: c}"/>
<Asp: boundfield
Datafield = "quantity"
Headertext = "quantity"/>
</Columns>
</ASP: gridview>
<B> total: </B>
<Asp: Label id = "lbltotal" runat = "server"/>
</TD>
</Tr>
</Table>
</Form>
</Body>
</Html>
4. On how to play with authenticated profile:
<% @ Page Language = "C #" %>
<SCRIPT runat = "server">
Void login (Object S, eventargs E)
{
Formsauthentication. setauthcookie ("bill", false );
Response. Redirect (request. Path );
}
Void logout (Object S, eventargs E)
{
Formsauthentication. signout ();
Response. Redirect (request. Path );
}
void updateprofile (Object S, eventargs e)
{< br> profile. favoritecolor = txtfavoritecolor. text;
}< br>
void page_prerender ()
{< br> lblusername. TEXT = profile. username;
lblfavoritecolor. TEXT = profile. favoritecolor;
}< br>
5. migrating the info in the anonymous mode to authenticated Mode
This magic lies in global. asax (C #)
<% @ Application language = "C #" %>
<SCRIPT runat = "server">
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
Step 1: 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 ID = "form1" runat = "server">
<Asp: Panel id = "pnlsurvey" runat = "server">
What is your favorite programming language?
<Br/>
<Asp: radiobuttonlist
Id = "rdllanguage"
Runat = "server">
<Asp: listitem text = "VB. NET" selected = "true"/>
<Asp: listitem text = "C #"/>
<Asp: listitem text = "J #"/>
</ASP: radiobuttonlist>
<P> & nbsp; </P>
What is your favorite development environment?
<Br/>
<Asp: radiobuttonlist
Id = "rdlenvironment"
Runat = "server">
<Asp: listitem text = "vs. Net" selected = "true"/>
<Asp: listitem text = "web matrix"/>
<Asp: listitem text = "Notepad"/>
</ASP: radiobuttonlist>
<P> & nbsp; </P>
<Asp: button id = "button1"
TEXT = "submit survey"
Onclick = "savesurvey"
Runat = "server"/>
</ASP: Panel>
<Asp: Panel id = "pnlsurveycompleted" runat = "server">
Thank you for completing the survey!
</ASP: Panel>
</Form>
</Body>
</Html>