ASP日历的数据源

时间:2018-08-10 08:54:03

标签: c# mysql asp.net

我正在尝试将mysql数据绑定到asp:calendar,但是它不起作用。我需要根据日期列在mysql表slotavailable列中显示数据。如何将其放入日历单元格?

<asp:Calendar ID="cal2" runat="server" Width="50%" DayField="Date" OnDayRender="Calendar1_DayRender"
            BackColor="Orange" NextMonthText="Next" PrevMonthText="Prev" >
            <DayStyle CssClass="days" VerticalAlign="Top" Font-Name="Arial" Height="80px" BackColor="lightYellow"  />
            <TodayDayStyle BackColor="Orange"  />
            <OtherMonthDayStyle BackColor="LightGray" ForeColor="DarkGray"/>
</asp:Calendar>

下面是获取数据的CS代码

 protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            cal2.VisibleDate = DateTime.Today;
            FillLeaveplannerDataset();
        }
    }
    protected void FillLeaveplannerDataset()
    {
        DateTime firstDate = new DateTime(cal2.VisibleDate.Year, cal2.VisibleDate.Month, 1);
        DateTime lastDate = GetFirstDayOfNextMonth();
        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;");
        MySqlCommand cmd = new MySqlCommand("SELECT * FROM dummy WHERE date >= @firstDate AND date < @lastDate", con);
        cmd.Parameters.Add(new MySqlParameter("@firstDate", firstDate));
        cmd.Parameters.Add(new MySqlParameter("@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"];
            var slot = dr["slotavailable"];
            if (nextDate == e.Day.Date)
            {
                e.Cell.BackColor = System.Drawing.Color.Pink;
            }
        }
    }
}
protected void Calendar1_VisibleMonthChanged(object sender,
MonthChangedEventArgs e)
{
    FillLeaveplannerDataset();
}

如何将广告位列数据获取到日历单元格中?

1 个答案:

答案 0 :(得分:2)

请确保您的nextDatee.Day.Date匹配,然后像

那样更改代码

e.Cell.Controls可以将任何文本添加到您的单元格中

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"];
            var slot = dr["slotavailable"];
            if (nextDate == e.Day.Date)
            {
                //This is the line where we add slotavailable column data
                e.Cell.Controls.Add(new LiteralControl($"<p>{slot}</p>"));
                e.Cell.BackColor = System.Drawing.Color.Pink;
            }
        }
    }
}

输出将为

enter image description here

e.Cell.Controls的优势是您可以根据需要添加html buttonspanimage或其他任何内容

e.Cell.Controls.Clear();可能会帮助您清除与特定单元格相关的所有控件

尝试一次可能对您有帮助