Accueil > > > CUSTOM DATEPIKER DÉRIVANT DE COMPOSITECONTROL
CUSTOM DATEPIKER DÉRIVANT DE COMPOSITECONTROL
Information sur la source
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;
}
}
}
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
Sources de la même categorie
Commentaires et avis
|
Derniers Blogs
XNA IS DEAD!XNA IS DEAD! par richardc
Depuis la semaine dernière (et grâce aux TechDays 2012), je me penche activement sur la nouvelle version de Windows, aka Windows 8. Vous me direz, il était temps puisque la première preview date de Septembre dernier.
OK. Remarquez, on n'en est qu'aux...
Cliquez pour lire la suite de l'article par richardc TECHDAYS PARIS 2012 : WINDOWS SERVER "8" QUOI DE 9 !TECHDAYS PARIS 2012 : WINDOWS SERVER "8" QUOI DE 9 ! par ROMELARD Fabrice
Speakers: Fabrice Meillon et Stanislas Quastana Cette session est basée entièrement sur celle donnée lors de la BUILD cet hiver. Il n'y a pas d'ajout d'information en rapport avec cet évènement passé. Windows 8 Server sera intégralem...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice [HTML5] AUTOUR DU W3C : NOUVEAUX STANDARDS ET WEB MOBILE (LILLE)[HTML5] AUTOUR DU W3C : NOUVEAUX STANDARDS ET WEB MOBILE (LILLE) par Gio
Je m'y prends un peu tard je sais, mais bon je suis développeur web et donc hyper fainéant ! Toujours dans le cadre des technologies émergentes, ici HTML5, parce qu'on aime HTML5 chez Wyg , nous seront présent, le vieux ( Aurélien V.) et moi, pour pr...
Cliquez pour lire la suite de l'article par Gio [WP7] DYNAMICALLY CHANGE STARTUP PAGE[WP7] DYNAMICALLY CHANGE STARTUP PAGE par KooKiz
Let's say that you want to allow the user to customize the startup page of your application. You can easily change the startup page by editing the 'NavigationPage' attribute in the manifest file. But the manifest cannot be modified once the applicatio...
Cliquez pour lire la suite de l'article par KooKiz
Forum
RE : FORMULAIRERE : FORMULAIRE par ap24dp
Cliquez pour lire la suite par ap24dp RE : FORMULAIRERE : FORMULAIRE par jopop
Cliquez pour lire la suite par jopop RE : FORMULAIRERE : FORMULAIRE par ap24dp
Cliquez pour lire la suite par ap24dp RE : FORMULAIRERE : FORMULAIRE par jopop
Cliquez pour lire la suite par jopop
Logiciels
DocTranslate (V3.1.0.0)DOCTRANSLATE (V3.1.0.0)DocTranslate est un traducteur de document Microsoft Word, PowerPoint et Excel. Il permet d'autom... Cliquez pour télécharger DocTranslate Tribler (2012)TRIBLER (2012)Tribler est un client pair à pair (P2P/Peer-to-Peer) open source avec la capacité de regarder des... Cliquez pour télécharger Tribler OneSwarm (2012)ONESWARM (2012)Le peer-to-peer qui protège votre vie privée, c'est OneSwarm.
Ce logiciel de peer-to-peer crypté... Cliquez pour télécharger OneSwarm PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V8.4)PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V8.4)PONAMEDIA TV DEVIENS HELLLOOO FLASH
LA TV SUR VOTRE ORDINATEUR.
Toute une plateforme Multi... Cliquez pour télécharger PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO Academy System (17.2.1.0)ACADEMY SYSTEM (17.2.1.0)Logiciel de gestion des établissements.
- élèves/étudiants (inscription, dossier, absence...)
-... Cliquez pour télécharger Academy System
|