ASP日历控件更改C#后面的背景颜色代码
本文关键字:背景 颜色 代码 日历 控件 ASP | 更新日期: 2023-09-27 17:58:06
如果日期在数据库表中可用,则应根据SQL表中可用的事件日期将日历背景颜色更改为红色。下面是我的设计代码。
<asp:Calendar ID="Calendar1" runat="server" OnSelectionChanged="Calendar1_SelectionChanged">
</asp:Calendar>
Selected Date: <asp:Label ID="Label1" runat="server" ></asp:Label>
<br />
<br />
<asp:GridView ID="gv_Regdetails" runat="server" AllowSorting="True"
AlternatingRowStyle-CssClass="alt" AutoGenerateColumns="False" CssClass="mGrid"
Width="900px" PageSize="50">
<RowStyle HorizontalAlign="Center" />
<AlternatingRowStyle CssClass="alt" />
<Columns>
<asp:TemplateField HeaderText="Sno">
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Center" Width="30px" />
<ItemStyle CssClass="th" HorizontalAlign="Center" Width="30px" />
</asp:TemplateField>
<asp:BoundField DataField="EventName" HeaderText="EventName">
<ItemStyle CssClass="th" HorizontalAlign="Left" Width="30px" />
<HeaderStyle HorizontalAlign="Left" Width="30px" Height="25px" />
</asp:BoundField>
<asp:BoundField DataField="Title" HeaderText="Title">
<ItemStyle CssClass="th" HorizontalAlign="Left" Width="30px" />
<HeaderStyle HorizontalAlign="Left" Width="30px" Height="25px" />
</asp:BoundField>
<asp:BoundField DataField="Description" HeaderText="Description">
<ItemStyle CssClass="th" HorizontalAlign="Left" Width="30px" />
<HeaderStyle HorizontalAlign="Left" Width="30px" Height="25px" />
</asp:BoundField>
</Columns>
<EmptyDataTemplate>
<center>
<asp:Label ID="Label12" runat="server" Text="No Data avaliable for selected dates"
ForeColor="Red"></asp:Label></center>
</EmptyDataTemplate>
</asp:GridView>
下面是我的c#代码,我正在Calendar1_DayRender方法中编写逻辑。
protected void Calendar1_SelectionChanged(object sender, EventArgs e)
{
Label1.Text = Calendar1.SelectedDate.ToShortDateString();
bo.Para1 = Label1.Text;
DataTable dt = new DataTable();
dt = bl.Get_detailsbydate(bo);
gv_Regdetails.DataSource = dt;
gv_Regdetails.DataBind();
}
protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
DataTable dt = new DataTable();
dt = bl.Get_detailsbydate(bo);
string dates = dt.Rows[0]["EventDate"].ToString();
// if (e.Day.Date == dates)
// {
// e.Day.IsSelectable = false;
// e.Cell.ForeColor = System.Drawing.Color.Red;
// }
}
protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
DataTable dt = new DataTable();
dt = bl.Get_detailsbydate(bo);
string dates = dt.Rows[0]["EventDate"].ToString();// Confirm if you EventDate is in a same format to calendar
if (e.Day.Date == dates)
{
e.Day.IsSelectable = false;
e.Cell.BackColor = System.Drawing.Color.Red;
}
}