Solved

asp.net c#   Prevent users from selecting a previous date

Posted on 2011-09-15
3
227 Views
Last Modified: 2012-06-27
Hello,

I have a an application where I'm attempting to have users select a date from a datepicker control, however, I would like to prevent users from selecting a previous date.

My *. aspx file code follows
-----------------------
   <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
         
          <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
     
     
 &nbsp;
          <asp:Calendar ID="Calendar1" runat="server" BackColor="#FFFFCC"
              BorderColor="#FFCC66" BorderWidth="1px" DayNameFormat="Shortest"
              Font-Names="Verdana" Font-Size="8pt" ForeColor="#663399" Height="200px"
              onselectionchanged="Calendar1_SelectionChanged" ShowGridLines="True"
              Width="220px" Visible="false">
              <SelectedDayStyle BackColor="#CCCCFF" Font-Bold="True" />
              <SelectorStyle BackColor="#FFCC66" />
              <TodayDayStyle BackColor="#FFCC66" ForeColor="White" />
              <OtherMonthDayStyle ForeColor="#CC9966" />
              <NextPrevStyle Font-Size="9pt" ForeColor="#FFFFCC" />
              <DayHeaderStyle BackColor="#FFCC66" Font-Bold="True" Height="1px" />
              <TitleStyle BackColor="#990000" Font-Bold="True" Font-Size="9pt"
                  ForeColor="#FFFFCC" />
          </asp:Calendar>
     
          <br />
     
      <asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton1_Click">PickDate...</asp:LinkButton>
------------

My codebehind follows:

------------
protected void Page_Load(object sender, EventArgs e)
        {
            TextBox1.Text = DateTime.Today.ToString("MM/dd/yyyy");


        }




        protected void Calendar1_SelectionChanged(object sender, EventArgs e)
        {



            DateTime dt = System.DateTime.Now;
            DateTime dt2 = System.DateTime.Now;
            String dt2 = TextBox1.Text;

            if ((System.DateTime)dt2 <= dt)

                 //Convert.ToDateTime(myDateTimeString);

            {
                Label1.Text = "You can not pick a date earlier than today's date";

            }

            else
                TextBox1.Text = Calendar1.SelectedDate.ToShortDateString();


            Calendar1.Visible = false;
         

        }

       


        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            Calendar1.Visible = true;
 
        }
    -------------

ANY help would be greatly appreciated.
0
Comment
Question by:Codeaddict7423
  • 2
3 Comments
 
LVL 9

Expert Comment

by:dexterrajesh
ID: 36544296
hi,

try this jquery

$("#datepicker").datepicker({ minDate: +0 });

or you can disable the previous dates

protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)

    {

        if (e.Day.Date < DateTime.Now.Date)

        {

            e.Day.IsSelectable = false;

            e.Cell.ForeColor = System.Drawing.Color.Gray;

        }

    }

Open in new window

0
 

Author Comment

by:Codeaddict7423
ID: 36548969
dexterrajesh,

Thank you for your reply.

However, I don't understand your suggestion for jquery.

Can you be more specific or point me to where examples of this coding exists?

Thank you in advance.
0
 
LVL 9

Accepted Solution

by:
dexterrajesh earned 500 total points
ID: 36558595
minDate tells jQuery to set the minimum date for selection. all dates less than min date will be disabled
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Install Problem 13 30
Question! 4 36
Ideas for Displaying a hierarchy of data in ASP.NET (Org chart in table or tree format) 1 17
Expression Evaluater 3 24
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question