问题描述
我有一个可以创造奇迹的中继器。现在我需要验证数据库中是否存在记录,如果存在,则不显示下拉列表,而是显示其他文本框。如果只有 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 中调用的方法中。
谢谢!
解决方法
显然,出于某种原因,i++ 在 foreach 之外。由于疲劳导致的愚蠢错误。