Thursday 22 November 2012

User control for Textbox Date entry

.ascx code-

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="DateTextBox.ascx.cs" Inherits="DateTextBox" %>

<script language="javascript" type="text/javascript">
    function isDate(txtDate, separator) {
        var aoDate,           // needed for creating array and object
        ms,               // date in milliseconds
        month, day, year; // (integer) month, day and year
        // if separator is not defined then set '/'
        if (separator === undefined) {
            separator = '/';
        }
        // split input date to month, day and year
        aoDate = txtDate.split(separator);
        // array length should be exactly 3 (no more no less)
        if (aoDate.length !== 3) {
            return false;
        }
        // define month, day and year from array (expected format is m/d/yyyy)
        // subtraction will cast variables to integer implicitly
        month = aoDate[1] - 1; // because months in JS start from 0
        day = aoDate[0] - 0;
        year = aoDate[2] - 0;
        // test year range
        if (year < 1000 || year > 3000) {
            return false;
        }
        // convert input date to milliseconds
        ms = (new Date(year, month, day)).getTime();
        // initialize Date() object from milliseconds (reuse aoDate variable)
        aoDate = new Date();
        aoDate.setTime(ms);
        // compare input date and parts from Date() object
        // if difference exists then input date is not valid
        if (aoDate.getFullYear() !== year ||
        aoDate.getMonth() !== month ||
        aoDate.getDate() !== day) {
            return false;
        }
        // date is OK, return true
        return true;
    }
    function checkDate() {
        // define date string to test
        var dt = document.getElementById('<%=TxtDate.ClientID %>');
        var txtDate = document.getElementById('<%=TxtDate.ClientID %>').value;
        // check date and print message
        if (isDate(txtDate)) {
            return true;
        }
        else {
            alert('Invalid date format!');
            dt.value = "";
            dt.focus();
            return false;
        }
    }

  

</script>

<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<asp:TextBox ID="TxtDate" runat="server" OnTextChanged="TxtDate_TextChanged" OnChange="checkDate();"></asp:TextBox>
<cc1:MaskedEditExtender ID="med" runat="server" TargetControlID="TxtDate" ClearTextOnInvalid="true"
    AutoComplete="false" CultureName="en-US" InputDirection="LeftToRight" Mask="99/99/9999"
    MaskType="Date" PromptCharacter="_">
</cc1:MaskedEditExtender>
<cc1:CalendarExtender ID="ceTxtdate" runat="server" TargetControlID="TxtDate" Format="dd/MM/yyyy">
</cc1:CalendarExtender>
<asp:RequiredFieldValidator ID="Rfv_Date" runat="server" ControlToValidate="TxtDate"
    Display="None" SetFocusOnError="true"></asp:RequiredFieldValidator>

c# code-

using System;

public partial class DateTextBox : System.Web.UI.UserControl
{
    public bool boolDefault = false;
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    public event System.EventHandler TxtTextChanged;
    protected virtual void OnTextBoxTextChanged(object sender)
    {
        if (this.TxtTextChanged != null)
        {
            this.TxtTextChanged(sender, new EventArgs());
        }
    }
    protected void TxtDate_TextChanged(object sender, EventArgs e)
    {
        OnTextBoxTextChanged(sender);
    }
    public string TextBoxValue
    {
        get { return TxtDate.Text; }
        set { TxtDate.Text = value; }
    }
    public string Validate
    {
        set { this.Rfv_Date.ValidationGroup = value; }
    }
    public bool TxtEnabled
    {
        set { this.TxtDate.Enabled = value; }
    }
    public bool TxtReadOnly
    {
        set { this.TxtDate.ReadOnly = value; }
    }
    public bool TxtAutoPostback
    {
        set { this.TxtDate.AutoPostBack = value; }
    }
    public string TxtCss
    {
        set { this.TxtDate.CssClass = value; }
    }
    public string ErrorMessage
    {
        set { this.Rfv_Date.ErrorMessage = value; }
    }
    public bool DefaultValue
    {
        set
        {
            boolDefault = value;
            if (boolDefault)
            {
                this.TxtDate.Text = DateTime.Today.Date.ToString("dd/MM/yyyy");
            }
            else
            {
                this.TxtDate.Text = "";
            }
        }
    }
}

No comments:

Post a Comment