我有一个页面,其中有一个显示转发器项目的面板,它来自数据库表。这是显示项目的代码:
HTML:
<asp:Panel ID="panelLightRefreshmentsExpanded" runat="server">
<asp:Repeater ID="rptLightRefreshments" runat="server">
<HeaderTemplate>
<table cellpadding="4" cellspacing="4" border="1">
<tr>
<th><asp:Label ID="lblItem" Text="Item" runat="server"></asp:Label></th>
<th><asp:Label ID="lblUnitPruce" Text="Unit Price" runat="server"></asp:Label></th>
<th><asp:Label ID="lblQuantity" Text="Quantity" runat="server"></asp:Label></th>
<th><asp:Label ID="lblUnitPriceTotal" Text="Total" runat="server"></asp:Label></th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><asp:Label ID="lblItem" runat="server" Text='<%# Eval("Description") %>' OnClick="lnkSubmit_Click" CommandArgument='<% Eval("ID").ToString() %>' ViewStateMode="Enabled"></asp:Label></td>
<td><asp:Label ID="lblUnitPrice" runat="server" Text='<%# Eval("Cost") %>' ViewStateMode="Enabled"></asp:Label></td>
<td><asp:TextBox ID="txtQuantity" runat="server" ViewStateMode="Enabled"></asp:TextBox></td>
<asp:RangeValidator ID="RangeValidatorLightRefQuantity" runat="server" ErrorMessage='<%# "Quantity must be a minimum of " + Eval("MinQuantity") %>' ControlToValidate="txtQuantity"
MaximumValue="100" MinimumValue='<%# Eval("MinQuantity") %>' CssClass="validator" Text="*" Type="Integer" Display="Dynamic"></asp:RangeValidator>
<td><asp:TextBox ID="txtUnitPriceTotal" runat="server" ViewStateMode="Enabled" CssClass="grey" ReadOnly="true"></asp:TextBox></td>
<asp:TextBox ID="txtItemID" runat="server" Visible="false" Text='<%# Eval("ItemID") %>'></asp:TextBox>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</asp:Panel>
<asp:CollapsiblePanelExtender ID="panelLightRefreshmentsExpanded_CollapsiblePanelExtender"
runat="server" Enabled="True" TargetControlID="panelLightRefreshmentsExpanded">
</asp:CollapsiblePanelExtender>
代码背后的代码:
SqlCommand lightRefreshmentsCommand = new SqlCommand();
lightRefreshmentsCommand.Connection = itemsConnection;
lightRefreshmentsCommand.CommandText = "select * from table where LocationAvailable LIKE '%" + site + "%' AND ItemType = 'Light refreshments' ";
SqlDataReader lightRefreshmentReader = lightRefreshmentsCommand.ExecuteReader();
rptLightRefreshments.DataSource = lightRefreshmentReader;
rptLightRefreshments.DataBind();
lightRefreshmentReader.Close();
这些项目是餐饮项目,在某些情况下,用户应该只能选择一个选项。例如,如果转发器项目如下,则用于点心:
用户应该只能选择其中一个手指自助餐选项。我打算添加验证,但据我所知,你只能验证一行(即确保一行上的所有字段都有一个条目)。是否可以对转发器项列表中的多行进行验证?我看不出你会怎么做。或者还有另一种方法可以解决这个问题。
感谢任何帮助。
这是我发现的那种帮助文章的一个例子,但是他们只展示了如何使用自定义验证器来验证一行而不是如何验证一行与另一行。
<asp:Repeater ID="rptItem" runat="server" OnItemDataBound="rptItem_ItemDataBound">
<HeaderTemplate>
<table>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:CheckBox ID="chkSelectItem" runat="server" />
</td>
<td>
<asp:TextBox ID="txtToValidate" runat="server"></asp:TextBox>
</td>
<td>
<asp:CustomValidator ID="cValidation" runat="server" ClientValidationFunction="MyClientValidation"
ErrorMessage="Invalid"></asp:CustomValidator>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
protected void rptItem_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
CheckBox chkSelectItem = e.Item.FindControl("chkSelectItem") as CheckBox;
TextBox txtToValidate = e.Item.FindControl("txtToValidate") as TextBox;
CustomValidator cValidation = e.Item.FindControl("cValidation") as CustomValidator;
if (chkSelectItem != null && txtToValidate != null && cValidation != null)
{
ClientScript.RegisterExpandoAttribute(cValidation.ClientID, "chkId", chkSelectItem.ClientID);
ClientScript.RegisterExpandoAttribute(cValidation.ClientID, "txtId", txtToValidate.ClientID);
}
}
}
答案 0 :(得分:1)
要针对多行验证数据,您必须向表单添加自定义验证程序。您需要在转发器控件中添加此项
<asp:CustomValidator ID="cValidation"
runat="server"
OnServerValidate="cValidation_ServerValidate"
ErrorMessage="Invalid">
</asp:CustomValidator>
在验证功能中,重复遍历转发器中的所有行并执行验证逻辑
protected void cValidation_ServerValidate(object source, ServerValidateEventArgs args)
{
bool isValid = true;
//Here you can loop through each item
foreach (RepeaterItem item in rptLightRefreshments.Items)
{
//
if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
{
//Get the controls
Label lblItem = item.FindControl("lblItem") as Label;
TextBox txtQuantity = item.FindControl("txtQuantity") as TextBox;
//Do your validation
}
}
//Finally set the result to args
args.IsValid = isValid;
}