首页 > 解决方案 > ASP 中继器未填充所有项目

问题描述

我有一个可以创造奇迹的中继器。现在我需要验证数据库中是否存在记录,如果存在,则不要显示下拉列表,而是显示其他文本框。如果只有 1 条记录,它可以工作,但如果它看到多于 1 条,它不会。这是代码:

主要ASPX:

<asp:Repeater ID="repStudents" runat="server">
                        <ItemTemplate>
                                <asp:DropDownList ID="reasonStd" runat="server">
                                      <asp:ListItem Enabled="true" Text="Select Reason" Value="-1"></asp:ListItem>
                                         OPTIONS...
                                 </asp:DropDownList>
                                            <asp:TextBox ID="reasonLabel" runat="server" Visible="false"></asp:TextBox>
                                            <span>
                                                <asp:TextBox ID="dateForStudents" type="date" runat="server" CssClass="form-control"></asp:TextBox>
                                                <asp:TextBox ID="dateLabel" runat="server" Visible="false"></asp:TextBox>
                                            </span>

这是后端:

List<ASF.FinanceCenter.Entity.Student> dt = dbStudents.GetAllByFamilyId(int.Parse(lFamilyId.Text)); <--- This fills the same number as repeater
            int i = 0;
            DropDownList students;
            TextBox txtStudents, lastDay, reason;
            foreach (ASF.FinanceCenter.Entity.Student studentinos in dt)
            {
                students = repStudents.Items[i].FindControl("reasonStd") as DropDownList;
                txtStudents = repStudents.Items[i].FindControl("dateForStudents") as TextBox;               
                lastDay = repStudents.Items[i].FindControl("dateLabel") as TextBox;                            
                reason = repStudents.Items[i].FindControl("reasonLabel") as TextBox;              
                reason.Visible = true;
                students.Visible = false;
                txtStudents.Visible = false;
                lastDay.Visible = true;
                lastDay.Text = GetDataLastDay(studentinos.Id.ToString());
                reason.Text = GetDataReason(studentinos.Id.ToString());
            }
                i++;

我得到的有点奇怪,因为它没有填写所有选项,比如中继器中有 2 个项目,它只适用于第一个项目,但第二个项目看起来什么也没做,尽管如果我调试,值填写框是正确的,它从数据库中正确获取它们。后端的最后一个,它在我在 Page_Load 中调用的方法中。

谢谢!

标签: asp.netasprepeater

解决方案


显然,出于某种原因, i++ 在 foreach 之外。由于疲劳导致的愚蠢错误。


推荐阅读