Explanation:
We will start with how to create and populate the Birthday DropDownlist in the Registration
Page. Actually, we are using three dropdownlists to get our desired result. 1st
Dropdownlist is for Days, 2nd Months and 3rd for
Years.
Just drag three DropdownlistBoxes to your .aspx Page.
<table>
<tr>
<td>
<asp:DropDownList ID="DropDownListDay" runat="server">
</asp:DropDownList>
</td>
<td>
<asp:DropDownList ID="DropDownListMonth" runat="server">
</asp:DropDownList>
</td>
<td>
<asp:DropDownList ID="DropDownListYear" runat="server">
</asp:DropDownList>
</td>
</tr>
</table>
Now, Create three Functions in codebehind for each of the Dropdownlist to populate
them.
public void GetDaysListed(DropDownList DDDayList)
{
for (int i = 1; i < 32; i++)
{
ListItem list = new ListItem();
list.Text = i.ToString();
list.Value = i.ToString();
DDDayList.Items.Add(list);
}
ListItem mlist = new ListItem();
mlist.Text = "[Day]";
mlist.Value = "[Day]";
mlist.Selected = true;
DDDayList.Items.Add(mlist);
}
public void GetMonthsListed(DropDownList DDMonthList)
{
DateTime month = Convert.ToDateTime("1/1/2007");
for (int i = 0; i < 12; i++)
{
DateTime NextMont = month.AddMonths(i);
ListItem list = new ListItem();
list.Text = NextMont.ToString("MMMM");
list.Value = NextMont.Month.ToString();
DDMonthList.Items.Add(list);
}
ListItem mlist = new ListItem();
mlist.Text = "[Month]";
mlist.Value = "[Month]";
mlist.Selected = true;
DDMonthList.Items.Add(mlist);
}
public void GetYearsListed(DropDownList DDYearList)
{
int yearLast = DateTime.Now.Year-10;
int yearThen = yearLast - 90;
for (int i = yearThen; i < yearLast; i++)
{
ListItem list = new ListItem();
list.Text = yearThen.ToString();
list.Value = yearThen.ToString();
DDYearList.Items.Add(list);
yearThen += 1;
}
ListItem mlist = new ListItem();
mlist.Text = "[Year]";
mlist.Value = "[Year]";
mlist.Selected = true;
DDYearList.Items.Add(mlist);
}
You noticed that we are inserting an extra Item to our every DropDownlistBox. This
is because of three reasons.
- To give our users liberty of Optional behaviour.
- To Inform Users of fields data types
- Restrict users to enter what we want.
We will Pass the DropDownList objects to each of the function. It will be performed
at the Page_Load event. One more thing, these three DropDownlistBoxes
are embeded in to the "CreateUserWizardStep"
of "CreateUserWizard". We will
use " FindControl" function of
"ContentTemplateContainer" which
lies in "CreateUserStep". Where
as "CreateUserStep" is the
First Step of "CreateUserWizard".
Our Page_Load look like this;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GetYearsListed(
(DropDownList)WizardCreateUser.CreateUserStep.ContentTemplateContainer.FindControl
("DropDownListYear"));
GetMonthsListed(
(DropDownList)WizardCreateUser.CreateUserStep.ContentTemplateContainer.FindControl
("DropDownListMonth"));
GetDaysListed(
(DropDownList)WizardCreateUser.CreateUserStep.ContentTemplateContainer.FindControl
("DropDownListDay"));
}
}
We are Casting the "FindControl" to our DropDownlist
Control type.
Now, we are going to save the users choice in the Profile. Before going next we
should know how to save and retrive profiles in ASP .NET 2.0. See the Following
articles for the basics of Profilling.
- To create UserPrifile page? See our Article
Profiling in ASP .NET 2.0.
- To create Profile in the Registration Page see
Create a User Profile in Regisration Page.
We added One more field to our Profile in the Web.config. Data Type of the Field
is DateTime.
<add name="Bday"
type="System.DateTime"/>
Now, see the code to save Birthday:
DateTime bday = new
DateTime(
Int32.Parse(
((DropDownList)
WizardCreateUser.CreateUserStep.ContentTemplateContainer.FindControl
("DropDownListYear")).SelectedValue),
Int32.Parse(
((DropDownList)
WizardCreateUser.CreateUserStep.ContentTemplateContainer.FindControl
("DropDownListMonth")).SelectedValue),
Int32.Parse(
((DropDownList)
WizardCreateUser.CreateUserStep.ContentTemplateContainer.FindControl
("DropDownListDay")).SelectedValue)
);
p.Bday = bday;
p.Save();
Thats it, now profile have the Birthday of the newly registerd user. Next we will
see the trick to show Birthday in the userProfile.aspx Page.
MembershipUser ThisUser =
Membership.GetUser(UserName);
if (ThisUser != null)
{
ProfileCommon Prof
= Profile.GetProfile(UserName);
if (Prof.Bday != null &&
Prof.Bday.Year> 1907)
{
TimeSpan
timespn = DateTime.Now.Subtract(Prof.Bday);
int years = (int)(timespn.TotalDays / 365.25);
LabeldataBd.Text = "Age:" + Convert.ToString(years);
}
We are checking the Profile.Bday.Year that if it is Greater then 1907 or not (which
is our least value in the Year DropDownListBox). Why? because if your page is already
running and you enter a new Profile Field "Bday" to the existing Profile
it will generate a dummy value for all users. We want some how to check the dummy
value creation. In my case it enterd "01/01/0001" to each user.
Where as, UserName can be Taken from QueryString.
string UserName=Request.QueryString("UserName");
|