Solved

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

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
This video discusses moving either the default database or any database to a new volume.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

758 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

16 Experts available now in Live!

Get 1:1 Help Now