我使用嵌套的Employee数据设计了员工数据。我有两个表emp和dept .Table信息 EMP-EMPID(PK),empname,empjob,empsalary,DEPTID 部门-DEPTID(FK),DEPTNAME
我已经在部门明智地显示了数据。父级中继器显示部门表和子中继器显示了emp详细信息。我想要计算我的员工部门总数以及总薪水。就像我最后想要的那样明智计算总员工和总薪水。但是我面临的问题是计算员工和工资部门的明智。这是我的aspx.cs页面..如果你没有得到我什么是我的问题..那么看我附上一个截图
的输出using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
public partial class RepeaterControlNested : System.Web.UI.Page
{
SqlConnection cn = null;
SqlCommand cmd = null;
SqlDataAdapter da = null;
DataSet ds = null;
int TotalEmployeeCount = 0;
decimal TotalSalary = 0;
int GrandTotalEmployeeCount = 0;
decimal GrandTotalSalary = 0;
protected void Page_Load(object sender, EventArgs e)
{
cn = new [![enter image description here][1]][1]SqlConnection(ConfigurationManager.ConnectionStrings["conStr"].ConnectionString);
if (!Page.IsPostBack)
{
BindDept();
}
}
void BindDept()
{
da = new SqlDataAdapter("select * from dept", cn);
ds = new DataSet();
da.Fill(ds, "dept");
deptRepeater.DataSource = ds.Tables["dept"];
deptRepeater.DataBind();
}
protected void deptRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Repeater EmpRepeater = (Repeater)e.Item.FindControl("EmpRepeater");
Label lblDeptId = (Label)e.Item.FindControl("lblDeptId");
Label lblStatus = (Label)e.Item.FindControl("lblStatus");
da = new SqlDataAdapter("select e.empid,e.empname,e.empjob,e.empsalary,d.deptname from emp e, dept d where e.deptid=d.deptid and d.deptid=" + lblDeptId.Text, cn);
ds = new DataSet();
da.Fill(ds, "emp");
if (ds.Tables["emp"].Rows.Count > 0)
{
EmpRepeater.DataSource = ds.Tables["emp"];
EmpRepeater.DataBind();
}
else
{
lblStatus.Text = "no data available";
}
}
if (e.Item.ItemType == ListItemType.Footer)
{
Label lblGrandTotalEmp = (Label)e.Item.FindControl("lblGrandTotalEmp");
lblGrandTotalEmp.Text = GrandTotalEmployeeCount.ToString();
Label lblGrandTotalSalary = (Label)e.Item.FindControl("lblGrandTotalSalary");
lblGrandTotalSalary.Text = GrandTotalSalary.ToString("c");
}
TotalSalary = 0;
TotalEmployeeCount = 0;
}
protected void EmpRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.Item)
{
Label lblEmpSalary = (Label)e.Item.FindControl("lblEmpSalary");
Label lblGrandTotalSalary = (Label)e.Item.FindControl("lblGrandTotalSalary");
if (lblEmpSalary != null)
{
TotalSalary += decimal.Parse(lblEmpSalary.Text);
TotalEmployeeCount += 1;
GrandTotalEmployeeCount += 1;
GrandTotalSalary = GrandTotalSalary + decimal.Parse(lblEmpSalary.Text);
}
}
if (e.Item.ItemType == ListItemType.Footer)
{
Label lblTotalEmp = (Label)e.Item.FindControl("lblTotalEmp");
lblTotalEmp.Text = TotalEmployeeCount.ToString();
Label lblTotalSalary = (Label)e.Item.FindControl("lblTotalSalary");
lblTotalSalary.Text = TotalSalary.ToString("c");
}
}
}
这是我的设计页面 -
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="RepeaterControlNested.aspx.cs" Inherits="RepeaterControlNested" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table border="1" style="background-color:blue;color:white" width="100%">
<tr>
<th align="center">Employee Data</th>
</tr>
</table>
</div>
<div>
<asp:Repeater ID="deptRepeater" runat="server" OnItemDataBound="deptRepeater_ItemDataBound">
<ItemTemplate>
<table width="100%">
<tr style="background-color:#2bb9d9;color:white">
<td align="left">
Dept Name : <%#Eval("deptname") %>
<asp:Label ID="lblDeptId" runat="server" Style="display:none" Text='<%#Eval("deptid") %>'></asp:Label>
</td>
</tr>
</table>
<asp:Repeater ID="EmpRepeater" runat="server" OnItemDataBound="EmpRepeater_ItemDataBound">
<HeaderTemplate>
<table width="100%">
<tr style="background-color:yellow;color:green">
<td width="20%">Emp Id</td>
<td width="20%">Emp Name</td>
<td width="20%">Emp Job</td>
<td width="20%">Emp Salary</td>
<td width="20%">Dept Name</td>
</tr>
</table>
</HeaderTemplate>
<ItemTemplate>
<table width="100%">
<tr>
<td width="20%">
<%#Eval("empid") %>
</td>
<td width="20%">
<%#Eval("empname") %>
</td>
<td width="20%">
<%#Eval("empjob") %>
</td>
<td width="20%">
<asp:Label ID="lblEmpSalary" runat="server" Text='<%#Eval("empsalary") %>'></asp:Label>
</td>
<td width="20%">
<%#Eval("deptname") %>
</td>
</tr>
</table>
</ItemTemplate>
<FooterTemplate>
<table width="100%">
<tr style="background-color:yellow;color:green">
<th width="40%" colspan="2" align="left">
Total no of employee:
<asp:Label ID="lblTotalEmp" runat="server"></asp:Label>
</th>
<th width="20%" align="right">
Total Salary:
</th>
<th width="40%" colspan="2" align="left">
<asp:Label ID="lblTotalSalary" runat="server"></asp:Label>
</th>
</tr>
</table>
</FooterTemplate>
</asp:Repeater>
<asp:Label ID="lblStatus" runat="server" BackColor="Red" ForeColor="White"></asp:Label>
</ItemTemplate>
<FooterTemplate>
<table width="100%">
<tr style="background-color:yellow;color:green">
<th width="40%" colspan="2" align="left">
GrandTotal of employee:
<asp:Label ID="lblGrandTotalEmp" runat="server"></asp:Label>
</th>
<th width="20%" align="right">
Grand Total Salary:
</th>
<th width="40%" colspan="2" align="left">
<asp:Label ID="lblGrandTotalSalary" runat="server"></asp:Label>
</th>
</tr>
</table>
</FooterTemplate>
</asp:Repeater>
</div>
</form>
enter code here
答案 0 :(得分:0)
我在做的唯一的错误是在这个陈述中
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.Item)
代替 我应该写
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
现在正在运作