Share This!

Sunday, May 25, 2008

ASP: Adding info to user profiles.

First: this is NOT a tutorial on adding security to your website. It's really about adding additional info to user profiles in once you have created the profile.

It all started when I was asked to add some site-specific data to user profiles for a job. I wanted to add a license number (and some other fields) as part of our validation process.

Here is the short version for those who just want to make it work. All the examples here are in C# and ASP.NET 2008.

Quick Summary

First open your web.config file and find .

Right after that add a few lines. This example shows my own License Number field.

    <add name="LicenseNo" type="System.String"></add>

Ok, that adds the spot for this data to the Profile object. Now here's what you do to access the data.

TextBox Example:

Make an ASP.Net AJAX web page in a secured directory. Then add this:

<asp:textbox id="tbLicense" runat="server"></asp:textbox>

Now click the Textbox and Select events from properties. Double click Init and create this handler (If you don't get the lightning bolt icon for events in the properties window, read this).

 protected void tbLicense_Init(object sender, EventArgs e)
     tbLicense.Text = Profile.LicenseNo;

Now add a handler for textchanged.

 protected void tbLicense_TextChanged(object sender, EventArgs e)
     Profile.LicenseNo = tbLicense.Text;

You could make the update happen on a button click if you like but I wanted immediate updates when the user changes the field. So go back one more time to the aspx page and on tbLicenseNo change AutoPostBack to True.

Now run your page.

DropDownList Example:

Here is an example with a dropdown list for account types.

in web.config

<add name="AccountType" type="System.string">

in the aspx page

<asp:dropdownlist id="AcctType" runat="server" autopostback="True" 
   oninit="AcctType_Init" onselectedindexchanged="AcctType_SelectedIndexChanged">
   <asp:listitem text="Customer" value="Customer"></asp:listitem>
   <asp:listitem text="Vendor" value="Vendor"></asp:listitem>
   <asp:listitem text="Investor" value="Investor"></asp:listitem>

now the handlers

protected void AcctType_Init(object sender, EventArgs e)
     string s = Profile.AccountType;
protected void AcctType_SelectedIndexChanged(object sender, EventArgs e)
     Profile.AccountType = AcctType.SelectedValue;


Detailed explanation:

What's happening? There is an object named Profile associated with every web hit. Note that I didn't have to create an instance of it or load it from anywhere. It's just there. Note what happens in the C# source when you type Profile. The context help pops up the names of the data items you added in the web.config file.

Now open up your aspnetdb.mdf database. The data are stored in the aspnet_Profile table. I do not recommend editing this data on your own through SQL. It's much easier to modify it via the profile object.

No comments:

Post a Comment

Contact Us


Email *

Message *