Skip to main content

Date Picker control for ASP.net using Dropdown control of DD,MM,YYYY

Date Picker control for ASP.net using Dropdown control of DD,MM,YYYY


.aspx File


<asp:DropDownList ID="ddlDay" Width="60px" runat="server">
</asp:DropDownList>
<asp:DropDownList ID="ddlMonth" Width="120px" runat="server">
</asp:DropDownList>
<asp:DropDownList ID="ddlYear" Width="80px" runat="server">
</asp:DropDownList>
&nbsp;
<%--<asp:Image ID="imgCalendar" runat="server" ImageUrl="~/Images/calendar.gif"
    EnableViewState="False"></asp:Image>--%>
<asp:Label ID="lblError" runat="server" CssClass="error" Visible="False" EnableViewState="False"><BR />The date is not valid.</asp:Label>




.cs File


public partial class DateTimePicker : System.Web.UI.UserControl
{
// Fields
private DateTime _dateTime;
private int _endYear = -1;
private bool _isValid;
private int _startYear = 0x7d0;

// Methods
private void BuildDropDownLists()
{
this.ddlDay.Items.Clear();
this.ddlMonth.Items.Clear();
this.ddlYear.Items.Clear();
for (int i = 1; i <= 0x1f; i++)
{
this.ddlDay.Items.Add(new ListItem(i.ToString(), i.ToString()));
}
for (int j = 1; j <= 12; j++)
{
DateTime time = new DateTime(0x7d0, j, 1);
this.ddlMonth.Items.Add(new ListItem(time.ToString("MMMM"), j.ToString()));
}
for (int k = this._startYear; k <= this.EndYear; k++)
{
this.ddlYear.Items.Add(new ListItem(k.ToString(), k.ToString()));
}
}

public override void DataBind()
{
base.DataBind();
this.ddlDay.SelectedIndex = this._dateTime.Day - 1;
this.ddlMonth.SelectedIndex = this._dateTime.Month - 1;
if ((this._dateTime.Year >= this._startYear) && (this._dateTime.Year <= this.EndYear))
{
this.ddlYear.SelectedIndex = this._dateTime.Year - this._startYear;
}
else
{
this.ddlYear.SelectedIndex = 0;
}
}

private int GetEndYear()
{
if (this._endYear != -1)
{
return this._endYear;
}
return DateTime.Now.Year;
}

private void InitializeComponent()
{
}

protected override void OnInit(EventArgs e)
{
this.InitializeComponent();
base.OnInit(e);
this.BuildDropDownLists();
}

protected void Page_Load(object sender, EventArgs e)
{
//this.imgCalendar.Attributes["onclick"] = "DisplayDatePicker('" + this.ddlDay.UniqueID + "','" + this.ddlMonth.UniqueID + "','" + this.ddlYear.UniqueID + "','" + this._startYear.ToString() + "');";
//this.imgCalendar.Attributes["style"] = "cursor: pointer;";
if (!this.Page.IsClientScriptBlockRegistered("DateTimePicker"))
{
this.Page.RegisterClientScriptBlock("DateTimePicker", "");
}
if (!base.IsPostBack)
{
this.DataBind();
}
else
{
this._startYear = Convert.ToInt32(this.ddlYear.Items[0].Value);
this._endYear = Convert.ToInt32(this.ddlYear.Items[this.ddlYear.Items.Count - 1].Value);
int day = Convert.ToInt32(this.ddlDay.SelectedItem.Value);
int month = Convert.ToInt32(this.ddlMonth.SelectedItem.Value);
int year = Convert.ToInt32(this.ddlYear.SelectedItem.Value);
try
{
this._dateTime = new DateTime(year, month, day);
if ((year >= this._startYear) && (year <= this.EndYear))
{
this._isValid = true;
}
else
{
this._isValid = false;
}
}
catch (ArgumentOutOfRangeException)
{
this._isValid = false;
}
this.lblError.Visible = !this._isValid;
}
}

// Properties
public DateTime DateTime
{
get
{
return this._dateTime;
}
set
{
this._dateTime = value;
}
}

public int EndYear
{
get
{
return this.GetEndYear();
}
set
{
this._endYear = value;
this.BuildDropDownLists();
}
}

public bool IsValid
{
get
{
return this._isValid;
}
}

public int StartYear
{
get
{
return this._startYear;
}
set
{
this._startYear = value;
this.BuildDropDownLists();
}
}
}

Comments

Popular posts from this blog

Message Helper for MVC Application

Message Helper for MVC Application public static class MessageExtensions { public enum MessageType { Success = 0, Info = 1, Warning = 2, Error = 3 } public static void ShowMessage( this Controller controller, MessageType messageType, string message, bool showAfterRedirect = false ) { string messageTypeKey = messageType.ToString(); if (showAfterRedirect) { controller.TempData[messageTypeKey] = message; } else { controller.ViewData[messageTypeKey] = message; } } public static HtmlString RenderMessages( this HtmlHelper htmlHelper, bool clearMessage = true ) { string messages = String .Empty; foreach ( string messageType in Enum .GetNames( t

Pass Values Between ASP.NET Web Pages without Session or any State Management

If the source page and target page are both ASP.NET Web pages in the same Web application, and if you transfer execution from the source page to the target page on the server by using the transfer method, the target page can access public properties in the source page. Page One <asp:TextBox ID="textCity" runat="server" Text="Brjesh"></asp:TextBox> <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />  public String CurrentCity         {             get             {                 return textCity.Text;             }         }         public List<String> Current         {             get             {                 return _Current;             }         }         private List<String> _Current = null;         protected void Page_Load(object sender, EventArgs e)         {             _Current = new List<String>();             _Current.Add("1

CRUD in EF

 public int Create(Users entity)         {             using (var context = new K305DataEntities())             {                 var newEntity = Mapper.Map<User>(entity);                             context.Users.Add(newEntity);                 context.SaveChanges();                 return newEntity.Id;             }         }         public int Update(Users entity)         {             using (var context = new K305DataEntities())             {                 var dbEntity = context.Users.FirstOrDefault(dc => dc.Id == entity.Id);                 if (dbEntity == null) throw new ApplicationException("Entity not found.");                 dbEntity.Name = entity.Name;                 context.SaveChanges();                 return dbEntity.Id;             }                 }         public List<Users> GetAll()         {             using (var context = new K305DataEntities())             {                 return context.Users.Select(