Solved

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

Posted on 2011-09-15
3
226 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
A short film showing how OnPage and Connectwise integration works.
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…

919 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now