ASP.NET日历控件

时间:2011-07-29 11:36:02

标签: asp.net visual-studio-2010 calendar

Heey Stackers

我的日历有一个小问题一切正常 到此为止

一旦我从月份更改并点击一天,它会更改回一个月,我该如何解决此问题。所有

这是我的代码

            using System;
            using System.Configuration;
            using System.Data;
            using System.Linq;
            using System.Web;
            using System.Web.Security;
            using System.Web.UI;
            using System.Web.UI.HtmlControls;
            using System.Web.UI.WebControls;
            using System.Web.UI.WebControls.WebParts;
            using System.Data.SqlClient;
            using System.Collections;
            using iTextSharp.text;
            using iTextSharp.text.pdf;
            using iTextSharp.text.html;
            using System.IO;
            using System.Net;
using System.Collections.Generic;

        public partial class _Default : BasePage
        {

            Hashtable _scheduleData;

            DataView todo = new DataView();

            protected void Page_Load(object sender, EventArgs e)
            {

                if (Calendar1.SelectedDate == DateTime.Today)
                {
                    Calendar1.SelectedDate.Date.ToShortDateString();
                }
                _scheduleData = GetSchedule();

                Calendar1.Caption = "<br/>Het is vandaag:<br />" + DateTimeOffset.Now.LocalDateTime.ToShortDateString() + "<h1>Plan School Activiteiten</h1>";

                Calendar1.FirstDayOfWeek = FirstDayOfWeek.Sunday;
                Calendar1.NextPrevFormat = NextPrevFormat.FullMonth;
                Calendar1.NextPrevStyle.CssClass = "NextPrevMonthText";
                Calendar1.TitleFormat = TitleFormat.MonthYear;
                Calendar1.TitleStyle.CssClass = "TitleStyle";
                //Calendar1.TitleStyle.BackColor = System.Drawing.Color.Transparent;
                Calendar1.ShowGridLines = true;
                Calendar1.DayStyle.HorizontalAlign = HorizontalAlign.Left;
                Calendar1.DayStyle.CssClass = "Daysoftheweek";
                Calendar1.DayStyle.VerticalAlign = VerticalAlign.Top;
                Calendar1.DayStyle.Height = new Unit(75);
                Calendar1.DayStyle.Width = new Unit(100);
                Calendar1.TodayDayStyle.CssClass = "Today";
                Calendar1.TodaysDate.ToShortDateString();
                Calendar1.VisibleDate = Calendar1.TodaysDate;
                Calendar1.SelectedDayStyle.CssClass = "SelectStyle";
                Calendar1.SelectedDayStyle.BackColor = System.Drawing.ColorTranslator.FromHtml("#ffea44");
                Calendar1.SelectedDayStyle.ForeColor = System.Drawing.ColorTranslator.FromHtml("#000");

            }

            private Hashtable GetSchedule()
            {
                Hashtable schedule = new Hashtable();

                string cnnString = ConfigurationManager.ConnectionStrings["Stefan"].ConnectionString;
                DataTable dt = new DataTable();

                using (SqlConnection con = new SqlConnection(cnnString))
                using (SqlCommand cmd = new SqlCommand("select * from [Calender]", con))
                {
                    using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                    {
                        da.Fill(dt);


                    }
                    con.Open();
                    cmd.ExecuteNonQuery();
                }

                    Dictionary<string, int> schedulesDateCount = new Dictionary<string, int>();

                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        string date = Convert.ToDateTime(dt.Rows[i]["date"]).ToShortDateString();

                        if (!schedulesDateCount.ContainsKey(date))
                            schedulesDateCount[date] = 0;

                        if (schedulesDateCount[date] < 3)
                        {
                            schedule[date] = (schedule[date] != null ? schedule[date].ToString(): "") + Server.HtmlEncode(dt.Rows[i]["todo"].ToString()) + "<br />" + dt.Rows[i]["time"].ToString() + "<br />";
                            schedulesDateCount[date] = schedulesDateCount[date] + 1;
                        }
                    }
                    return schedule;

                }


            void Page_PreRender()
            {

                todo = (DataView)calendarSrc.Select(DataSourceSelectArguments.Empty);
                todo.Sort = "date";

            }

            protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
            {

                string date = e.Day.Date.ToShortDateString();
                if (_scheduleData[date] != null)
                {

                    Literal lit = new Literal();
                    lit.Text = "<br />";
                    e.Cell.Controls.Add(lit);

                    Label lbl = new Label();
                    lbl.Text = (string)_scheduleData[e.Day.Date.ToShortDateString()];
                    lbl.Font.Size = new FontUnit(FontSize.Small);
                    e.Cell.Controls.Add(lbl);

                }

            }

            protected void Calendar1_SelectionChanged(object sender, EventArgs e)
            {
                FormView1.ChangeMode(FormViewMode.Edit);
            }

            protected void butAddNew_Click(object sender, EventArgs e)
            {
                FormView1.ChangeMode(FormViewMode.Insert);
            }
            protected void todoSrc_Inserted(object sender, SqlDataSourceStatusEventArgs e)
            {
                Refresh();
            }
            protected void todoSrc_Deleted(object sender, SqlDataSourceStatusEventArgs e)
            {
                Refresh();
            }
            protected void todoSrc_Updated(object sender, SqlDataSourceStatusEventArgs e)
            {
                Refresh();
            }

            private void Refresh()
            {
                Response.Redirect(Request.RawUrl);
            }

