Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2011-09-15
3
Medium Priority
?
238 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 2000 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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

824 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