Click here to Skip to main content
15,886,110 members
Articles / Web Development / ASP.NET

Date Picker User Control

Rate me:
Please Sign up or sign in to vote.
4.66/5 (43 votes)
2 Dec 2011CPOL6 min read 372.5K   18.2K   99  
A date picker user control in ASP.NET (C#, VS2010)
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// This class simulates a database.
/// </summary>
[System.ComponentModel.DataObject]
public class DataLayer
{
	public DataLayer()
	{
	}

    /// <summary>
    /// Access to DataTable which is stored in current session.
    /// </summary>
    private DataTable Persons
    {
        get
        {
            if (HttpContext.Current.Session["table"] == null)
            {
                HttpContext.Current.Session["table"] = CreateTable();
            }
            return (DataTable)HttpContext.Current.Session["table"];
        }
    }
    /// <summary>
    /// Returns a list of persons and their birthdates.
    /// </summary>
    /// <returns>DataTable</returns>
    [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, true)]
    public DataTable GetPersons()
    {
        return Persons;
    }

    /// <summary>
    /// Updates a person.
    /// </summary>
    /// <param name="id">person id</param>
    /// <param name="firstName">first name</param>
    /// <param name="lastName">last name</param>
    /// <param name="birthdate">birthdate</param>
    [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Update, true)]
    public void UpdatePerson(int id, string firstName, string lastName, DateTime birthdate)
    {
        foreach (DataRow row in Persons.Rows)
        {
            if ((int)row["Id"] == id)
            {
                row["FirstName"] = firstName;
                row["LastName"] = lastName;
                row["Birthdate"] = birthdate;
                row.AcceptChanges();
                break;
            }
        }
    }

    /// <summary>
    /// Create a DataTable with some persons in it.
    /// </summary>
    /// <returns>DataTable</returns>
    private DataTable CreateTable()
    {
        DataTable dt = new DataTable("Persons");
        dt.Columns.Add("Id", typeof(int));
        dt.Columns.Add("FirstName", typeof(string));
        dt.Columns.Add("LastName", typeof(string));
        dt.Columns.Add("Birthdate", typeof(DateTime));

        DataRow dr = dt.NewRow();
        dr[0] = 1;
        dr[1] = "Albert";
        dr[2] = "Albright";
        dr[3] = new DateTime(1960, 5, 12);
        dt.Rows.Add(dr);

        dr = dt.NewRow();
        dr[0] = 2;
        dr[1] = "Berta";
        dr[2] = "Bones";
        dr[3] = new DateTime(1955, 2, 20);
        dt.Rows.Add(dr);

        dr = dt.NewRow();
        dr[0] = 3;
        dr[1] = "Charles";
        dr[2] = "Chambers";
        dr[3] = new DateTime(1975, 10, 8);
        dt.Rows.Add(dr);

        dr = dt.NewRow();
        dr[0] = 4;
        dr[1] = "Diana";
        dr[2] = "Denver";
        dr[3] = new DateTime(1968, 1, 11);
        dt.Rows.Add(dr);

        return dt;
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer Sevitec Informatik AG
Switzerland Switzerland
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions