Link to home
Start Free TrialLog in
Avatar of mathieu_cupryk
mathieu_cuprykFlag for Canada

asked on

An Object reference is required for the non-static filed, method, or property

Error      42      An object reference is required for the non-static field, method, or property 'DateDLL.DateDDL.Date.get'      C:\inetpub\wwwroot\OmegaLove\OmegaLove.Web\Controls\uxRegister.ascx.cs      75      67      OmegaLove.Web
-------------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace OL_DateDLL
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:DateDDL runat=server></{0}:DateDDL>")]
    public class OL_DateDDL : CompositeControl
    {
        private Label lblCaption = new Label();
        private Label lblCaptionPadding = new Label();
        private DropDownList ddlMonth = new DropDownList();
        private DropDownList ddlDay = new DropDownList();
        private DropDownList ddlYear = new DropDownList();

        private string caption;
        private Unit captionWidth;
        private Unit captionPadding;
        private bool rightAlignCaption;

        private int month;
        private int day;
        private int year;
        private bool autoPostBack;
        private bool dateChangedEventRaised = false;

        public event EventHandler DateChanged;

        public OL_DateDDL()
        {
            ddlDay.SelectedIndexChanged += new EventHandler(ddlDay_SelectedIndexChanged);
            ddlMonth.SelectedIndexChanged += new EventHandler(ddlMonth_SelectedIndexChanged);
            ddlYear.SelectedIndexChanged += new EventHandler(ddlYear_SelectedIndexChanged);
            month = DateTime.Today.Month;
            day = DateTime.Today.Day;
            year = DateTime.Today.Year;

        }

        [Category("Appearance")]
        [Description("The caption displayed to the left of the text box.")]
        public string Caption
        {
            get { return caption; }
            set { caption = value; }
        }

        [Category("Appearance")]
        [Description("The width of the caption.")]
        public Unit CaptionWidth
        {
            get { return captionWidth; }
            set { captionWidth = value; }
        }

        [Category("Appearance")]
        [Description("The space between the caption and the text box.")]
        public Unit CaptionPadding
        {
            get { return captionPadding; }
            set { captionPadding = value; }
        }

        [Category("Appearance")]
        [Description("Determines if the caption is right-aligned.")]
        public bool RightAlignCaption
        {
            get { return rightAlignCaption; }
            set { rightAlignCaption = value; }
        }

        public DateTime Date
        {
            get
            {
                DateTime date;
                // Attempt to parse the date
                if (!DateTime.TryParse(this.Month + "/" + this.Day + "/" + this.Year, out date))
                {
                    // Invalid date! return MinValue
                    date = DateTime.MinValue;
                }

                return date;
            }
            set
            {
                this.Month = value.Month;
                this.Day = value.Day;
                this.Year = value.Year;
            }
        }



        // Add this to the DateDDL class somewhere
        protected virtual void OnDateChanged(EventArgs e)
        {
            // Trigger the event if it's defined,
            // and we haven't triggered it already.
            if (!dateChangedEventRaised && DateChanged != null)
            {
                DateChanged(this, e);
                dateChangedEventRaised = true; // Don't trigger more than once.
            }
        }


        // Change the property definitions in DateDDL as follows:
        [Category("Misc"), Description("Gets or set the month.")]
        public int Month
        {
            get
            {
                month = ddlMonth.SelectedIndex + 1;
                return month;
            }
            set
            {
                month = value;
                ddlMonth.SelectedIndex = month - 1;
                OnDateChanged(new EventArgs());
            }
        }

        [Category("Misc"), Description("Gets or sets the day.")]
        public int Day
        {
            get
            {
                day = ddlDay.SelectedIndex + 1;
                return day;
            }
            set
            {
                day = value;
                ddlDay.SelectedIndex = day - 1;
                OnDateChanged(new EventArgs());
            }
        }

        [Category("Misc"), Description("Gets or sets the year.")]
        public int Year
        {
            get
            {
                year = Convert.ToInt32(ddlYear.SelectedValue);
                return year;
            }
            set
            {
                year = value;
                ddlYear.SelectedValue = year.ToString();
                OnDateChanged(new EventArgs());
            }
        }

        [Category("Misc")]
        [Description("Determines whether a postback occurs "
            + "when the selection is changed.")]
        [DefaultValue(false)]
        public bool AutoPostBack
        {
            get { return autoPostBack; }
            set { autoPostBack = value; }
        }

        protected override void Render(HtmlTextWriter output)
        {
            base.Render(output);
        }

        protected override void CreateChildControls()
        {
            this.MakeCaption();
            this.MakeCaptionPadding();
            this.MakeMonthList();
            this.MakeDayList();
            this.MakeYearList();
            Controls.Clear();
            Controls.Add(lblCaption);
            Controls.Add(lblCaptionPadding);
            Controls.Add(ddlMonth);
            Controls.Add(new LiteralControl("&nbsp;"));
            Controls.Add(ddlDay);
            Controls.Add(new LiteralControl("&nbsp;"));
            Controls.Add(ddlYear);

            // Register client script:
            ddlMonth.Attributes.Add("onchange", "adjustDay('" + ddlMonth.ClientID + "','" + ddlDay.ClientID + "','" + ddlYear.ClientID + "');");
            ddlDay.Attributes.Add("onchange", "adjustDay('" + ddlMonth.ClientID + "','" + ddlDay.ClientID + "','" + ddlYear.ClientID + "');");
            ddlYear.Attributes.Add("onchange", "adjustDay('" + ddlMonth.ClientID + "','" + ddlDay.ClientID + "','" + ddlYear.ClientID + "');");
        }

        private void MakeCaption()
        {
            lblCaption.Text = Caption;
            lblCaption.Width = CaptionWidth;
            if (RightAlignCaption)
                lblCaption.Style["text-align"] = "right";
        }

        private void MakeCaptionPadding()
        {
            lblCaptionPadding.Width = CaptionPadding;
        }

        private void MakeMonthList()
        {
            ddlMonth.Width = Unit.Pixel(85);
            ddlMonth.Items.Clear();
            ddlMonth.Items.Add("January");
            ddlMonth.Items.Add("February");
            ddlMonth.Items.Add("March");
            ddlMonth.Items.Add("April");
            ddlMonth.Items.Add("May");
            ddlMonth.Items.Add("June");
            ddlMonth.Items.Add("July");
            ddlMonth.Items.Add("August");
            ddlMonth.Items.Add("September");
            ddlMonth.Items.Add("October");
            ddlMonth.Items.Add("November");
            ddlMonth.Items.Add("December");
            ddlMonth.AutoPostBack = AutoPostBack;
            ddlMonth.SelectedIndex = Month - 1;
        }

        private void MakeDayList()
        {
            ddlDay.Width = Unit.Pixel(40);
            ddlDay.Items.Clear();
            for (int i = 1; i <= 31; i++)
                ddlDay.Items.Add(i.ToString());
            ddlDay.SelectedValue = Day.ToString();
            ddlDay.AutoPostBack = AutoPostBack;
        }

        private void MakeYearList()
        {
            ddlYear.Width = Unit.Pixel(65);
            ddlYear.Items.Clear();
            for (int i = DateTime.Today.Year - 100;
                 i <= DateTime.Today.Year - 18; i++)
                ddlYear.Items.Add(i.ToString());
            ddlYear.SelectedValue = Year.ToString();
            ddlYear.AutoPostBack = AutoPostBack;
        }


        private void ddlMonth_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.Month = ddlMonth.SelectedIndex + 1;
            this.AdjustDay();
        }

        private void ddlDay_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.Day = Convert.ToInt32(ddlDay.SelectedValue);
            this.AdjustDay();
        }

        private void ddlYear_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.Year = Convert.ToInt32(ddlYear.SelectedValue);
            this.AdjustDay();
        }

        // C# Code behind in DateDDL class:
        private void AdjustDay()
        {
            switch (Month)
            {
                case 4:
                case 6:
                case 9:
                case 11:
                    if (Day > 30) Day = 30;
                    break;
                case 2:
                    int yy = Year;
                    if (Day > 28)
                        Day = (((yy % 4 == 0) && (yy % 100 != 0)) || (yy % 400 == 0)) ? 29 : 28;
                    break;
            }
        }


    }
}

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Threading;
using System.Globalization;
using System.Web;
using System.Web.Profile;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Resources;
using log4net;
using System.Collections.Generic;
using DateDLL;
 
