【问题标题】:nested repeater control and item count of child repeater嵌套中继器控件和子中继器的项目计数
【发布时间】:2016-01-07 21:52:05
【问题描述】:

我使用嵌套的Employee数据设计了一个员工数据。我有两个表emp和dept。表信息 emp-empid(PK),empname,empjob,empsalary,deptid dept-deptid(FK),部门名称

我已经在部门中显示了数据。父中继器显示部门表,子中继器显示 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

【问题讨论】:

  • 您不应该对外部中继器执行一次 SQL 查询,然后对每一行执行另一个查询。执行一个查询以获取您需要的所有数据(通过 JOIN),然后一次将所有数据绑定到您的转发器。一旦你这样做了,你当前的问题就会变得没有意义,因为当你在外部转发器中设置该行的其他字段时,你已经有了内部项目的计数。
  • 我认为您只是在员工的中继器中存在错误,并且您缺少 AlternateItem。
  • @hivo 我没有在这里附加任何交替项.. 看到我已经更新了我的问题,还添加了 aspx 页面
  • @Servy 好吧,我认为这不是问题..每次触发 itembound 事件时,数据集都会变空..所以没有必要使用 join 进行单个查询
  • @chikun 没有意义的唯一原因是,如果您的结果集中永远不会超过一行。如果您有不止一行,那么无论是在您的网络服务器上还是在您的数据库中,您所做的工作都比您应该做的多方式,而同时让它成为方式你更难做你想做的事。根本没有充分的理由这样做。您几乎不应该使用项目数据绑定事件;它通常对于任何给定的工作来说都是错误的工具。

标签: c# asp.net


【解决方案1】:

我做的唯一错误是在这句话中

     if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==  ListItemType.Item)

改为 我应该写

 if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)

现在可以正常使用了

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多