I have a calendar control that I was filling with different events. When one event only spans one day I wanted it to change the linkbutton color to red.
public partial class RequestCalander : System.Web.UI.Page
{
private DataSet GetData()
{
ConnectionStringSettingsCollection cssc = ConfigurationManager.ConnectionStrings;
var sql = "select (substring(status, 1,1)) AS stat1, lastname, firstname, lstdate, lenddate, requestid from [table] E inner join [tableEvent] T on E.EMPID = T.emppid where E.depdivid = '" + @UsrDepartment + "'";
using (iDB2Connection conn = new iDB2Connection(GetConnectionString()))
{
conn.Open();
using (iDB2Command cmd = new iDB2Command(sql, conn))
{
cmd.DeriveParameters();
using (iDB2DataAdapter da = new iDB2DataAdapter(cmd))
{
DataSet ds = new DataSet();
da.Fill(ds);
return ds;
}
}
}
}
private String GetConnectionString()
{
ConnectionStringSettingsCollection cssc = ConfigurationManager.ConnectionStrings;
return cssc["connStringEvent"].ToString();
}
protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
e.Cell.Width = 150;
e.Cell.Height = 100;
DataSet ds = GetData();
string link = "<a class='turnred' href='viewRequestForm.aspx?bwrequestid=";
string s = e.Day.Date.ToShortDateString();
e.Cell.Text = e.Day.Date.Day.ToString() + "<BR>";
LiteralControl l = new LiteralControl();
l.Text = e.Day.Date.Day.ToString() + "<BR>";
e.Cell.Controls.Add(l);
foreach (DataRow row in ds.Tables[0].Rows)
{
string scheduledDate = Convert.ToDateTime(row["lstdate"]).ToShortDateString();
string endDate = Convert.ToDateTime(row["lenddate"]).ToShortDateString();
e.Cell.Width = 120;
e.Cell.Height = 100;
Int32 start = 0;
Int32 end = 0;
start = string.CompareOrdinal(scheduledDate, s);
end = string.CompareOrdinal(endDate, s);
if ((start > 0) || (end < 0) || (e.Day.IsWeekend == true))
{
return;
}
HyperLink hl = new HyperLink();
hl.Text = "yes";
if (scheduledDate.CompareTo(endDate) == 0){
hl.ForeColor = System.Drawing.Color.Red;
e.Cell.Controls.Add(hl);
}
if ((start <= 0) & (end >= 0) & (!e.Day.IsWeekend))
{
HyperLink lb = new HyperLink();
lb.Text = link + (Int64)row["bwrequestid"] + "' >" + row["lastname"] + "</a>" as String + "(" + row["stat1"] + ")" as String + "<br />";
lb.ForeColor = System.Drawing.Color.Red;
e.Cell.Controls.Add(lb);
lb.NavigateUrl = "~/Form.aspx?requestid={0}";
}
}
}
aspx code
<%@ Page Title="Leave Request" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Calander.aspx.cs" Inherits="RequestCalander" %>
<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="asp" %>
<%@ Register TagPrefix="ec" Namespace="ControlSample" Assembly="EventCalendar" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
<link type="text/css" rel="Stylesheet" href="../../Styles/Calendar.css" />
<style type="text/css">
.style1{width: 782px;}
.style2{width: 883px;}
.changecolor { color: Red; }
</style>
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<table class="style1">
<tr>
<td class="style2">
<table class="style4">
<tr>
<td>
<asp:Button ID="backButton" runat="server" Text="Back" Width="100" />
<asp:Button ID="requestButton" runat="server" OnClick="goToPageRequest" Text="Make Request" Width="100" />
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td class="style2">
<asp:Calendar ID="Calendar1" runat="server" ondayrender="Calendar1_DayRender" Font-Size="Large" Width="901px" style="font-family: 'Times New Roman', Times, serif" >
<TitleStyle Font-Bold="true" Font-Size="X-large" BorderStyle="Solid" />
<DayHeaderStyle CssClass="caldays" Height="30px" BorderStyle="Solid" />
<DayStyle CssClass="calcurrentmonth" Font-Size="X-small" Height="100px" />
<TodayDayStyle CssClass="calcurrentday" Width="120px" Height="100px"/>
<WeekendDayStyle CssClass="calweekend" Width="120px" Height="100px"/>
<OtherMonthDayStyle CssClass="calothermonth" Width="120px" Height="100px"/>
</asp:Calendar>
</td>
</tr>
</table>
</asp:Content>
The Css
/************************************************************************
*
* Calendar specific formatting
*
************************************************************************/
/* Surrounds the calendar */
.eventmonth
{
padding-left: 1px;
padding-right: 1px;
padding-top: 1px;
text-align: center;
}
.changecolor
{
color:red;
}
/* used as the cssclass of the actual calendar */
.eventmonthtable
{
margin-right: 0px;
margin-left: 8px;
position: relative;
margin-bottom: 9px;
border: 1px solid #666666;
border-collapse:collapse;
top: 1px;
left: -228px;
height: 38px;
width: 722px;
}
.dayNumber
{
color :Background;
float: right;
border-bottom: 1px solid #666666;
border-left: 1px solid #666666;
clear: none;
padding: 2px;
}
.linkbutton
{
color:red;
}
.calcurrentmonth
{
}
.calothermonth
{
background-color: #6B9EB9;
}
.calcurrentday
{
background-color: #B0C9FF;
}
.calweekend
{
background-color: #6B9EB9;
}
.calcurrentmonth , .calcurrentmonth , .calothermonth , .calcurrentday , .calweekend
{
text-align: left;
border: 2px solid #000000;
vertical-align: top;
/* needed for positioning the dayNumber part */
position:relative;
border-collapse:separate;
border-spacing: 2px;
}
.nextlink
{
position:absolute;
right:0;
padding-right:15px;
}
The links are contained in a calendar control.
<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site.master.cs" Inherits="SiteMaster" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head runat="server">
<title>Attendance</title>
<link href="~/Styles/Site.css" rel="stylesheet" type="text/css" />
<asp:ContentPlaceHolder ID="HeadContent" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form runat="server">
<div class="page">
<div class="header">
<div class="title">
<h1>
</h1>
<asp:Table ID="Table1" runat="server">
<asp:TableRow>
<asp:TableCell>
<h2>ATTENDANCE
<img src="/images/LogoDrop.jpg" alt="" height="20" width="15" />
EXCEPTION LEAVE TIME</h2>
</asp:TableCell>
</asp:TableRow>
</asp:Table>
</div>
<div class="loginDisplay">
<asp:LoginView ID="HeadLoginView" runat="server" EnableViewState="false">
<AnonymousTemplate>
[ <a href="/Login.aspx" ID="HeadLoginStatus" runat="server">Log In</a> ]
</AnonymousTemplate>
<LoggedInTemplate>
Welcome <span class="bold"><asp:LoginName ID="HeadLoginName" runat="server" /></span>!
[ <asp:LoginStatus ID="HeadLoginStatus" runat="server" LogoutAction="Redirect" LogoutText="Log Out" LogoutPageUrl="/Login.aspx"/> ]
</LoggedInTemplate>
</asp:LoginView>
</div>
<div class="clear hideSkiplink">
<asp:Menu ID="NavigationMenu" runat="server" CssClass="menu"
EnableViewState="false" IncludeStyleBlock="false" Orientation="Horizontal" StaticDisplayLevels="1">
<Items>
<asp:MenuItem NavigateUrl="~/Default.aspx" Text="PayPeriodProcessing" Selected="True" ToolTip="Exception Leave Entry; Approval; Payroll " />
<asp:MenuItem NavigateUrl="~/Request/Time.aspx" Text="Leave Request" Selected="True" ToolTip="Leave Request " />
<asp:MenuItem NavigateUrl="~/About.aspx" Text="About"/>
</Items>
</asp:Menu>
</div>
</div>
<div class="main">
<asp:ContentPlaceHolder ID="MainContent" runat="server"/>
</div>
<div class="clear">
</div>
</div>
<div class="footer">
<asp:Label ID="lblCopyright" runat="server"> </asp:Label>
</div>
</form>
</body>
</html>
You may need to describe your code a bit further, but here are my initial thoughts.
Code front
Code behind
The problem was that you were creating two links. One without any link text, which was set to be red and a second one that had the name and status.
You'd never see the red text since it had no text to display. I've simplified your code a bit and it now works on my end.
This is a fake
DataSet
that I created to test with, but yours should take the place of it.This should correctly render your calendar I believe. One thing I'm not certain on, do you want the weekend dates to always be red?
Update Here is the output with the new code behind.