begin process at 2012 02 14 05:29:48
  Trouver un code source :
 
dans
 
Accueil > 

Code

 > 

ASP.Net

 > CUSTOM DATEPIKER DÉRIVANT DE COMPOSITECONTROL

CUSTOM DATEPIKER DÉRIVANT DE COMPOSITECONTROL


 Information sur la source

Note :
Aucune note
Catégorie :ASP.Net Source .NET ( DotNet ) Classé sous :datepiker, custom datepiker, usercontrol like, datepiker aspnet, CompositeControl Niveau :Initié Date de création :20/10/2009 Date de mise à jour :25/09/2010 11:59:06 Vu / téléchargé :5 579 / 187

Auteur : fredzool

Ecrire un message privé
Site perso
Commentaire sur cette source (0)
Ajouter un commentaire et/ou une note

 Description

un date piker similaire au datepier textbox
mais avec en plus une icone pour le piker

on voit dans ce bout de code comment integer du javascript et des images dans un control

il y a d'autres controls dans la solution

on peut integrer plusieurs controls dans un CompositeControl

Source

  • namespace WebControlLibrary1
  • {
  • using System;
  • using System.Collections.Generic;
  • using System.ComponentModel;
  • using System.Text;
  • using System.Web.UI;
  • using System.Web.UI.WebControls;
  • [DefaultProperty("Text")]
  • [ToolboxData("<{0}:CustomDatePicker runat=server></{0}:CustomDatePicker>")]
  • [ValidationPropertyAttribute("ValidationString")]
  • public class CustomDatePicker : CompositeControl
  • {
  • //To retrieve value i am using textbox
  • private TextBox _TxtDate = new TextBox();
  • // Image to select the calender date
  • private Image _ImgDate = new Image();
  • // Image URL to expose the image URL Property
  • private string _ImageUrl;
  • //date validator
  • private CompareValidator _dateValidator = new CompareValidator();
  • //RequiredFieldValidator
  • private RequiredFieldValidator _requiredFieldValidator;
  • //error mesage for date
  • private string _errorMessage = "";
  • //localisation
  • private string _localisation = "French";
  • #region properties
  • /// <summary>
  • /// DateFormat
  • /// </summary>
  • [Category("Appearance")]
  • [Description("Date format.")]
  • [Browsable(true)]
  • public string DateFormat = "dd/MM/yyyy";
  • /// <summary>
  • /// Gets or sets the localisation.
  • /// </summary>
  • /// <value>The localisation.</value>
  • [Category("Appearance")]
  • [Description("French or English.")]
  • [Browsable(true)]
  • public string Localisation
  • {
  • get { return _localisation; }
  • set { _localisation = value; }
  • }
  • [Category("Appearance")]
  • [Description("Calendar error validation message : empty = no validation")]
  • [Browsable(true)]
  • public string ErrorMessage
  • {
  • get { return _errorMessage; }
  • set { _errorMessage = value; }
  • }
  • /// <summary>
  • /// Gets or sets the text CSS class.
  • /// </summary>
  • /// <value>The text CSS class.</value>
  • [Category("Appearance")]
  • [Description("CSS class name applied to the text box.")]
  • [Browsable(true)]
  • public string TextCssClass
  • {
  • get { return base.CssClass; }
  • set { base.CssClass = value; }
  • }
  • /// <summary>
  • /// Gets or sets the content of the textbox which represents a date.
  • /// </summary>
  • [Bindable(true, BindingDirection.TwoWay)]
  • [Browsable(true)]
  • public string CalendarDateString
  • {
  • get
  • {
  • return Text;
  • }
  • set
  • {
  • Text = value;
  • DateTime date;
  • if (DateTime.TryParseExact(value, DateFormat, null, System.Globalization.DateTimeStyles.None, out date))
  • {
  • if (date.Date == DateTime.MaxValue.Date)
  • {
  • Text = "";
  • }
  • }
  • }
  • }
  • /// <summary>
  • /// Gets or sets a DateTime representation of the currently selected date.
  • /// </summary>
  • [Bindable(true, BindingDirection.TwoWay)]
  • [Browsable(true)]
  • [Description("Date contenue")]
  • public DateTime CalendarDate
  • {
  • get
  • {
  • DateTime date;
  • if (DateTime.TryParseExact(Text, DateFormat, null, System.Globalization.DateTimeStyles.None, out date))
  • {
  • return date;
  • }
  • return DateTime.MaxValue;
  • }
  • set
  • {
  • Text = value.ToString(DateFormat);
  • }
  • }
  • /// <summary>
  • /// Gets or sets the text content of the <see cref="T:System.Web.UI.WebControls.TextBox"/> control.
  • /// </summary>
  • /// <value></value>
  • /// <returns>The text displayed in the <see cref="T:System.Web.UI.WebControls.TextBox"/> control. The default is an empty string ("").</returns>
  • [Bindable(true), Category("Appearance"), DefaultValue(""), Localizable(true)]
  • public string Text
  • {
  • get
  • {
  • String s = (String)ViewState["Text"];
  • return ((s == null) ? string.Empty : s);
  • }
  • set
  • {
  • ViewState["Text"] = value;
  • }
  • }
  • /// <summary>
  • /// Sets the image URL.
  • /// </summary>
  • /// <value>The image URL.</value>
  • [Category("Appearance")]
  • [Bindable(true, BindingDirection.TwoWay)]
  • [Browsable(true)]
  • [Description("Image du bouton calendrier")]
  • public string ImageUrl
  • {
  • set
  • {
  • this._ImageUrl = value;
  • }
  • }
  • #endregion properties
  • /// <summary>
  • /// Registers client script for generating postback events prior to rendering on the client, if <see cref="P:System.Web.UI.WebControls.TextBox.AutoPostBack"/> is true.
  • /// </summary>
  • /// <param name="e">An <see cref="T:System.EventArgs"/> that contains the event data.</param>
  • protected override void OnPreRender(EventArgs e)
  • {
  • string scriptName = "TextBoxDatePicker";
  • if (!Page.ClientScript.IsClientScriptBlockRegistered(scriptName))
  • {
  • _TxtDate.ID = this.ID + "t";
  • string jsPopupCalendarFileName = "popcalendar.js";
  • //bouble click on text box
  • string scriptStr = "javascript:return popUpCalendar(this, '" + ResolveUrl("cal") + "/', document.getElementById('" + _TxtDate.ClientID + @"'), '" + DateFormat + "', '" + _localisation + "')";
  • System.Diagnostics.Debug.WriteLine("_TxtDate.ClientID " + _TxtDate.ClientID);
  • _TxtDate.Attributes.Add("ondblclick", scriptStr);
  • this.Page.ClientScript.RegisterClientScriptInclude(
  • this.GetType(), "Test",
  • Page.ClientScript.GetWebResourceUrl(this.GetType(),
  • string.Format("WebControlLibrary1.cal.{0}", jsPopupCalendarFileName)));
  • //click on image
  • scriptStr = "javascript:return popUpCalendar(this, '" + ResolveUrl("cal") + "/', document.getElementById('" + _TxtDate.ClientID + @"'), '" + DateFormat + "','" + _localisation + "')";
  • _ImgDate.Attributes.Add("onclick", scriptStr);
  • this.Page.ClientScript.RegisterClientScriptInclude(
  • this.GetType(), "Test",
  • Page.ClientScript.GetWebResourceUrl(this.GetType(),
  • string.Format("WebControlLibrary1.cal.{0}", jsPopupCalendarFileName)));
  • // create the style sheet control
  • // and put it in the document header
  • string csslink = "<link href='" +
  • Page.ClientScript.GetWebResourceUrl(this.GetType(),
  • "WebControlLibrary1.cal.popcalendar.css")
  • + "' rel='stylesheet' type='text/css' />";
  • LiteralControl include = new LiteralControl(csslink);
  • this.Page.Header.Controls.Add(include);
  • }
  • base.OnPreRender(e);
  • }
  • /// <summary>
  • /// Gets the validation string.
  • /// </summary>
  • /// <value>The validation string.</value>
  • public string ValidationString
  • {
  • get
  • {
  • if (_TxtDate.Text == string.Empty)
  • {
  • return "";
  • }
  • else
  • {
  • return _TxtDate.Text;
  • }
  • }
  • }
  • /// <summary>
  • /// Raises the <see cref="E:System.Web.UI.Control.Init"/> event.
  • /// </summary>
  • /// <param name="e">An <see cref="T:System.EventArgs"/> object that contains the event data.</param>
  • protected override void OnInit(EventArgs e)
  • {
  • base.OnInit(e);
  • // For TextBox
  • // setting name for textbox. using t just to concat with this.ID for unqiueName
  • _TxtDate.ID = this.ID + "t";
  • // For Image
  • // setting alternative name for image
  • _ImgDate.AlternateText = "imageURL";
  • if (!string.IsNullOrEmpty(_ImageUrl))
  • _ImgDate.ImageUrl = _ImageUrl;
  • else
  • {
  • _ImgDate.ImageUrl = Page.ClientScript.GetWebResourceUrl(this.GetType(), "WebControlLibrary1.cal.calendar.gif");
  • }
  • //setting name for image
  • _ImgDate.ID = this.ID + "i";
  • //setting image class for textbox
  • _ImgDate.Attributes.Add("class", this.CssClass);
  • if (!string.IsNullOrEmpty( _errorMessage))
  • {
  • //date validator
  • _dateValidator.ID = this.ID + "v";
  • _dateValidator.ErrorMessage = _errorMessage;
  • _dateValidator.ControlToValidate = _TxtDate.UniqueID;
  • _dateValidator.Operator = ValidationCompareOperator.DataTypeCheck; ;
  • _dateValidator.Type = ValidationDataType.Date;
  • }
  • }
  • /// <summary>
  • /// adding child controls to composite control
  • /// </summary>
  • protected override void CreateChildControls()
  • {
  • this.Controls.Add(_TxtDate);
  • this.Controls.Add(_ImgDate);
  • if (!string.IsNullOrEmpty(_errorMessage))
  • this.Controls.Add(_dateValidator);
  • base.CreateChildControls();
  • }
  • // Get the id of the control rendered on client side
  • // Very essential for Javascript Calendar scripts to locate the textbox
  • public string getClientID()
  • {
  • return base.ClientID;
  • }
  • }
  • }
