Calendar Popup Custom Control

I have to maintain existing ASP.NET application that used eWorld.UI.CalendarPopup custom control to show calendar in a small popup window.

The users complained that the popup window not always rendered correctly if shown in front of other controls and if the control is on the bottom of visible part of the browser window, most of popup calendar is cut off.

I’ve tried to download the latest version of the eWorld.UI.CalendarPopup  and found that it is currently not available.

I’ve tried a few free controls from http://www.411asp.net/home/assembly/datetime and from http://weblogs.asp.net/datagridgirl/archive/2004/02/05/68105.aspx

but most of them do not position well when the contol is located near the bootom of the page.

Finally I found Matt Kruse JavaScript Calendar Popup control (PLEASE SUPPORT HIS SITE) that works as it is required.

I’ve created an ASP.Net custom control as a wrupper to Matt’s JavaScript to make it easier to include in ASP.NET pages.

My custom control has only a few properties that I needed and doesn’t expose all options that Matt’s JavaScript allowed.

If required it can be extended.

      ///

      /// DatePicker -one more attempt to do the same things

      /// it’s ASP.NET wrapper for Matt Kruse Calendar Popup http://www.mattkruse.com/javascript/calendarpopup/index.html

      /// I had to replace eWorld.UI.CalendarPopup

      ///

      // TODO : if the control is in the frame, it can position far below the parent control (see DSPAddSchoolInfo)

      [DefaultProperty(“Text”),

            ToolboxData(“<{0}:DatePicker runat=server>”)]

      public class DatePicker :  Control, INamingContainer 

      { //,IPostBackDataHandler

            //private string m_text;use ViewState instead

            public DatePicker() : base()

            {

            }

            #region   “Private properties”

            private TextBox m_txtDate;

            private HyperLink  m_lnkSelect;

            #endregion  // “Private properties”

 

#region   “Public properties”

           

            [Bindable(true),

                  Category(“Appearance”),

                  DefaultValue(“”),

            EditorAttribute(typeof(System.Web.UI.Design.ImageUrlEditor), typeof(UITypeEditor))]

            public   string ImageUrl 

            {

                  get

                  {

                        this.EnsureChildControls();

                        return m_lnkSelect.ImageUrl;

                  }

                  set

                  { 

                        this.EnsureChildControls();

                        m_lnkSelect.ImageUrl = value;

                  }

            }

 

            [Bindable(true),

            Category(“Appearance”),

            DefaultValue(“”)

            ]

            public  string Text

            {

                  get

                  {

                        return TextBox.Text;

                  }

                  set

                  { 

                        TextBox.Text = value;

                  }

            }

///

/// Property TextBox (TextBox)

///

public TextBox TextBox

{

      get

      {

            this.EnsureChildControls();

            return this.m_txtDate;

      }

      set

      {

            this.EnsureChildControls();

            this.m_txtDate = value;

      }

}

            [Category(“Data”), Bindable(true)]

            public DateTime SelectedDate

            {

                  get

                  {

                       

                        if (DataHelper.IsNullOrEmpty(TextBox.Text)==false)

                        {

                              return (DateTime) Convert.ToDateTime(TextBox.Text) ;

                        }

                        return new DateTime();

                  }

                  set

                  {

                        TextBox.Text = value.ToShortDateString() ;

                  }

            }

 

            private const  string  cnstJavaScriptUrlName  = “JavaScriptUrl”;

            ///

            /// ViewState Property JavaScriptUrl

            ///

            public string JavaScriptUrl

            {

                  get

                  {

                        return  (string)ViewState[cnstJavaScriptUrlName];

                  }

                  set

                  {

                              ViewState[cnstJavaScriptUrlName] = value;

                  }

            }

            #endregion  // “Public properties”

 

            ///

            /// Overrides

            ///

            protected override void OnLoad(System.EventArgs e)

            {

                  base.OnLoad(e);

                  this.EnsureChildControls();

                  InitCalendarPopupControl(this.m_txtDate,this.m_lnkSelect );

            }

 

            protected override void CreateChildControls()

            {

 

                  m_txtDate = new TextBox();

                  m_txtDate.ID=”txtDate”;

                  this.Controls.Add(m_txtDate);

                  m_lnkSelect = new HyperLink();

                  m_lnkSelect.ID=”lnkSelect”;

                  this.Controls.Add(m_lnkSelect);

            }

            protected void InitCalendarPopupControl(TextBox txtDate,HyperLink  lnkSelect)

            { //from http://www.mattkruse.com/javascript/calendarpopup/index.html)

                  Page page=txtDate.Page;

                  string sClndrId=”clndr”+txtDate.ID;

                  string sDecl=”var ” + sClndrId + ” = new CalendarPopup();”;

                  page.RegisterStartupScript( sClndrId,JScriptHelper.JScript(sDecl));

                  string srcPath=JavaScriptUrl;// “~/js/CalendarPopup.js”;

                  if(DataHelper.IsNullOrEmpty(srcPath))

                  {

                    srcPath= ResSrvHandler.ResourceUrl(“CalendarPopup.js”,true );  

                  }

                  JScriptHelper.RegisterStartupScriptUrl(page,srcPath);

                  string sJScriptSrc=@”
<script language="" src=" + page.ResolveUrl(srcPath) +  “>
“;

                  page.RegisterClientScriptBlock( “CalendarPopup.js”,sJScriptSrc);

                  //          “cal.select(document.forms[‘example’].date1,’anchor1′,’MM/dd/yyyy’); return false;”

                  string sInvokePopupCal=sClndrId+”.select(document.forms[0].” + txtDate.ClientID + “,'” + lnkSelect.ClientID  + “‘,’dd/MM/yyyy’)”;

                  page.RegisterStartupScript( sClndrId,sDecl);

                  //very similar to DNN C:Visual Studio ProjectsFuncSolnFSDNNcomponentsSharedCalendar.vb

                  lnkSelect.NavigateUrl=”javascript:” + sInvokePopupCal;

            }

 

 

      }

 

  

Advertisements