@Tim Schmelter

我不得不因为如果我在特殊的日子将数据插入我的日历中,如果我点击那天而不是它的节目,它将不会直接显示我的插入数据但是我不想这样我这样做

protected void todoSrc_Deleted(object sender, SqlDataSourceStatusEventArgs e)
{
    Refresh();
}
protected void todoSrc_Updated(object sender, SqlDataSourceStatusEventArgs e)
{
    Refresh();
}

private void Refresh()
{
    Response.Redirect(Request.RawUrl);
}

1 个答案:

答案 0 :(得分:2)

我不确定这是否是原因,但您应该仅在!Page.IsPostBack时执行日历的默认初始化。

if(!Page.IsPostBack){
        if (Calendar1.SelectedDate == DateTime.Today)
        {
            Calendar1.SelectedDate.Date.ToShortDateString();
        }
        _scheduleData = GetSchedule();

        Calendar1.Caption = "<br/>Het is vandaag:<br />" + DateTimeOffset.Now.LocalDateTime.ToShortDateString() + "<h1>Plan School Activiteiten</h1>";

        Calendar1.FirstDayOfWeek = FirstDayOfWeek.Sunday;
        Calendar1.NextPrevFormat = NextPrevFormat.FullMonth;
        Calendar1.NextPrevStyle.CssClass = "NextPrevMonthText";
        Calendar1.TitleFormat = TitleFormat.MonthYear;
        Calendar1.TitleStyle.CssClass = "TitleStyle";
        //Calendar1.TitleStyle.BackColor = System.Drawing.Color.Transparent;
        Calendar1.ShowGridLines = true;
        Calendar1.DayStyle.HorizontalAlign = HorizontalAlign.Left;
        Calendar1.DayStyle.CssClass = "Daysoftheweek";
        Calendar1.DayStyle.VerticalAlign = VerticalAlign.Top;
        Calendar1.DayStyle.Height = new Unit(75);
        Calendar1.DayStyle.Width = new Unit(100);
        Calendar1.TodayDayStyle.CssClass = "Today";
        Calendar1.TodaysDate.ToShortDateString();
        Calendar1.VisibleDate = Calendar1.TodaysDate;
        Calendar1.SelectedDayStyle.CssClass = "SelectStyle";
        Calendar1.SelectedDayStyle.BackColor = System.Drawing.ColorTranslator.FromHtml("#ffea44");
        Calendar1.SelectedDayStyle.ForeColor = System.Drawing.ColorTranslator.FromHtml("#000");
}