0

我一直在尝试实现这个链接

到我的基本 ASP.NET 日历,但我真的很担心我使用哪种方法?我用这种方式,没有任何效果..

public partial class WebForm2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            calendar1.VisibleDate = DateTime.Today;
            FillTrainingDataset();
        }

        protected DataSet dsTraining;


        protected void FillTrainingDataset()
        {
            DateTime firstDate = new DateTime(calendar1.VisibleDate.Year,
                calendar1.VisibleDate.Month, 1);
            DateTime lastDate = GetFirstDayOfNextMonth();
            dsTraining = GetCurrentMonthData(firstDate, lastDate);
        }

        protected DateTime GetFirstDayOfNextMonth()
        {
            int monthNumber, yearNumber;
            if (calendar1.VisibleDate.Month == 12)
            {
                monthNumber = 1;
                yearNumber = calendar1.VisibleDate.Year + 1;
            }
            else
            {
                monthNumber = calendar1.VisibleDate.Month + 1;
                yearNumber = calendar1.VisibleDate.Year;
            }
            DateTime lastDate = new DateTime(yearNumber, monthNumber, 1);
            return lastDate;
        } 

  protected DataSet GetCurrentMonthData(DateTime firstDate,
             DateTime lastDate)
{
    DataSet dsMonth = new DataSet();
    ConnectionStringSettings cs;
    cs = ConfigurationManager.ConnectionStrings["KKSTechConnectionString"];
    String connString = cs.ConnectionString;
    SqlConnection dbConnection = new SqlConnection(connString);
    String query;
    query = "SELECT StartDate, EndDate FROM Tentative_Orders";
    SqlCommand dbCommand = new SqlCommand(query, dbConnection);
    dbCommand.Parameters.Add(new SqlParameter("@StartDate", 
        firstDate));
    dbCommand.Parameters.Add(new SqlParameter("@EndDate", lastDate));

    SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(dbCommand);
    try
    {
        sqlDataAdapter.Fill(dsMonth);
    }
    catch {}
    return dsMonth;
}

        protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
        {
            DateTime nextDate;
            if (dsTraining != null)
            {
                foreach (DataRow dr in dsTraining.Tables[0].Rows)
                {
                    nextDate = (DateTime)dr["StartDate"];
                    if (nextDate == e.Day.Date)
                    {
                        e.Cell.BackColor = System.Drawing.Color.Pink;
                    }
                }
            }
        }
        protected void Calendar1_VisibleMonthChanged(object sender,
            MonthChangedEventArgs e)
        {
            FillTrainingDataset();
        }

    }

有什么简单的方法可以创建方法并渲染到我的日历上吗?这对我来说似乎很长。我希望我的数据库表中的日期(开始和结束)出现在日历上。

4

0 回答 0