Solved

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

Posted on 2011-09-15
3
232 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

734 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