asp日历重复标签

yfwxisqw  于 2021-06-23  发布在  Mysql
关注(0)|答案(1)|浏览(354)

我有一个asp日历链接到datasource,它获取一个日期在数据表中重复的次数,并显示在日历上。但是如果mysql表上有两个同一个月的日期,那么标签会显示两次,如果有3个日期,则会显示三次,以此类推,如下图所示。

代码如下所示

protected void Page_Load(Object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            cal2.VisibleDate = DateTime.Today;
            FillLeaveplannerDataset();
        }
    }
    protected void FillLeaveplannerDataset()
    {
        cal2.VisibleDate = cal2.TodaysDate;
        DateTime firstDate = new DateTime(cal2.VisibleDate.Year, cal2.VisibleDate.Month, 1).AddDays(-6);
        DateTime lastDate = new DateTime(cal2.VisibleDate.Date.AddMonths(1).Year, cal2.VisibleDate.Date.AddMonths(1).Month, 1).AddDays(7);
        dsleaveplanner = GetCurrentMonthData(firstDate, lastDate);
    }
    protected DateTime GetFirstDayOfNextMonth()
    {
        int monthNumber, yearNumber;
        if (cal2.VisibleDate.Month == 12)
        {
            monthNumber = 1;
            yearNumber = cal2.VisibleDate.Year + 1;
        }
        else
        {
            monthNumber = cal2.VisibleDate.Month + 1;
            yearNumber = cal2.VisibleDate.Year;
        }
        DateTime lastDate = new DateTime(yearNumber, monthNumber, 1);
        return lastDate;
    }
    protected DataSet GetCurrentMonthData(DateTime firstDate, DateTime lastDate)
    {
        DataSet dsMonth = new DataSet();
        MySqlConnection con = new MySqlConnection("Server=localhost;Database=mydb;Uid=myid;Pwd=abc123;");
        string caldate = "Select date From approved Where date >= @firstDate And date <= @lastDate Group By date";
        MySqlCommand cmd = new MySqlCommand(caldate, con);
        cmd.Parameters.AddWithValue("@firstDate", firstDate);
        cmd.Parameters.AddWithValue("@lastDate", lastDate);
        MySqlDataAdapter mysqlDataAdapter = new MySqlDataAdapter(cmd);
        try
        {
            mysqlDataAdapter.Fill(dsMonth);
        }
        catch { }
        return dsMonth;
    }

    protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
    {
        DateTime nextDate;
        if (dsleaveplanner != null)
        {
            foreach (DataRow dr in dsleaveplanner.Tables[0].Rows)
            {
                nextDate = (DateTime)dr["date"];
                MySqlConnection conn = new MySqlConnection("Server=localhost;Database=mydb;Uid=myid;Pwd=abc123;");
                string cntdate = "SELECT COUNT(date) FROM approved WHERE date = @date";
                string cntdate2 = "SELECT COUNT(date) FROM pending WHERE date = @date";
                MySqlCommand cmd2 = new MySqlCommand(cntdate, conn);
                MySqlCommand cmd3 = new MySqlCommand(cntdate2, conn);
                cmd2.Parameters.AddWithValue("@date", nextDate);
                cmd3.Parameters.AddWithValue("@date", nextDate);
                conn.Open();
                string count = cmd2.ExecuteScalar().ToString();
                string count2 = cmd3.ExecuteScalar().ToString();
                var slot2 = Convert.ToInt32(count);
                Int32 slot3 = 10 - slot2;
                string slot4 = slot3.ToString();
                conn.Close();
                 if (nextDate == e.Day.Date)
                {
                    e.Cell.BackColor = System.Drawing.Color.Orange;
                    Environment.NewLine.ToString();
                    e.Cell.ForeColor = System.Drawing.Color.Red;
                    e.Cell.Font.Size = 9;
                    e.Cell.Controls.Add(new LiteralControl("<p></p>Slot available:"));
                    e.Cell.Controls.Add(new LiteralControl(slot4));
                    e.Cell.Controls.Add(new LiteralControl("<p></p>Pending:"));
                    e.Cell.Controls.Add(new LiteralControl(count2));
                }
                else
                {
                    e.Cell.Font.Size = 9;
                    e.Cell.Controls.Add(new LiteralControl("<p>Slot available: 10</p>"));
                    e.Cell.Controls.Add(new LiteralControl("<p></p>Pending: 0"));
                }
            }
        }

    }
    protected void Calendar1_VisibleMonthChanged(object sender,
    MonthChangedEventArgs e)
    {
        DateTime firstDate = e.NewDate.AddDays(-7);
        DateTime lastDate = e.NewDate.AddMonths(1).AddDays(7);
        dsleaveplanner = GetCurrentMonthData(firstDate, lastDate);

    }

我想要的是,如果没有日期的数据,我希望显示默认值slot avalable:10和pending:0。我希望每个日期只显示一次可用和挂起的插槽。我错过了什么?提前谢谢

iq3niunx

iq3niunx1#

首先用你的名字创建一个示例类

class Sample
{
    public DateTime Date { get; set; }
    public int SlotAvailable { get; set; }
    public int Pending { get; set; }
}

创建一个 List<Sample> ```
List samples = new List();

那就换衣服吧 `foreach` 就像

foreach (DataRow dr in dsleaveplanner.Tables[0].Rows)
{
nextDate = (DateTime)dr["date"];
MySqlConnection conn = new MySqlConnection("Server=localhost;Database=mydb;Uid=myid;Pwd=abc123;");
string cntdate = "SELECT COUNT(date) FROM approved WHERE date = @date";
string cntdate2 = "SELECT COUNT(date) FROM pending WHERE date = @date";
MySqlCommand cmd2 = new MySqlCommand(cntdate, conn);
MySqlCommand cmd3 = new MySqlCommand(cntdate2, conn);
cmd2.Parameters.AddWithValue("@date", nextDate);
cmd3.Parameters.AddWithValue("@date", nextDate);
conn.Open();
string count = cmd2.ExecuteScalar().ToString();
string count2 = cmd3.ExecuteScalar().ToString();
var slot2 = Convert.ToInt32(count);
Int32 slot3 = 10 - slot2;
string slot4 = slot3.ToString();
conn.Close();

samples.Add(new Sample { Date = nextDate, SlotAvailable = slot4, Pending = count2 });

}

然后简单地检查 `e.Day.Date` 出现在我们的约会中 `List<Sample>` 她约会的样子

if (samples.Any(x => x.Date == e.Day.Date))
{
e.Cell.BackColor = System.Drawing.Color.Orange;
Environment.NewLine.ToString();
e.Cell.ForeColor = System.Drawing.Color.Red;
e.Cell.Font.Size = 9;
e.Cell.Controls.Add(new LiteralControl("Slot available:"));
e.Cell.Controls.Add(new LiteralControl(samples.Where(x => x.Date == e.Day.Date).FirstOrDefault().SlotAvailable.ToString()));
e.Cell.Controls.Add(new LiteralControl("Pending:"));
e.Cell.Controls.Add(new LiteralControl(samples.Where(x => x.Date == e.Day.Date).FirstOrDefault().Pending.ToString()));
}
else
{
e.Cell.Font.Size = 9;
e.Cell.Controls.Add(new LiteralControl("Slot available: 10"));
e.Cell.Controls.Add(new LiteralControl("Pending: 0"));
}

输出:
![](https://i.stack.imgur.com/WfvYT.png)

相关问题