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 / Exemple :
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;
}
}
}
Vous n'êtes pas encore membre ?
inscrivez-vous, c'est gratuit et ça prend moins d'une minute !
Les membres obtiennent plus de réponses que les utilisateurs anonymes.
Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.
Le fait d'être membre vous permet d'avoir des options supplémentaires.