简体   繁体   English

数据源到ASP日历

[英]Datasource to asp calendar

I'm trying to bind mysql data to asp:calendar but its not working. 我正在尝试将mysql数据绑定到asp:calendar,但是它不起作用。 I need to display the data in mysql table slotavailable column according to the date column. 我需要根据日期列在mysql表slotavailable列中显示数据。 How can I get it into the calendar cells? 如何将其放入日历单元格?

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

Below would be the cs code to fetch data 下面是获取数据的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();
}

How can get the slot column data into the calendar cell? 如何将广告位列数据放入日历单元格?

You please make sure that your nextDate and e.Day.Date are matches and then change in your code like 您请确保您的nextDatee.Day.Date匹配,然后在代码中进行更改,例如

e.Cell.Controls can add any text to your cell 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;
            }
        }
    }
}

The output will be 输出将是

在此处输入图片说明

The advantage of e.Cell.Controls you may add html button or span or image or any else as your need e.Cell.Controls的优点是您可以根据需要添加html buttonspanimage或其他任何内容

And e.Cell.Controls.Clear(); 还有e.Cell.Controls.Clear(); may help you to clear all controls related to particular cell 可以帮助您清除与特定单元格相关的所有控件

Try once may it help you 尝试一次可能对您有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM