我有两个中继器,一个嵌套在另一个中。
第一个转发器创建12行,每年一个月。它依次计算每个月的天数并将它们添加到数组中。然后将该数组用作第二个转发器的数据源。
如果DayOfWeek是星期六或星期日,则第二个中继器检查每个月的日期并设置td的背景颜色。 (见第二张图片)。
这一切都正常工作,并做我需要的。
接下来我需要做的是查询包含假日日期列表的SQL数据库表。该表包含假日的开始日期,所用的天数和十六进制颜色代码。
我想要它做的是在rptAbsenceDays_ItemDataBound中找到开始日期并使用天数和颜色值显示它类似于第二个链接。此信息存储在隐藏字段中。
如何调用StaffAbsence并让它显示假期
以下是我的网页代码
<div class="tab-pane fade" id="Absence">
<div class="col-md-12">
<table>
<thead>
<tr>
<th>
<asp:Literal ID="litYear" runat="server"></asp:Literal>
</th>
<%--Creates a row of 31 days along the top of the repeater--%>
<% for (int iWeek = 1; iWeek <= 31; iWeek++) { %>
<th style="width:30px; text-align:center; background-color:darkgray;"" >
<%= iWeek %>
</th>
<% } %>
</tr>
</thead>
<asp:Repeater ID="rptAbsenceMonths" runat="server" OnItemDataBound="rptAbsenceMonths_ItemDataBound">
<ItemTemplate>
<tr>
<td>
<asp:Literal ID="litMonth" runat="server" />
</td>
<asp:Repeater ID="rptAbsenceDays" runat="server" OnItemDataBound="rptAbsenceDays_ItemDataBound">
<ItemTemplate>
<td id="tdDay" runat="server">
<asp:Literal ID="litDay" runat="server" />
<asp:HiddenField ID="hidAbsenceStartDate" runat="server" Value='<%#DataBinder.Eval(Container.DataItem,"ScheduleStartDate")%>'/>
<asp:HiddenField ID="hidAbsenceTotalDays" runat="server" Value='<%#DataBinder.Eval(Container.DataItem,"ScheduleDays")%>'/>
<asp:HiddenField ID="hidAbsenceColour" runat="server" Value='<%#DataBinder.Eval(Container.DataItem,"ScheduleColour")%>'/>
</td>
</ItemTemplate>
</asp:Repeater>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
</div>
<div class="clearfix"></div>
这是我的C#代码
protected void AbsenceMonths()
{
litYear.Text = DateTime.Now.Year.ToString();
rptAbsenceMonths.DataSource = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
rptAbsenceMonths.DataBind();
}
protected void rptAbsenceMonths_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Repeater rptAbsenceDays = (Repeater)e.Item.FindControl("rptAbsenceDays");
Literal litMonth = (Literal)e.Item.FindControl("litMonth");
_currentBindingMonth = (int)e.Item.DataItem;
_absenceMonth = new DateTime(DateTime.Now.Year, _currentBindingMonth, 1);
litMonth.Text = _absenceMonth.ToString("MMM");
int numberOfDays = System.DateTime.DaysInMonth(Convert.ToInt16(litYear.Text), _currentBindingMonth);
DateTime[] monthdays = new DateTime[numberOfDays];
for (int i = 0; i < numberOfDays; i++)
{
monthdays[i] = _absenceMonth;
_absenceMonth = _absenceMonth.AddDays(1);
}
rptAbsenceDays.DataSource = monthdays;
rptAbsenceDays.DataBind();
}
}
}
protected void rptAbsenceDays_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
DateTime date = (DateTime)e.Item.DataItem;
Literal litDay = (Literal)e.Item.FindControl("litDay");
HtmlTableCell tdDay = (HtmlTableCell)e.Item.FindControl("tdDay");
if(date.DayOfWeek == DayOfWeek.Saturday || date.DayOfWeek == DayOfWeek.Sunday)
{
tdDay.Style.Add("background-color", "#D0D0D0");
}
tdDay.ID = string.Empty;
}
}
protected void StaffAbsence(int StaffID, int AbsenceYear)
{
SqlCommand cmd = new SqlCommand("sp_GetStaffAbsence", cnn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@EmpCode", StaffID);
cmd.Parameters.AddWithValue("@Year", AbsenceYear);
DataTable dt = new DataTable();
SqlDataAdapter adapt = new SqlDataAdapter(cmd);
adapt.Fill(dt);
if (dt.Rows.Count > 0)
{
//rptAbsenceDays.DataSource = dt;
//rptAbsenceDays.DataBind();
//rptAbsenceDays.Visible = true;
}
}
任何帮助非常感谢
答案 0 :(得分:0)
如果我理解你的问题。您可以在rptAbsenceDays_ItemDataBound
中绑定转发器时执行此操作,但请确保首先获得假期数据。
如果您不想在转发器被绑定时这样做。您可以循环遍历转发器项目,然后执行您需要的任务
foreach (RepeaterItem ri in rptAbsenceDays.Items)
{
if (ri.ItemType == ListItemType.Item || ri.ItemType == ListItemType.AlternatingItem)
{
//do your logic here
}
}