如何将嵌套循环转换为ListViews

时间:2019-04-15 17:21:27

标签: c# asp.net

我试图显示一个对象数组,每个对象实例都包含数组。

我使用了scriptlet,它可以完美运行,但是我并不是这些的忠实拥护者。 ListView是我尝试实现的,但没有找到第二个循环的解决方案。

一些解释:

  • 第一个循环显示每个用户名
  • 第二个显示用户从“开始日期”到“结束日期”的每个月的薪金

这是用户界面

<%@ Page Title="" Language="C#" MasterPageFile="~/Template.Master" AutoEventWireup="true" CodeBehind="SalaryBetweenDate.aspx.cs" Inherits="UserManagement.Data.SalaryBetweenDate" %>

<asp:Content ID="Content1" ContentPlaceHolderID="BodyContent" runat="server">
    <div class="row form-inline col-lg-6 offset-lg-3">
        <div class="row justify-content-center mt-3">
            <div class="form-group mb-2">
                <label for="beginningDate">Début</label>
                <input runat="server" type="date" class="form-control" id="beginningDate">
            </div>
            <div class="form-group mx-sm-3 mb-2">
                <label for="endingDate">Fin</label>
                <input runat="server" type="date" class="form-control" id="endingDate">
            </div>
            <asp:Button runat="server" OnClick="Unnamed_Click" CssClass="btn btn-primary mb-2" Text="Afficher" />
        </div>
    </div>

    <div class="row">
        <div  class="accordion w-100" id="accordionExample">
            <%
                if (otherAccessibleUsers != null)
                {
                    for (int i = 0; i < otherAccessibleUsers.Length; i++)
                    {
            %>
            <div class="card">
                <div class="card-header" id="<%= otherAccessibleUsers[i].Id %>">
                    <h2 class="mb-0">
                        <button class="btn btn-link collapsed" type="button" data-toggle="collapse" data-target="#collapse<%= otherAccessibleUsers[i].Id %>" aria-expanded="false" aria-controls="collapse<%= otherAccessibleUsers[i].Id %>">
                            <%= otherAccessibleUsers[i].Name %>
                        </button>
                    </h2>
                </div>

                <div id="collapse<%= otherAccessibleUsers[i].Id %>" class="collapse" aria-labelledby="<%= otherAccessibleUsers[i].Id %>" data-parent="#accordionExample">
                    <% 
                        try
                        {
                            display = otherAccessibleUsers[i].GetSalaryBetweenDates(beginningDate.Value, endingDate.Value, allUsers, allSalaryRisings);
                            if (display.Length != 0)
                            {
                        %>
                        <div class="card-body">
                            <table class="table table-bordered">
                                <thead>
                                    <tr>
                                        <th>Mois</th>
                                        <th>Année</th>
                                        <th>Salaire</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <%
                                        for (int j = 0; j < display.Length; j++)
                                        {
                                    %>
                                    <tr>
                                        <td><%= display[j].Month %></td>
                                        <td><%= display[j].Date.Year %></td>
                                        <td><%= display[j].Salary  %></td>
                                    </tr>
                                    <%
                                        }
                                    %>
                                </tbody>
                            </table>
                        </div>
                        <%
                            }
                        %>
                    </div>
                        <%
                            }

                            catch (Exception e)
                            {
                        %>
                        <p><%= e.Message %></p>
                        <%

                            }
                        %>
            </div>


            <%
                    }
                }
            %>
        </div>
    </div>
</asp:Content>

然后是后面的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using UserManagement.Model;

namespace UserManagement.Data
{
    public partial class SalaryBetweenDate : System.Web.UI.Page
    {
        public User[] otherAccessibleUsers = null;
        public User[] allUsers = null;
        public SalaryRising[] allSalaryRisings = null;
        public Model.SalaryBetweenDate[] display = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            PageProtection pageProtection = new PageProtection();

            if (Session["UserId"] != null)
            {

            }
            else
            {
                pageProtection.NotConnected(Session, Server, Request, Response);
            }
        }

        protected void Unnamed_Click(object sender, EventArgs e)
        {
            allUsers = (User[])Session["allUsers"];
            allSalaryRisings = (SalaryRising[])Session["allSalaryRisings"];
            CalculatedElementUser[] allElements = (CalculatedElementUser[])Session["allCalculatedElementUser"];
            User user = (User)Session["user"];
            otherAccessibleUsers = user.GetOtherUsersInformation(allUsers);
        }
    }
}

0 个答案:

没有答案