Hallo everyone..
can anyone assist me on how to disable previous date on the asp.net calender.

i create this application using microsoft visual web developer 2005 express edition..
the calender i expected is will automatically disabled the previous date.

eg. today is 23/3/2010. the calender will disable the all the date before 23/3/2010.that is from 22/3/2010 back

i am developing a ticket reservation system for my education project.
hope you all might assist me..

i appreciate a alot for your kindness..

here my coding is given below.

Imports System.Text
Imports System.Web.UI.LiteralControl

Namespace VbNetTest

    Partial Class DatePicker
        Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

        'This call is required by the Web Form Designer.
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

        End Sub

        Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
            'CODEGEN: This method call is required by the Web Form Designer
            'Do not modify it using the code editor.
        End Sub

#End Region

        Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load, Calendar1.SelectionChanged
            'Nothing to do, here
        End Sub

        Private Sub Calendar1_DayRender(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DayRenderEventArgs) Handles Calendar1.DayRender
            '// Clear the link from this day

            '// Add the custom link
            Dim Link As System.Web.UI.HtmlControls.HtmlGenericControl
            Link = New System.Web.UI.HtmlControls.HtmlGenericControl
            Link.TagName = "a"
            Link.InnerText = e.Day.DayNumberText
            Link.Attributes.Add("href", String.Format("JavaScript:window.opener.document.{0}.value = '{1:d}'; window.close();", Request.QueryString("field"), e.Day.Date))

            '// By default, this will highlight today's date.
            If e.Day.IsSelected Then
                Link.Attributes.Add("style", Me.Calendar1.SelectedDayStyle.ToString())
            End If

            '// Now add our custom link to the page


        End Sub
    End Class

End Namespace

please help me...........

7 Years
Discussion Span
Last Post by apegram

You can add an event handler to the calendar's SelectionChanged event to validate the date. Example:

<form id="form1" runat="server">
        <asp:Calendar ID="calDemo" runat="server" OnSelectionChanged="calDemo_SelectionChanged" />
        <asp:Label ID="lblDemoError" runat="server" ForeColor="Red" />
Protected Sub calDemo_SelectionChanged(ByVal sender As Object, ByVal e As EventArgs)

        Dim selectedDate As DateTime = calDemo.SelectedDate

        If (selectedDate < DateTime.Now.Date) Then
            lblDemoError.Text = "Please select a present or future date."
            lblDemoError.Text = String.Empty
        End If

    End Sub

It doesn't disable the old dates, per se, but it does provide immediate negative feedback to the user. You would want to test the date on final form submission, as well.

Edited by apegram: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.