如何显示ASP-直放站项目模板模式? [英] how to show pattern in asp-Repeater item template?

查看:164
本文介绍了如何显示ASP-直放站项目模板模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网页在我的asp.net项目中,我想展示员工的出勤。当present P 应显示,当没有那么 A 和节假日 ^ h 应在中继器显示。现在我的网页上我有2个文本框,通过它我进入年份和月份和该月我想要得到的上座率。我的数据库表如下所示。

I have a page in my asp.net project where I want to show the attendance of the employees. When present P should be shown and when absent then A and on holidays H should be shown in the repeater. Now on my web page I have 2 textboxes through which I enter the year and the month and for that month I want to get the attendance. My database tables are as follows.

(1)日历

CalederID  Year  Month     WorkingDays
1          2013  January   1111001111100111110011111001111
2          2013  February  1001111100111110011111001111

和等。在这里,1重present当月工作日和0是周六和周日的
现在用这种模式,因为在我的网页之一是让复选框检查(周六,周日),并选中为别人,所以我知道这些都是节日

and so on . Here 1 represent the working days in the month and 0's are Saturday's and Sunday's am using this pattern because on one of my page am getting checkboxes checked for (sat and sun) and unchecked for others so I know that these are holidays

(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表

(3) EmpInfo Table

EmpID  EmpName  and so on
1       Joe
2      Sandra              

现在来这个问题我的网页上,当我进入的年份和月份我要显示页眉的日期数字从而重新present该月的日期中继。现在,如果一个月有30天,然后30号所示。另一个中继器使用具有显示格式P中的考勤,A,H如上面说

Now coming to the problem on my web page when I enter the year and month I want to show the repeater with headers as Date Numbers which represent the dates of that month. Now if the month has 30 days then 30 numbers are shown. Another repeater is used which has to show the attendance in the format P,A,H as told above

我的中继器看起来像这样

My Repeaters look like this

<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>

和code的背后,是

and the code behind is

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();
        }
    }

在code后面我得到当月的天数,然后将其与具有31日期和一张桌子比较从逻辑I dsplay我的号码,并在底部用中继器2显示我的EmpID的,现在离开他们下面的日期编号的旁边我想显示出席。有人能告诉我如何做到这一点。在我的出勤表中的presendtDays栏是空的,但我不知道如何使用它。请帮我出我已经从很多时间尝试这一点,这就是为什么我贴我的完整code所以有些人会帮助我。寻找一个早期反应。在此先感谢!

In code behind I get the number of days in the month then compare it with a table which has 31 dates and from that logic I dsplay my numbers and on the bottom left using repeater 2 to show my EmpID's and now beside them below the date numbers I want to show the attendance. Can some one tell me how to do this. The PresendtDays columns in my attendance table is empty but I dont know how to use that. Please help me out I have been trying this from many hours and that is why I posted my complete code so that some one would help me. Looking for an early response. Thanks in advance !!

推荐答案

由于数据库的非常规设计,我不得不做一些重要的数据操作,使这项工作。话虽这么说,这是我提出的解决方案。

Due to the unconventional design of your database, I had to do some major data manipulation to make this work. That being said, here is my proposed solution.

相反,SQL语句选择empinfo的EmpID,你将需要执行另外三个查询:

Instead of the SQL statement "SELECT EmpID from empinfo ", you will need to perform three additional queries:


  1. 从该日历信息表:


  • 选择CalendarID从日历WorkingDays其中year = [年份]和Month = [月]

  • 这将返回一个表,看起来像这样:

从使用CalendarID日历表信息:

Retrieve info from the Calendar table using the CalendarID:


  • 选择的EmpID,EmpName FROM员工

  • 这将返回一个表,看起来像这样:

从考勤表信息,使用CalendarID第一个查询。

Retrieve info from Attendance table, using the CalendarID from the first query.


  • 选择的EmpID,DAY(LeaveDate)AS LeaveDayOfMonth FROM WHERE考勤= CalendarID [日历ID]

  • 这将返回一个表,看起来像这样:

一旦你做到了这一点,更换你的第二个中继器( Repeater2 )具有以下的两个中继器。的,第一个中继器(rptAttendance)列出了每一个员工,第二个中继器(rptAttendance code)列出每月为员工的每一天。 (请注意,我们连接到中继器的 OnItemDataBound 事件,以后会更多):

Once you have done this, replace your second repeater (Repeater2) with the following TWO repeaters. The, first repeater (rptAttendance) lists each employee, the second repeater (rptAttendanceCode) list each day of the month for the employee. (Note, we are connecting to the repeaters's OnItemDataBound event, more on this later):

    <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>

现在,这就是乐趣的开始!

Now, this is where the fun starts!

首先,你需要创建一个保存员工的名字和他/她的月的每一天出勤的数据结构。我们将使用 WorkingDays 字段为我们的基线,并与每位员工的出勤追加它(从出席表获取)。

First, you need to create a data structure that hold the employee name and his/her attendance for each day of the month. We will use the WorkingDays field for our base line and append it with each employee's attendance (taken from the Attendance table).

//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");

与相应的字母替换日式code

Replace the day-type code with the corresponding letter

days = days.Replace("1", "P");
days = days.Replace("0", "H");

转换成一个阵列,以使我们可以遍历其

Convert this into an an array so we can iterate over it

string[] daysList = days.Select(d => d.ToString()).ToArray();

现在,我们将遍历每个员工记录,并为每个员工创建一个数据结构。然后,我们将遍历每个员工的一天,并与他们没有工作的日子里更新自己的daylist集合。

Now we will iterate over each employee record and create a data structure for each employee. Then we will iterate over each employee's day off and update their daylist collection with the days that they were not at work.

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事件处理程序:

Here are each repeater's ItemDataBound event handlers:

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();
    }
}

下面是code-背后的全部:

Here is the code-behind in its entirety:

    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();
        }
    }

这篇关于如何显示ASP-直放站项目模板模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