namespace OmegaLove.Web.UI
{
    public partial class uxRegister : System.Web.UI.UserControl
    {
        // For generating random numbers.
        private readonly Random random = new Random();
        private static readonly ILog log = LogManager.GetLogger(typeof(uxRegister));
        private String iUserID;
 
      
 
        protected void Page_Load(object sender, EventArgs e)
        {
            //txtUserName.Focus();
            String UserID = Session["UserID"] != null ? Session["UserID"].ToString() : "";
 
            if (!Page.IsPostBack)
            {
                BindDropDownList();
                cddCountryRegister.SelectedValue = "CA";
                imgFlagRegister.ImageUrl = "~/images/flags/" + cddCountryRegister.SelectedValue + ".gif";
                // Create a random code and store it in the Session object.
                Session["CaptchaImageText"] = GenerateRandomCode();
            }
        }
 
        protected void DateDDL_DateChanged(object sender, EventArgs e)
        {
            var dateObj = (DateDDL)sender;
            DateTime dateValue = dateObj.Date;
        }
 
        protected void imgRegister_Click(object sender, EventArgs e)
        {
            if ((CaptchaTextBox.Text == Session["CaptchaImageText"].ToString()) && Page.IsValid)
            {
                try
                {
                    MembershipCreateStatus status;
                    
                    MembershipUser user = Membership.CreateUser(txtUserName.Text, txtPassword.Text, txtMailFrom.Text, null, null, false, null, out status);
                   
                    if (MembershipCreateStatus.Success == status)
                    {
 
                        var profile = new WebProfile().GetProfile(user.UserName);
                         
                        Guid userId = (Guid)user.ProviderUserKey;
                       
                        
                        profile.Personal.UserName = txtUserName.Text;
                        profile.Personal.Password = txtPassword.Text;
                        profile.Personal.Email = txtMailFrom.Text;
                        profile.Personal.FirstName = txtFirstName.Text;
                        profile.Personal.LastName = txtLastName.Text;
                        profile.Personal.DOB = Convert.ToDateTime(DateDDL.Date, CultureInfo.InvariantCulture);
                        profile.Personal.Age = getAge(DateDDL.Date);
                        profile.Personal.Gender = ddlGender.SelectedValue;
                        profile.Personal.Seeking = ddlSeeking.SelectedValue;
                        profile.Personal.ConfirmationGUID = Guid.NewGuid().ToString("N");
                        profile.Personal.UserIP = Request.UserHostAddress;
                        profile.Personal.SessionID = Session.SessionID;
 
                        profile.Address.Country = ddlCountryRegister.SelectedValue;
                        profile.Address.Region = ddlRegion.SelectedValue;
                        profile.Address.City = ddlCity.SelectedValue;
                        profile.Address.ZipCode = txtZipCode.Text;
 
                        profile.Save();
 
                        WebMsgBox.Show("User created successfully!");
                        iUserID = txtUserName.Text;
 
                        string sData = ResourceEmail.NewMemberEmail;
                        sData = sData.Replace("[Name]", txtFirstName.Text.Trim());
                        sData = sData.Replace("[LINK]", "http://www.omegalove.com/Activate.aspx?ID=" + userId);
                        sData = sData.Replace("[UserName]", txtUserName.Text.Trim());
                        sData = sData.Replace("[Pwd]", txtPassword.Text.Trim());
                        SMTPManager.SendEmail("webmaster@omegalove.com", "OmegaLove", txtMailFrom.Text.Trim(),
                                              sData, "New Member Activation", false);
                        Session["UserID"] = iUserID;
                        Response.Redirect("Registered.aspx");
                    }

Open in new window

Avatar of Anurag Thakur
Anurag Thakur
Flag of India image

you cannot use DateDDL.Date because Date is not a static property
you have to create an instance of the class and then only use it
profile.Personal.DOB = Convert.ToDateTime(DateDDL.Date, CultureInfo.InvariantCulture);
What ragi0017 said above is correct.
This is my addition to it.
You have something like this in your code:

var dateObj = (DateDDL)sender;
DateTime dateValue = dateObj.Date;

so what you can do is define a public variable or property in your .aspx code behind. then set this value as you are doing in the above two lines.
Than you can assign that value to profile.Personal.DOB.

So e.g. declare  DateTime dateValue outside to make it publicly avaialble within the class uxRegister. Assign it the value from your DDL and use it anywhere in the class.
Avatar of mathieu_cupryk

ASKER

Guru, I have this : I declared it private in the behind code.

namespace OmegaLove.Web.UI
{
    public partial class uxRegister : System.Web.UI.UserControl
    {
        // For generating random numbers.
        private readonly Random random = new Random();
        private static readonly ILog log = LogManager.GetLogger(typeof(uxRegister));
        private String iUserID;
        private DateTime dateValue;
 protected void Page_Load(object sender, EventArgs e)
        {
            txtUserName.Focus();
            String UserID = Session["UserID"] != null ? Session["UserID"].ToString() : "";

            if (!Page.IsPostBack)
            {
                BindDropDownList();
                cddCountryRegister.SelectedValue = "CA";
                dateValue.Date = ?
                imgFlagRegister.ImageUrl = "~/images/flags/" + cddCountryRegister.SelectedValue + ".gif";
                // Create a random code and store it in the Session object.
                Session["CaptchaImageText"] = GenerateRandomCode();
            }
        }


     
protected void Page_Load(object sender, EventArgs e)
        {
            txtUserName.Focus();
            String UserID = Session["UserID"] != null ? Session["UserID"].ToString() : "";

            if (!Page.IsPostBack)
            {
                BindDropDownList();
                cddCountryRegister.SelectedValue = "CA";
                imgFlagRegister.ImageUrl = "~/images/flags/" + cddCountryRegister.SelectedValue + ".gif";
                // Create a random code and store it in the Session object.
                Session["CaptchaImageText"] = GenerateRandomCode();
            }
        }


  dateObj = new DateDDL();
              ?
Is there something I am doing wrong:

public partial class uxRegister : System.Web.UI.UserControl
    {
        // For generating random numbers.
        private readonly Random random = new Random();
        private static readonly ILog log = LogManager.GetLogger(typeof(uxRegister));
        private String iUserID;
        protected OL_DateDLL.DateDDL dateObj;


        protected void Page_Load(object sender, EventArgs e)
        {
            txtUserName.Focus();
            String UserID = Session["UserID"] != null ? Session["UserID"].ToString() : "";

            if (!Page.IsPostBack)
            {
                BindDropDownList();
                cddCountryRegister.SelectedValue = "CA";
                imgFlagRegister.ImageUrl = "~/images/flags/" + cddCountryRegister.SelectedValue + ".gif";
                dateObj = new DateDDL();
                // Create a random code and store it in the Session object.
                Session["CaptchaImageText"] = GenerateRandomCode();
            }
        }

        protected void DateDDL_DateChanged(object sender, EventArgs e)
        {
            dateObj = (DateDDL)sender;
            DateTime dateValue = dateObj.Date;
        }

        protected void imgRegister_Click(object sender, EventArgs e)
        {
            if ((CaptchaTextBox.Text == Session["CaptchaImageText"].ToString()) && Page.IsValid)
            {
                try
                {
                    MembershipCreateStatus status;
                   
                    MembershipUser user = Membership.CreateUser(txtUserName.Text, txtPassword.Text, txtMailFrom.Text, null, null, false, null, out status);
                   
                    if (MembershipCreateStatus.Success == status)
                    {

                        var profile = new WebProfile().GetProfile(user.UserName);
                       
               
                        DateTime dateValue = dateObj.Date;

                         
                        Guid userId = (Guid)user.ProviderUserKey;
                       
                       
                        profile.Personal.UserName = txtUserName.Text;
                        profile.Personal.Password = txtPassword.Text;
                        profile.Personal.Email = txtMailFrom.Text;
                        profile.Personal.FirstName = txtFirstName.Text;
                        profile.Personal.LastName = txtLastName.Text;
                        profile.Personal.DOB = Convert.ToDateTime(dateObj.Date, CultureInfo.InvariantCulture);
                        profile.Personal.Age = getAge(dateObj.Date);
                        profile.Personal.Gender = ddlGender.SelectedValue;
                        profile.Personal.Seeking = ddlSeeking.SelectedValue;
                        profile.Personal.ConfirmationGUID = Guid.NewGuid().ToString("N");
                        profile.Personal.UserIP = Request.UserHostAddress;
                        profile.Personal.SessionID = Session.SessionID;

                        profile.Address.Country = ddlCountryRegister.SelectedValue;
                        profile.Address.Region = ddlRegion.SelectedValue;
                        profile.Address.City = ddlCity.SelectedValue;
                        profile.Address.ZipCode = txtZipCode.Text;

                        profile.Save();

                        WebMsgBox.Show("User created successfully!");
                        iUserID = txtUserName.Text;

                        string sData = ResourceEmail.NewMemberEmail;
                        sData = sData.Replace("[Name]", txtFirstName.Text.Trim());
                        sData = sData.Replace("[LINK]", "http://www.omegalove.com/Activate.aspx?ID=" + userId);
                        sData = sData.Replace("[UserName]", txtUserName.Text.Trim());
                        sData = sData.Replace("[Pwd]", txtPassword.Text.Trim());
                        SMTPManager.SendEmail("webmaster@omegalove.com", "OmegaLove", txtMailFrom.Text.Trim(),
                                              sData, "New Member Activation", false);
                        Session["UserID"] = iUserID;
                        Response.Redirect("Registered.aspx");
                    }
                    else
                    {
                        // first else block:
                        switch (status)
                        {
                            case MembershipCreateStatus.DuplicateUserName:
                                WebMsgBox.Show("There already exists a user with this username.");
                                break;
                            case MembershipCreateStatus.DuplicateEmail:
                                WebMsgBox.Show("There already exists a user with this email address.");
                                break;
                            case MembershipCreateStatus.DuplicateProviderUserKey:
                            case MembershipCreateStatus.InvalidAnswer:
                            case MembershipCreateStatus.InvalidEmail:
                            case MembershipCreateStatus.InvalidProviderUserKey:
                            case MembershipCreateStatus.InvalidQuestion:
                            case MembershipCreateStatus.InvalidUserName:
                                WebMsgBox.Show(string.Format("The {0} provided was invalid.", status.ToString().Substring(7)));
                                break;
                            default:
                                WebMsgBox.Show("There was an unknown error; the user account was NOT created.");
                                break;
                        }
                    }
                }
                catch (Exception ex)
                {
                    WebMsgBox.Show(ex.Message);
                }
            }
            else
            {
                WebMsgBox.Show("Invalid Access Code, try again.");
                CaptchaTextBox.Text = "";
                Session["CaptchaImageText"] = GenerateRandomCode();
            }
        }
Is this what u meant?
Please check this.
you are getting error at the following line
profile.Personal.DOB = Convert.ToDateTime(DateDDL.Date, CultureInfo.InvariantCulture);

because you are trying to access an instance property Date directly from the class and its not an issue if you use
dateObj = new DateDDL();
dateValue.Date = ?
I get an error on
 Parser Error
Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately.

Parser Error Message: Unknown server tag 'OL_DateDLL:OL_DateDLL'.

Source Error:

Line 233:                                                      
Line 234:                                                      <td class="physicalLightBox">&nbsp;
Line 235:                                                      <OL_DateDLL:OL_DateDLL ID="OL_DateDLL1" runat="server"
Line 236:                                            ondatechanged="DateDDL_DateChanged" Font-Bold="True" />
Line 237:                                                        </td>


<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="uxRegister.ascx.cs" Inherits="OmegaLove.Web.UI.uxRegister" %>
<%@ Register Assembly="OL_DateDLL" Namespace="OL_DateDLL" TagPrefix="OL_DateDLL" %>
<%@ Register Assembly="ClassLibrary" Namespace="ClassLibrary" TagPrefix="cc3"%>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit"  TagPrefix="ajaxToolkit" %>  
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
namespace OL_DateDLL
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:OL_DateDDL runat=server></{0}:DateDDL>")]
    public class OL_DateDDL : CompositeControl
    {
        private Label lblCaption = new Label();
        private Label lblCaptionPadding = new Label();
        private DropDownList ddlMonth = new DropDownList();
        private DropDownList ddlDay = new DropDownList();
        private DropDownList ddlYear = new DropDownList();
 
        private string caption;
        private Unit captionWidth;
        private Unit captionPadding;
        private bool rightAlignCaption;
 
        private int month;
        private int day;
        private int year;
        private bool autoPostBack;
        private bool dateChangedEventRaised = false;
 
        public event EventHandler DateChanged;
 
        public OL_DateDDL()
        {
            ddlDay.SelectedIndexChanged += new EventHandler(ddlDay_SelectedIndexChanged);
            ddlMonth.SelectedIndexChanged += new EventHandler(ddlMonth_SelectedIndexChanged);
            ddlYear.SelectedIndexChanged += new EventHandler(ddlYear_SelectedIndexChanged);
            month = DateTime.Today.Month;
            day = DateTime.Today.Day;
            year = DateTime.Today.Year;
 
        }
 
        [Category("Appearance")]
        [Description("The caption displayed to the left of the text box.")]
        public string Caption
        {
            get { return caption; }
            set { caption = value; }
        }
 
        [Category("Appearance")]
        [Description("The width of the caption.")]
        public Unit CaptionWidth
        {
            get { return captionWidth; }
            set { captionWidth = value; }
        }
 
        [Category("Appearance")]
        [Description("The space between the caption and the text box.")]
        public Unit CaptionPadding
        {
            get { return captionPadding; }
            set { captionPadding = value; }
        }
 
        [Category("Appearance")]
        [Description("Determines if the caption is right-aligned.")]
        public bool RightAlignCaption
        {
            get { return rightAlignCaption; }
            set { rightAlignCaption = value; }
        }
 
        public DateTime Date
        {
            get
            {
                DateTime date;
                // Attempt to parse the date
                if (!DateTime.TryParse(this.Month + "/" + this.Day + "/" + this.Year, out date))
                {
                    // Invalid date! return MinValue
                    date = DateTime.MinValue;
                }
 
                return date;
            }
            set
            {
                this.Month = value.Month;
                this.Day = value.Day;
                this.Year = value.Year;
            }
        }
 
 
 
        // Add this to the DateDDL class somewhere
        protected virtual void OnDateChanged(EventArgs e)
        {
            // Trigger the event if it's defined,
            // and we haven't triggered it already.
            if (!dateChangedEventRaised && DateChanged != null)
            {
                DateChanged(this, e);
                dateChangedEventRaised = true; // Don't trigger more than once.
            }
        }
 
 
        // Change the property definitions in DateDDL as follows:
        [Category("Misc"), Description("Gets or set the month.")]
        public int Month
        {
            get
            {
                month = ddlMonth.SelectedIndex + 1;
                return month;
            }
            set
            {
                month = value;
                ddlMonth.SelectedIndex = month - 1;
                OnDateChanged(new EventArgs());
            }
        }
 
        [Category("Misc"), Description("Gets or sets the day.")]
        public int Day
        {
            get
            {
                day = ddlDay.SelectedIndex + 1;
                return day;
            }
            set
            {
                day = value;
                ddlDay.SelectedIndex = day - 1;
                OnDateChanged(new EventArgs());
            }
        }
 
        [Category("Misc"), Description("Gets or sets the year.")]
        public int Year
        {
            get
            {
                year = Convert.ToInt32(ddlYear.SelectedValue);
                return year;
            }
            set
            {
                year = value;
                ddlYear.SelectedValue = year.ToString();
                OnDateChanged(new EventArgs());
            }
        }
 
        [Category("Misc")]
        [Description("Determines whether a postback occurs "
            + "when the selection is changed.")]
        [DefaultValue(false)]
        public bool AutoPostBack
        {
            get { return autoPostBack; }
            set { autoPostBack = value; }
        }
 
        protected override void Render(HtmlTextWriter output)
        {
            base.Render(output);
        }
 
        protected override void CreateChildControls()
        {
            this.MakeCaption();
            this.MakeCaptionPadding();
            this.MakeMonthList();
            this.MakeDayList();
            this.MakeYearList();
            Controls.Clear();
            Controls.Add(lblCaption);
            Controls.Add(lblCaptionPadding);
            Controls.Add(ddlMonth);
            Controls.Add(new LiteralControl("&nbsp;"));
            Controls.Add(ddlDay);
            Controls.Add(new LiteralControl("&nbsp;"));
            Controls.Add(ddlYear);
 
            // Register client script:
            ddlMonth.Attributes.Add("onchange", "adjustDay('" + ddlMonth.ClientID + "','" + ddlDay.ClientID + "','" + ddlYear.ClientID + "');");
            ddlDay.Attributes.Add("onchange", "adjustDay('" + ddlMonth.ClientID + "','" + ddlDay.ClientID + "','" + ddlYear.ClientID + "');");
            ddlYear.Attributes.Add("onchange", "adjustDay('" + ddlMonth.ClientID + "','" + ddlDay.ClientID + "','" + ddlYear.ClientID + "');");
        }
 
        private void MakeCaption()
        {
            lblCaption.Text = Caption;
            lblCaption.Width = CaptionWidth;
            if (RightAlignCaption)
                lblCaption.Style["text-align"] = "right";
        }
 
        private void MakeCaptionPadding()
        {
            lblCaptionPadding.Width = CaptionPadding;
        }
 
        private void MakeMonthList()
        {
            ddlMonth.Width = Unit.Pixel(85);
            ddlMonth.Items.Clear();
            ddlMonth.Items.Add("January");
            ddlMonth.Items.Add("February");
            ddlMonth.Items.Add("March");
            ddlMonth.Items.Add("April");
            ddlMonth.Items.Add("May");
            ddlMonth.Items.Add("June");
            ddlMonth.Items.Add("July");
            ddlMonth.Items.Add("August");
            ddlMonth.Items.Add("September");
            ddlMonth.Items.Add("October");
            ddlMonth.Items.Add("November");
            ddlMonth.Items.Add("December");
            ddlMonth.AutoPostBack = AutoPostBack;
            ddlMonth.SelectedIndex = Month - 1;
        }
 
        private void MakeDayList()
        {
            ddlDay.Width = Unit.Pixel(40);
            ddlDay.Items.Clear();
            for (int i = 1; i <= 31; i++)
                ddlDay.Items.Add(i.ToString());
            ddlDay.SelectedValue = Day.ToString();
            ddlDay.AutoPostBack = AutoPostBack;
        }
 
        private void MakeYearList()
        {
            ddlYear.Width = Unit.Pixel(65);
            ddlYear.Items.Clear();
            for (int i = DateTime.Today.Year - 100;
                 i <= DateTime.Today.Year - 18; i++)
                ddlYear.Items.Add(i.ToString());
            ddlYear.SelectedValue = Year.ToString();
            ddlYear.AutoPostBack = AutoPostBack;
        }
 
 
        private void ddlMonth_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.Month = ddlMonth.SelectedIndex + 1;
            this.AdjustDay();
        }
 
        private void ddlDay_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.Day = Convert.ToInt32(ddlDay.SelectedValue);
            this.AdjustDay();
        }
 
        private void ddlYear_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.Year = Convert.ToInt32(ddlYear.SelectedValue);
            this.AdjustDay();
        }
 
        // C# Code behind in DateDDL class:
        private void AdjustDay()
        {
            switch (Month)
            {
                case 4:
                case 6:
                case 9:
                case 11:
                    if (Day > 30) Day = 30;
                    break;
                case 2:
                    int yy = Year;
                    if (Day > 28)
                        Day = (((yy % 4 == 0) && (yy % 100 != 0)) || (yy % 400 == 0)) ? 29 : 28;
                    break;
            }
        }
 
    }
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of guru_sami
guru_sami
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
First one tells me that the it does not see the server tag.

 Parser Error
Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately.

Parser Error Message: Unknown server tag 'OL_DateDLL:OL_DateDLL'.

Source Error:

Line 233:                                                      
Line 234:                                                      <td class="physicalLightBox">&nbsp;
Line 235:                                                      <OL_DateDLL:OL_DateDLL ID="OL

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
namespace OL_DateDLL
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:OL_DateDDL runat=server></{0}:DateDDL>")]
    public class OL_DateDDL : CompositeControl
    {
        private Label lblCaption = new Label();
        private Label lblCaptionPadding = new Label();
        private DropDownList ddlMonth = new DropDownList();
        private DropDownList ddlDay = new DropDownList();
        private DropDownList ddlYear = new DropDownList();
 
        private string caption;
        private Unit captionWidth;
        private Unit captionPadding;
        private bool rightAlignCaption;
 
        private int month;
        private int day;
        private int year;
        private bool autoPostBack;
        private bool dateChangedEventRaised = false;
 
        public event EventHandler DateChanged;
 
        public OL_DateDDL()
        {
            ddlDay.SelectedIndexChanged += new EventHandler(ddlDay_SelectedIndexChanged);
            ddlMonth.SelectedIndexChanged += new EventHandler(ddlMonth_SelectedIndexChanged);
            ddlYear.SelectedIndexChanged += new EventHandler(ddlYear_SelectedIndexChanged);
            month = DateTime.Today.Month;
            day = DateTime.Today.Day;
            year = DateTime.Today.Year;
 
        }
 
        [Category("Appearance")]
        [Description("The caption displayed to the left of the text box.")]
        public string Caption
        {
            get { return caption; }
            set { caption = value; }
        }
 
        [Category("Appearance")]
        [Description("The width of the caption.")]
        public Unit CaptionWidth
        {
            get { return captionWidth; }
            set { captionWidth = value; }
        }
 
        [Category("Appearance")]
        [Description("The space between the caption and the text box.")]
        public Unit CaptionPadding
        {
            get { return captionPadding; }
            set { captionPadding = value; }
        }
 
        [Category("Appearance")]
        [Description("Determines if the caption is right-aligned.")]
        public bool RightAlignCaption
        {
            get { return rightAlignCaption; }
            set { rightAlignCaption = value; }
        }
 
        public DateTime Date
        {
            get
            {
                DateTime date;
                // Attempt to parse the date
                if (!DateTime.TryParse(this.Month + "/" + this.Day + "/" + this.Year, out date))
                {
                    // Invalid date! return MinValue
                    date = DateTime.MinValue;
                }
 
                return date;
            }
            set
            {
                this.Month = value.Month;
                this.Day = value.Day;
                this.Year = value.Year;
            }
        }
 
 
 
        // Add this to the DateDDL class somewhere
        protected virtual void OnDateChanged(EventArgs e)
        {
            // Trigger the event if it's defined,
            // and we haven't triggered it already.
            if (!dateChangedEventRaised && DateChanged != null)
            {
                DateChanged(this, e);
                dateChangedEventRaised = true; // Don't trigger more than once.
            }
        }
 
 
        // Change the property definitions in DateDDL as follows:
        [Category("Misc"), Description("Gets or set the month.")]
        public int Month
        {
            get
            {
                month = ddlMonth.SelectedIndex + 1;
                return month;
            }
            set
            {
                month = value;
                ddlMonth.SelectedIndex = month - 1;
                OnDateChanged(new EventArgs());
            }
        }
 
        [Category("Misc"), Description("Gets or sets the day.")]
        public int Day
        {
            get
            {
                day = ddlDay.SelectedIndex + 1;
                return day;
            }
            set
            {
                day = value;
                ddlDay.SelectedIndex = day - 1;
                OnDateChanged(new EventArgs());
            }
        }
 
        [Category("Misc"), Description("Gets or sets the year.")]
        public int Year
        {
            get
            {
                year = Convert.ToInt32(ddlYear.SelectedValue);
                return year;
            }
            set
            {
                year = value;
                ddlYear.SelectedValue = year.ToString();
                OnDateChanged(new EventArgs());
            }
        }
 
        [Category("Misc")]
        [Description("Determines whether a postback occurs "
            + "when the selection is changed.")]
        [DefaultValue(false)]
        public bool AutoPostBack
        {
            get { return autoPostBack; }
            set { autoPostBack = value; }
        }
 
        protected override void Render(HtmlTextWriter output)
        {
            base.Render(output);
        }
 
        protected override void CreateChildControls()
        {
            this.MakeCaption();
            this.MakeCaptionPadding();
            this.MakeMonthList();
            this.MakeDayList();
            this.MakeYearList();
            Controls.Clear();
            Controls.Add(lblCaption);
            Controls.Add(lblCaptionPadding);
            Controls.Add(ddlMonth);
            Controls.Add(new LiteralControl("&nbsp;"));
            Controls.Add(ddlDay);
            Controls.Add(new LiteralControl("&nbsp;"));
            Controls.Add(ddlYear);
 
            // Register client script:
            ddlMonth.Attributes.Add("onchange", "adjustDay('" + ddlMonth.ClientID + "','" + ddlDay.ClientID + "','" + ddlYear.ClientID + "');");
            ddlDay.Attributes.Add("onchange", "adjustDay('" + ddlMonth.ClientID + "','" + ddlDay.ClientID + "','" + ddlYear.ClientID + "');");
            ddlYear.Attributes.Add("onchange", "adjustDay('" + ddlMonth.ClientID + "','" + ddlDay.ClientID + "','" + ddlYear.ClientID + "');");
        }
 
        private void MakeCaption()
        {
            lblCaption.Text = Caption;
            lblCaption.Width = CaptionWidth;
            if (RightAlignCaption)
                lblCaption.Style["text-align"] = "right";
        }
 
        private void MakeCaptionPadding()
        {
            lblCaptionPadding.Width = CaptionPadding;
        }
 
        private void MakeMonthList()
        {
            ddlMonth.Width = Unit.Pixel(85);
            ddlMonth.Items.Clear();
            ddlMonth.Items.Add("January");
            ddlMonth.Items.Add("February");
            ddlMonth.Items.Add("March");
            ddlMonth.Items.Add("April");
            ddlMonth.Items.Add("May");
            ddlMonth.Items.Add("June");
            ddlMonth.Items.Add("July");
            ddlMonth.Items.Add("August");
            ddlMonth.Items.Add("September");
            ddlMonth.Items.Add("October");
            ddlMonth.Items.Add("November");
            ddlMonth.Items.Add("December");
            ddlMonth.AutoPostBack = AutoPostBack;
            ddlMonth.SelectedIndex = Month - 1;
        }
 
        private void MakeDayList()
        {
            ddlDay.Width = Unit.Pixel(40);
            ddlDay.Items.Clear();
            for (int i = 1; i <= 31; i++)
                ddlDay.Items.Add(i.ToString());
            ddlDay.SelectedValue = Day.ToString();
            ddlDay.AutoPostBack = AutoPostBack;
        }
 
        private void MakeYearList()
        {
            ddlYear.Width = Unit.Pixel(65);
            ddlYear.Items.Clear();
            for (int i = DateTime.Today.Year - 100;
                 i <= DateTime.Today.Year - 18; i++)
                ddlYear.Items.Add(i.ToString());
            ddlYear.SelectedValue = Year.ToString();
            ddlYear.AutoPostBack = AutoPostBack;
        }
 
 
        private void ddlMonth_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.Month = ddlMonth.SelectedIndex + 1;
            this.AdjustDay();
        }
 
        private void ddlDay_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.Day = Convert.ToInt32(ddlDay.SelectedValue);
            this.AdjustDay();
        }
 
        private void ddlYear_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.Year = Convert.ToInt32(ddlYear.SelectedValue);
            this.AdjustDay();
        }
 
        // C# Code behind in DateDDL class:
        private void AdjustDay()
        {
            switch (Month)
            {
                case 4:
                case 6:
                case 9:
                case 11:
                    if (Day > 30) Day = 30;
                    break;
                case 2:
                    int yy = Year;
                    if (Day > 28)
                        Day = (((yy % 4 == 0) && (yy % 100 != 0)) || (yy % 400 == 0)) ? 29 : 28;
                    break;
            }
        }
 
    }
}

Open in new window