我的asp.net项目中有一个页面,我想要显示员工的出席情况。如果显示P
,则不在时,A
和节假日H
应显示在转发器中。现在在我的网页上我有2个文本框,通过它我可以输入年份和月份,那个月我希望得到参加者。我的数据库表如下。
(1)日历
CalederID Year Month WorkingDays
1 2013 January 1111001111100111110011111001111
2 2013 February 1001111100111110011111001111
等等。这里1代表本月的工作日,0代表星期六和星期日 我正在使用这种模式,因为在我的一个页面上我正在检查(sat和sun)复选框并且未经检查其他人因此我知道这些是假期
(2)出勤表
AttendanceID EmpID PresentDays CalenderID LeaveDate
1 1 Null 1 2013-01-14
2 1 Null 2 2013-02-15
3 1 Null 4 2013-04-11
4 3 Null 6 2013-06-26
(3)EmpInfo表
EmpID EmpName and so on
1 Joe
2 Sandra
现在,当我输入年份和月份时,在我的网页上出现问题我想要显示带有标题的转发器作为日期编号,表示该月的日期。现在,如果月份有30天,则显示30个数字。使用另一个转发器,其必须以如上所述的格式P,A,H显示出席
我的中继器看起来像这样
<table class="table1" >
<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate>
<tr>
<td>Employee ID</td>
</HeaderTemplate>
<ItemTemplate>
<td><asp:Label ID="lbldates" runat="server" Text='<%# Eval("Dates") %>' ></asp:Label></td>
</ItemTemplate>
<FooterTemplate>
<td>TOTAL</td></tr>
<tr>
</FooterTemplate>
</asp:Repeater>
<asp:Repeater id="rptAttendance" runat="server" OnItemDataBound="rptAttendance_ItemDataBound">
<ItemTemplate>
<tr>
<td><asp:Label ID="lblEmpName" runat="server" /></td>
<asp:Repeater ID="rptAttendanceCode" runat="server" OnItemDataBound="rptAttendanceCode_ItemDataBound" >
<ItemTemplate><td><asp:Label ID="lblAttendanceCode" runat="server" /></td></ItemTemplate>
</asp:Repeater>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
,背后的代码是
protected void Page_Load(object sender, EventArgs e)
{
}
public void search(object sender, EventArgs e)
{
string cnnString = "Server=localhost;Port=3307;Database=leavesystem;Uid=root;Pwd=ashish";
MySqlConnection cnx = new MySqlConnection(cnnString);
cnx.Open();
string cmdText1 = "SELECT DAY(LAST_DAY(CAST(CONCAT('" + year.Value + "', '-', MONTH(STR_TO_DATE('" + month.Value + "', '%M')), '-', 1) AS DATE))) ";
MySqlCommand cmd1 = new MySqlCommand(cmdText1, cnx);
MySqlDataAdapter adapter1 = new MySqlDataAdapter();
DataSet ds1 = new DataSet();
adapter1.SelectCommand = cmd1;
adapter1.Fill(ds1);
DataRow dr;
dr = ds1.Tables[0].Rows[0];
string value = dr[0].ToString();
string cmdText2 = "SELECT Dates from temp where Dates <= " + value + " ";
MySqlCommand cmd2 = new MySqlCommand(cmdText2, cnx);
MySqlDataAdapter adapter2 = new MySqlDataAdapter();
DataSet ds2 = new DataSet();
adapter2.SelectCommand = cmd2;
adapter2.Fill(ds2);
DataTable dt = ds2.Tables[0];
Repeater1.DataSource = dt;
Repeater1.DataBind();
string cmdText3 = "SELECT CalenderID, WorkingDays from calender where Year = '" + year.Value + "' and Month = '" + month.Value + "' ";
MySqlCommand cmd3 = new MySqlCommand(cmdText3, cnx);
MySqlDataAdapter adapter3 = new MySqlDataAdapter();
DataSet ds3 = new DataSet();
adapter3.SelectCommand = cmd3;
adapter3.Fill(ds3);
DataTable calender = ds3.Tables[0];
DataRow dr3;
dr3 = ds3.Tables[0].Rows[0];
string CalenderID = dr3[0].ToString();
string cmdText4 = "SELECT EmpID,EmpName from empinfo ";
MySqlCommand cmd4 = new MySqlCommand(cmdText4, cnx);
MySqlDataAdapter adapter4 = new MySqlDataAdapter();
DataSet ds4 = new DataSet();
adapter4.SelectCommand = cmd4;
adapter4.Fill(ds4);
DataTable employees = ds4.Tables[0];
string cmdText5 = "SELECT EmpID,DAY(LeaveDate) AS LeaveDayOfMonth from attendance where CalenderID = '" + CalenderID + "' ";
MySqlCommand cmd5 = new MySqlCommand(cmdText5, cnx);
MySqlDataAdapter adapter5 = new MySqlDataAdapter();
DataSet ds5 = new DataSet();
adapter5.SelectCommand = cmd5;
adapter5.Fill(ds5);
DataTable attendance = ds5.Tables[0];
List<Tuple<string, string[]>> info = new List<Tuple<string, string[]>>();
int calendarID = calender.Rows[0].Field<int>("CalenderID");
string days = calender.Rows[0].Field<string>("WorkingDays");
days = days.Replace("1", "P");
days = days.Replace("0", "H");
string[] daysList = days.Select(d => d.ToString()).ToArray();
int present = 0;
int holidays = 0;
for (int i = 0; i < daysList.Length; ++i)
{
if (daysList[i] == "P")
present++;
if (daysList[i] == "H")
holidays++;
}
int working = (monthdays - holidays);
string total = (present + "/" + working);
Tuple<string, string[],string> employeeAttendance = null;
foreach (DataRow employee in employees.Rows)
{
employeeAttendance = new Tuple<string, string[],string>(employee.Field<string>("EmpName"), daysList,total);
foreach (DataRow absentDay in attendance.AsEnumerable().Where(a => a.Field<int>("EmpID") == employee.Field<int>("EmpID")))
{
var leaveDay = absentDay.Field<Int64>("LeaveDayOfMonth");
int leaveDay1 = Convert.ToInt16(leaveDay);
leaveDay1 = leaveDay1 - 1;
employeeAttendance.Item2[leaveDay1] = "A";
}
info.Add(employeeAttendance);
}
this.rptAttendance.DataSource = info;
this.rptAttendance.DataBind();
}
protected void rptAttendance_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Tuple<string, string[],string> info = (Tuple<string, string[],string>)e.Item.DataItem;
((Label)e.Item.FindControl("lblEmpName")).Text = info.Item1;
((Label)e.Item.FindControl("lbltotal")).Text = info.Item3;
Repeater attendanceCode = (Repeater)e.Item.FindControl("rptAttendanceCode");
attendanceCode.DataSource = info.Item2;
attendanceCode.DataBind();
}
}
protected void rptAttendanceCode_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
((Label)e.Item.FindControl("lblAttendanceCode")).Text = e.Item.DataItem.ToString();
}
}
在后面的代码中,我得到了一个月中的天数,然后将它与一个有31个日期的表格进行比较,从逻辑上我可以看到我的数字,在左下角使用转发器2来显示我的EmpID,现在在下面它们旁边我希望显示出席日期的数字。有人可以告诉我如何做到这一点。我的考勤表中的PresendtDays列是空的,但我不知道如何使用它。请帮助我,我已经尝试了很多个小时,这就是我发布完整代码以便有人帮助我的原因。寻找早期的回应。在此先感谢!!
答案 0 :(得分:1)
由于数据库的非常规设计,我不得不做一些主要的数据操作才能使其工作。话虽如此,这是我提出的解决方案。
您需要执行三个额外的查询,而不是SQL语句"SELECT EmpID from empinfo "
:
从Calendar
表中检索信息:
使用CalendarID从Calendar表中检索信息:
使用第一个查询中的CalendarID从考勤表中检索信息。
完成此操作后,请使用以下 TWO 转发器替换第二个转发器(Repeater2
)。第一个转发器(rptAttendance)列出每个员工,第二个转发器(rptAttendanceCode)列出该员工每月的每一天。 (注意,我们正在连接到转发器的OnItemDataBound
事件,稍后会详细介绍):
<asp:Repeater id="rptAttendance" runat="server" OnItemDataBound="rptAttendance_ItemDataBound">
<ItemTemplate>
<tr>
<td><asp:Label ID="lblEmpName" runat="server" /></td>
<asp:Repeater ID="rptAttendanceCode" runat="server" OnItemDataBound="rptAttendanceCode_ItemDataBound" >
<ItemTemplate><td><asp:Label ID="lblAttendanceCode" runat="server" /></td></ItemTemplate>
</asp:Repeater>
</tr>
</ItemTemplate>
</asp:Repeater>
现在,这就是乐趣的开始!
首先,您需要创建一个数据结构,其中包含员工姓名和每月每天的出勤率。我们将WorkingDays
字段用于我们的基线,并将其附加到每位员工的出勤率(取自出勤表)。
//This list (info) holds the employee's name in the first string, and their attendance in the string array
List<Tuple<string, string[]>> info = new List<Tuple<string, string[]>>();
int calendarID = calendar.Rows[0].Field<int>("CalendarID");
string days = calendar.Rows[0].Field<string>("WorkingDays");
将日期类型代码替换为相应的字母
days = days.Replace("1", "P");
days = days.Replace("0", "H");
将其转换为数组,以便我们可以迭代它
string[] daysList = days.Select(d => d.ToString()).ToArray();
现在,我们将遍历每个员工记录并为每个员工创建数据结构。然后我们将在每个员工的休息日进行迭代,并在他们不工作的日子里更新他们的日程表集合。
foreach (DataRow employee in employees.Rows)
{
employeeAttendance = new Tuple<string, string[]>(employee.Field<string>("EmpName"), daysList);
foreach (DataRow absentDay in attendance.AsEnumerable().Where(a => a.Field<int>("EmpID") == employee.Field<int>("EmpID")))
{
employeeAttendance.Item2[absentDay.Field<int>("LeaveDayOfMonth") - 1] = "A";
}
info.Add(employeeAttendance);
}
以下是每个转发器的ItemDataBound事件处理程序:
protected void rptAttendance_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Tuple<string, string[]> info = (Tuple<string, string[]>)e.Item.DataItem;
((Label)e.Item.FindControl("lblEmpName")).Text = info.Item1;
Repeater attendanceCode = (Repeater)e.Item.FindControl("rptAttendanceCode");
attendanceCode.DataSource = info.Item2;
attendanceCode.DataBind();
}
}
protected void rptAttendanceCode_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
((Label)e.Item.FindControl("lblAttendanceCode")).Text = e.Item.DataItem.ToString();
}
}
以下是完整的代码隐藏:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
List<Tuple<string, string[]>> info = new List<Tuple<string, string[]>>();
int calendarID = calendar.Rows[0].Field<int>("CalendarID");
string days = calendar.Rows[0].Field<string>("WorkingDays");
days = days.Replace("1", "P");
days = days.Replace("0", "H");
string[] daysList = days.Select(d => d.ToString()).ToArray();
Tuple<string, string[]> employeeAttendance = null;
foreach (DataRow employee in employees.Rows)
{
employeeAttendance = new Tuple<string, string[]>(employee.Field<string>("EmpName"), daysList);
foreach (DataRow absentDay in attendance.AsEnumerable().Where(a => a.Field<int>("EmpID") == employee.Field<int>("EmpID")))
{
employeeAttendance.Item2[absentDay.Field<int>("LeaveDayOfMonth") - 1] = "A";
}
info.Add(employeeAttendance);
}
this.rptAttendance.DataSource = info;
this.rptAttendance.DataBind();
}
}
protected void rptAttendance_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Tuple<string, string[]> info = (Tuple<string, string[]>)e.Item.DataItem;
((Label)e.Item.FindControl("lblEmpName")).Text = info.Item1;
Repeater attendanceCode = (Repeater)e.Item.FindControl("rptAttendanceCode");
attendanceCode.DataSource = info.Item2;
attendanceCode.DataBind();
}
}
protected void rptAttendanceCode_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
((Label)e.Item.FindControl("lblAttendanceCode")).Text = e.Item.DataItem.ToString();
}
}