namespace WebControlLibrary1
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Text;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    [DefaultProperty("Text")]
    [ToolboxData("<{0}:CustomDatePicker runat=server></{0}:CustomDatePicker>")]
    [ValidationPropertyAttribute("ValidationString")]
    public class CustomDatePicker : CompositeControl
    {

        //To retrieve value i am using textbox
        private TextBox _TxtDate = new TextBox();
        // Image to select the calender date
        private Image _ImgDate = new Image();
        // Image URL to expose the image URL Property
        private string _ImageUrl;
        //date validator
        private CompareValidator _dateValidator = new CompareValidator();
        //RequiredFieldValidator
        private RequiredFieldValidator _requiredFieldValidator;
        //error mesage for date
        private string _errorMessage = "";
        //localisation
        private string _localisation = "French";

        #region properties

        /// <summary>
        /// DateFormat
        /// </summary>
        [Category("Appearance")]
        [Description("Date format.")]
        [Browsable(true)]
        public string DateFormat = "dd/MM/yyyy";

        /// <summary>
        /// Gets or sets the localisation.
        /// </summary>
        /// <value>The localisation.</value>
        [Category("Appearance")]
        [Description("French or English.")]
        [Browsable(true)]
        public string Localisation
        {
            get { return _localisation; }
            set { _localisation = value; }
        }

        [Category("Appearance")]
        [Description("Calendar error validation message : empty = no validation")]
        [Browsable(true)]
        public string ErrorMessage
        {
            get { return _errorMessage; }
            set { _errorMessage = value; }
        }

        /// <summary>
        /// Gets or sets the text CSS class.
        /// </summary>
        /// <value>The text CSS class.</value>
        [Category("Appearance")]
        [Description("CSS class name applied to the text box.")]
        [Browsable(true)]
        public string TextCssClass
        {
            get { return base.CssClass; }
            set { base.CssClass = value; }
        }

        /// <summary>
        /// Gets or sets the content of the textbox which represents a date.
        /// </summary>
        [Bindable(true, BindingDirection.TwoWay)]
        [Browsable(true)]
        public string CalendarDateString
        {
            get
            {
                return Text;
            }
            set
            {
                Text = value;
                DateTime date;
                if (DateTime.TryParseExact(value, DateFormat, null, System.Globalization.DateTimeStyles.None, out date))
                {
                    if (date.Date == DateTime.MaxValue.Date)
                    {
                        Text = "";
                    }
                }
            }
        }

        /// <summary>
        /// Gets or sets a DateTime representation of the currently selected date.
        /// </summary>
        [Bindable(true, BindingDirection.TwoWay)]
        [Browsable(true)]
        [Description("Date contenue")]
        public DateTime CalendarDate
        {
            get
            {
                DateTime date;
                if (DateTime.TryParseExact(Text, DateFormat, null, System.Globalization.DateTimeStyles.None, out date))
                {
                    return date;
                }
                return DateTime.MaxValue;
            }
            set
            {
                Text = value.ToString(DateFormat);
            }
        }

        /// <summary>
        /// Gets or sets the text content of the <see cref="T:System.Web.UI.WebControls.TextBox"/> control.
        /// </summary>
        /// <value></value>
        /// <returns>The text displayed in the <see cref="T:System.Web.UI.WebControls.TextBox"/> control. The default is an empty string ("").</returns>
        [Bindable(true), Category("Appearance"), DefaultValue(""), Localizable(true)]
        public string Text
        {
            get
            {
                String s = (String)ViewState["Text"];
                return ((s == null) ? string.Empty : s);
            }

            set
            {
                ViewState["Text"] = value;
            }
        }

        /// <summary>
        /// Sets the image URL.
        /// </summary>
        /// <value>The image URL.</value>
        [Category("Appearance")]
        [Bindable(true, BindingDirection.TwoWay)]
        [Browsable(true)]
        [Description("Image du bouton calendrier")]
        public string ImageUrl
        {
            set
            {
                this._ImageUrl = value;
            }
        }

        #endregion properties

        /// <summary>
        /// Registers client script for generating postback events prior to rendering on the client, if <see cref="P:System.Web.UI.WebControls.TextBox.AutoPostBack"/> is true.
        /// </summary>
        /// <param name="e">An <see cref="T:System.EventArgs"/> that contains the event data.</param>
        protected override void OnPreRender(EventArgs e)
        {
            string scriptName = "TextBoxDatePicker";
            if (!Page.ClientScript.IsClientScriptBlockRegistered(scriptName))
            {
                _TxtDate.ID = this.ID + "t";

                string jsPopupCalendarFileName = "popcalendar.js";

                //bouble click on text box
                string scriptStr = "javascript:return popUpCalendar(this, '" + ResolveUrl("cal") + "/', document.getElementById('" + _TxtDate.ClientID + @"'), '" + DateFormat + "', '" + _localisation + "')";
                System.Diagnostics.Debug.WriteLine("_TxtDate.ClientID  " + _TxtDate.ClientID);
                _TxtDate.Attributes.Add("ondblclick", scriptStr);
                this.Page.ClientScript.RegisterClientScriptInclude(
                this.GetType(), "Test",
                Page.ClientScript.GetWebResourceUrl(this.GetType(),
                string.Format("WebControlLibrary1.cal.{0}", jsPopupCalendarFileName)));

                //click on image
                scriptStr = "javascript:return popUpCalendar(this, '" + ResolveUrl("cal") + "/', document.getElementById('" + _TxtDate.ClientID + @"'), '" + DateFormat + "','" + _localisation + "')";

                _ImgDate.Attributes.Add("onclick", scriptStr);
                this.Page.ClientScript.RegisterClientScriptInclude(
                this.GetType(), "Test",
                Page.ClientScript.GetWebResourceUrl(this.GetType(),
                string.Format("WebControlLibrary1.cal.{0}", jsPopupCalendarFileName)));

                // create the style sheet control
                // and put it in the document header
                string csslink = "<link href='" +
                  Page.ClientScript.GetWebResourceUrl(this.GetType(),
                   "WebControlLibrary1.cal.popcalendar.css")
                  + "' rel='stylesheet' type='text/css' />";
                LiteralControl include = new LiteralControl(csslink);
                this.Page.Header.Controls.Add(include);
            }

            base.OnPreRender(e);
        }

        /// <summary>
        /// Gets the validation string.
        /// </summary>
        /// <value>The validation string.</value>
        public string ValidationString
        {
            get
            {
                if (_TxtDate.Text == string.Empty)
                {
                    return "";
                }
                else
                {
                    return _TxtDate.Text;
                }
            }
        }

        /// <summary>
        /// Raises the <see cref="E:System.Web.UI.Control.Init"/> event.
        /// </summary>
        /// <param name="e">An <see cref="T:System.EventArgs"/> object that contains the event data.</param>
        protected override void OnInit(EventArgs e)
        {

            base.OnInit(e);

            // For TextBox
            // setting name for textbox. using t just to concat with this.ID for unqiueName
            _TxtDate.ID = this.ID + "t";

            // For Image
            // setting alternative name for image
            _ImgDate.AlternateText = "imageURL";
            if (!string.IsNullOrEmpty(_ImageUrl))
                _ImgDate.ImageUrl = _ImageUrl;
            else
            {
                _ImgDate.ImageUrl = Page.ClientScript.GetWebResourceUrl(this.GetType(), "WebControlLibrary1.cal.calendar.gif");
            }

            //setting name for image
            _ImgDate.ID = this.ID + "i";
            //setting image class for textbox
            _ImgDate.Attributes.Add("class", this.CssClass);


            if (!string.IsNullOrEmpty( _errorMessage))
            {
                //date validator
                _dateValidator.ID = this.ID + "v";
                _dateValidator.ErrorMessage = _errorMessage;
                _dateValidator.ControlToValidate = _TxtDate.UniqueID;
                _dateValidator.Operator = ValidationCompareOperator.DataTypeCheck; ;
                _dateValidator.Type = ValidationDataType.Date;
            }
            
        }

        /// <summary>
        /// adding child controls to composite control
        /// </summary>
        protected override void CreateChildControls()
        {
            this.Controls.Add(_TxtDate);
            this.Controls.Add(_ImgDate);
            if (!string.IsNullOrEmpty(_errorMessage))
                this.Controls.Add(_dateValidator);
            base.CreateChildControls();
        }

        // Get the id of the control rendered on client side
        // Very essential for Javascript Calendar scripts to locate the textbox
        public string getClientID()
        {
            return base.ClientID;
        }
    }
}


 Fichier Zip

