gpt4 book ai didi

c# - 数据源到 asp 日历

转载 作者:行者123 更新时间:2023-11-29 05:53:40 27 4
gpt4 key购买 nike

我正在尝试将 mysql 数据绑定(bind)到 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();
}

如何将插槽列数据放入日历单元格?

最佳答案

请确保您的 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(); 可以帮助您清除与特定单元格相关的所有控件

尝试一次可能对你有帮助

关于c# - 数据源到 asp 日历,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51782638/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com