在Asp.net中创建日历 [英] Creating a calender in Asp.net

查看:113
本文介绍了在Asp.net中创建日历的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好专家,
我正在尝试在asp.net中创建一个日历.首先,我有一个标签和4个按钮.我试图在标签中显示当前日期,并使用按钮尝试加减月份和年份.
像这样:<< <标签> >>
但是当我单击按钮时,它只会增加或减少一次,而不是连续地.

这是我的代码:

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 ?

推荐答案

问题很简单,但是如果您是初学者,则很难理解.

基本上,当用户连接到您的页面时,将调用Page Load事件,并将标签设置为当前时间.当页面完全加载给用户时,就服务器而言,页面将死掉.当用户按下按钮时,信号将发送到服务器,服务器从头开始重新加载页面,然后调用按钮事件处理程序.这意味着将再次调用Page Load事件,因此您将标签内容重置为当前时间.然后会调用Click事件ahndler,然后您再次获取现在的时间并使用它.

您要么需要将当前显示日期"存储在会话变量 [ ^ ]或从标签中读取值,然后使用该值(还有其他几种方法,但是对于初学者来说,这是最容易的.)我会参加Session-它的操作非常非常简单:

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"];
   }




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


因此它将始终显示当前日期+1.我有一个解决方法.您可以使用会话,缓存等.


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;
      }



这只是一个例子.有比这更好的解决方案.

问候
多米尼克

[edit]添加了代码块[/edit]



This only an example. There are better solutions than this.

Regards
Dominic

[edit]code block added[/edit]



希望它能工作.

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


这篇关于在Asp.net中创建日历的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