Les Membres Club peuvent télécharger directement un fichier contenu dans le zip sans télécharger le zip en entier !

Télécharger le zip


 Historique

20 octobre 2009 15:13:57 :
ajout de la solution
20 octobre 2009 19:30:55 :
ajout d'un RequiredFieldValidator
25 septembre 2010 11:59:09 :
J'ai ajoute un hiddenfield, la date est directement accessible au premier postback

 Sources du même auteur

Source avec Zip CHECKED DROPDOWNLIST
Source avec Zip Source avec une capture Source .NET (Dotnet) GRIDVIEW WITH TREEVIEW AND CALLBACK
Source avec Zip APPELLER UN WEBSERVICE DEPUIS JAVASCRIPT
Source avec Zip Source .NET (Dotnet) MONEY TEXTBOX WITH EMBEDED JAVASCRIPT
Source avec Zip Source .NET (Dotnet) BOUTON QUI EMPECHE LE MULTI CLIC AVANT LA FIN DU TRAITEMENT....

 Sources de la même categorie

Source avec Zip Source .NET (Dotnet) GUESTBOOK AVEC GRIDVIEW par DanMor498
Source avec Zip CHECKED DROPDOWNLIST par fredzool
Source avec Zip Source avec une capture Source .NET (Dotnet) GRIDVIEW WITH TREEVIEW AND CALLBACK par fredzool
Source avec Zip APPELLER UN WEBSERVICE DEPUIS JAVASCRIPT par fredzool
Source avec Zip Source .NET (Dotnet) MONEY TEXTBOX WITH EMBEDED JAVASCRIPT par fredzool

Commentaires et avis

Aucun commentaire pour le moment.

 Ajouter un commentaire




Nos sponsors


Sondage...

CalendriCode

Février 2012
LMMJVSD
  12345
6789101112
13141516171819
20212223242526
272829    

Consulter la suite du CalendriCode

Photothèque

 
Développement réalisé par Nicolas SOREL (Nix) avec l'aide de : Cyril DURAND et Emmanuel (EBArtSoft), Merci à Vincent pour ses précieux conseils.
CodeS-SourceS.com© Toute reproduction même partielle est interdite sauf accord écrit du Webmaster
CodeS-SourceS.com© est une marque déposée tous droits réservés

Google Coop CodeS-SourceS Google Coop CodeS-SourceS
Temps d'éxécution de la page : 0,624 sec (4)

Nous contacter | Annoncer sur CodeS-SourceS | Mentions légales