Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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:32am
Edited 14-Nov-12 1:43am
__TR__28.9K
v3
Comments
Mathlab at 14-Nov-12 9:15am
   
You need to initialize your date to now, then add and subtract from your calendars data not now
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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 at 14-Nov-12 9:02am
   
teh, teh, teh ... ?
Keith Barrow at 14-Nov-12 9:12am
   
That's quite a cough you've developed there Richard.
Richard MacCutchan at 14-Nov-12 9:30am
   
My throat needs lubricating.
OriginalGriff at 14-Nov-12 9:32am
   
See my profile - I can't spell teh word!
Richard MacCutchan at 14-Nov-12 9:42am
   
:))
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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  
v2
Comments
NayakMamuni at 15-Nov-12 1:56am
   
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 ?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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  
v2
Comments
NayakMamuni at 16-Nov-12 2:16am
   
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
0 OriginalGriff 540
1 Sergey Alexandrovich Kryukov 432
2 Yogesh Kumar Tyagi 384
3 Prakriti Goyal 280
4 Maciej Los 205
0 OriginalGriff 6,632
1 Sergey Alexandrovich Kryukov 5,404
2 Maciej Los 3,474
3 Peter Leow 3,299
4 DamithSL 2,495


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

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100