Click here to Skip to main content
15,891,905 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
C#
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
Updated 14-Nov-12 1:43am
v3
Comments
[no name] 14-Nov-12 9:15am    
You need to initialize your date to now, then add and subtract from your calendars data not now

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:

C#
private void UpdateMyLabel(DateTime dt)
   {
   Session["CurrentDate"] = dt;
   Label1.Text = dt.ToLongDateString();
   }

C#
private DateTime GetCurrentDate()
   {
   return Session["CurrentDate"] == null ? DateTime.Now : (DateTime) Session["CurrentDate"];
   }
 
Share this answer
 
Comments
Richard MacCutchan 14-Nov-12 9:02am    
teh, teh, teh ... ?
Keith Barrow 14-Nov-12 9:12am    
That's quite a cough you've developed there Richard.
Richard MacCutchan 14-Nov-12 9:30am    
My throat needs lubricating.
OriginalGriff 14-Nov-12 9:32am    
See my profile - I can't spell teh word!
Richard MacCutchan 14-Nov-12 9:42am    
:))
Hi

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



Here you are adding the years to 'now'. But 'now' is
C#
DateTime now = DateTime.Now

So it will always display current date + 1. I have a workaround. You can use session , caching etc.

C#
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]
 
Share this answer
 
v2
Comments
NayakMamuni 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 ?
Hi
Hope it will work.

C#
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
 
Share this answer
 
v2
Comments
NayakMamuni 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900