Click here to Skip to main content
Sign Up to vote bad
good
See more: ASP.NET
Hello Experts,
I am trying to create a calendar in asp.net. Firstly I have one label and 4 buttons.I am trying to show the current date in label and using the buttons trying to add and subtract month and year.
Like this: << < Label > >>
But when i click the buttons it adds or subtract only once, not continuously.
 
Here is my code:
public partial class Admin_stafftimesheet : System.Web.UI.Page
{
    DateTime now = DateTime.Now;
    protected void Page_Load(object sender, EventArgs e)
    {
        Label1.Text = DateTime.Now.ToLongDateString();        
    }
    protected void linknextmonth_Click(object sender, EventArgs e)
    {
           
        DateTime nextmonth = now.AddMonths(1);
        Label1.Text = nextmonth.ToLongDateString();
    }
    protected void linkpremonth_Click(object sender, EventArgs e)
    {
    
        DateTime premonth = now.AddMonths(-1);
        Label1.Text = premonth.ToLongDateString(); 
    }
    protected void linknextyr_Click(object sender, EventArgs e)
    {
 
        DateTime nxtyr = now.AddYears(1);
        Label1.Text = nxtyr.ToLongDateString();
        
    }
    protected void linkpreyr_Click(object sender, EventArgs e)
    {
        DateTime preyr = now.AddYears(-1);
        Label1.Text = preyr.ToLongDateString();
        
    }
}
 
What to do ?
Posted 14 Nov '12 - 1:32
Edited 14 Nov '12 - 1:43
__TR__28.5K

Comments
Mathlab - 14 Nov '12 - 9:15
You need to initialize your date to now, then add and subtract from your calendars data not now

3 solutions

The problem is quite simple, but it's a bit harder to understand if you are a beginner.
 
Basically, when teh user connects to your page, the Page Load event is called, and you set the label to teh current time. When teh page is loaded completely to the user, the page dies as far as the server is concerned. When the user presses a button, a signal is sent to the server, which reloads the page from scratch, and then calls the button event handler. This means that the Page Load event is called again, so you reset the label content to the current time. The Click event ahndler is then called, and you pick up the now time again and use that.
 
You either need to store your "current display date" in the Session variable[^] or read the value from the label, and use that (There are a few other ways, but they are the easiest for beginners.) I would go with the Session - it's very, very simple to do:
 
private void UpdateMyLabel(DateTime dt)
   {
   Session["CurrentDate"] = dt;
   Label1.Text = dt.ToLongDateString();
   }
private DateTime GetCurrentDate()
   {
   return Session["CurrentDate"] == null ? DateTime.Now : (DateTime) Session["CurrentDate"];
   }
  Permalink  
Comments
Richard MacCutchan - 14 Nov '12 - 9:02
teh, teh, teh ... ?
Keith Barrow - 14 Nov '12 - 9:12
That's quite a cough you've developed there Richard.
Richard MacCutchan - 14 Nov '12 - 9:30
My throat needs lubricating.
OriginalGriff - 14 Nov '12 - 9:32
See my profile - I can't spell teh word!
Richard MacCutchan - 14 Nov '12 - 9:42
:))
Hi
 

      DateTime nxtyr = now.AddYears(1);
      Label1.Text = nxtyr.ToLongDateString();
 

Here you are adding the years to 'now'. But 'now' is
 DateTime now = DateTime.Now
So it will always display current date + 1. I have a workaround. You can use session , caching etc.
 
  DateTime now = DateTime.Now;
        DateTime nxtyr = now.AddYears(1);
        if (Session["year"] == null)
        {
            Session["year"] = nxtyr;
            Label1.Text = nxtyr.AddYears(1).ToString();
        }
 
        if (Session["year"] != null)
        {
            DateTime dt = (DateTime)Session["year"];
            nxtyr = dt.AddYears(1);
            Label1.Text = nxtyr.ToString();
            Session["year"] = nxtyr;
        }
 
This only an example. There are better solutions than this.
 
Regards
Dominic
 
[edit]code block added[/edit]
  Permalink  
Comments
NayakMamuni - 15 Nov '12 - 1:56
Addition of year is performing well but subtraction of year is not performing. I am using -1 for subtraction. It subtract only once. How to do ?
Hi
 
Hope it will work.
 
  DateTime now = DateTime.Now;
        DateTime nxtyr = now.AddYears(-1);
        if (Session["year"] == null)
        {
            Session["year"] = nxtyr;
            Label1.Text = nxtyr.AddYears(-1).ToString();
        }
 
        if (Session["year"] != null)
        {
            DateTime dt = (DateTime)Session["year"];
            nxtyr = dt.AddYears(-1);
            Label1.Text = nxtyr.ToString();
            Session["year"] = nxtyr;
        }
 
Regards
Dominic
  Permalink  
Comments
NayakMamuni - 16 Nov '12 - 2:16
Hi, Its working properly. I have more requirements in this calender. Will you please help me ? My requirement is that i have a table with 7 column(sun,mon,...,sat) and 5 rows. When i click on the nextyear/preyear/nextmonth/premonth buttons which i have mentioned above, the table should be update automatic as per the month and year. How can i do it ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 OriginalGriff 223
1 Ron Beyer 220
2 Aarti Meswania 200
3 Mahesh Bailwal 175
4 Rohan Leuva 170
0 Sergey Alexandrovich Kryukov 8,553
1 OriginalGriff 6,899
2 CPallini 3,648
3 Rohan Leuva 2,963
4 Maciej Los 2,308


Advertise | Privacy | Mobile
Web01 | 2.6.130516.1 | Last Updated 15 Nov 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid