Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 针对重复项进行验证_C#_Asp.net_Validation_Repeater - Fatal编程技术网

C# 针对重复项进行验证

C# 针对重复项进行验证,c#,asp.net,validation,repeater,C#,Asp.net,Validation,Repeater,我有一个页面,其中有一个面板,面板上显示了来自数据库表的转发器项。以下是显示项目的代码: html: 这些项目是餐饮项目,在某些情况下,用户只能选择一个选项。例如,如果中继器项目如下所示,则为小吃: 三明治 汤 手指自助餐1 手指自助餐2 手指自助餐3 用户只能选择一个手指自助餐选项。我本来打算添加验证,但据我所知,您只能验证一行(即确保一行上的所有字段都有一个条目)。是否可以针对中继器项目列表上的多行进行验证?我看不出你会怎么做。或者有没有其他方法来解决这个问题 非常感谢您的帮助 这是我找

我有一个页面,其中有一个面板,面板上显示了来自数据库表的转发器项。以下是显示项目的代码:

html:

这些项目是餐饮项目,在某些情况下,用户只能选择一个选项。例如,如果中继器项目如下所示,则为小吃:

  • 三明治
  • 手指自助餐1
  • 手指自助餐2
  • 手指自助餐3
用户只能选择一个手指自助餐选项。我本来打算添加验证,但据我所知,您只能验证一行(即确保一行上的所有字段都有一个条目)。是否可以针对中继器项目列表上的多行进行验证?我看不出你会怎么做。或者有没有其他方法来解决这个问题

非常感谢您的帮助

这是我找到的帮助文章的一个例子,但是它们只展示了如何使用自定义验证器验证一行,而不是如何对照另一行验证一行

<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>

要针对多行验证数据,必须向表单中添加自定义验证器。您需要在中继器控件外添加此选项

<asp:CustomValidator ID="cValidation" 
              runat="server"      
              OnServerValidate="cValidation_ServerValidate"
              ErrorMessage="Invalid">
</asp:CustomValidator>

您可以添加customvalidator并在那里进行验证。谢谢Kiran。这就是我的想法,但我只能看到如何验证一行中的控件,而不能看到如何对照另一行验证一行。我已经更新了我的问题,以显示我读过的文章类型的示例。如果您知道如何对照另一行验证一行,您将能够提供一个示例。非常感谢这个基兰。我已经在repeater控件之外添加了自定义验证器,并将ServerValidate添加到了代码中,但出现了错误CS1061:“ASP.page1_aspx”不包含“cValidationServerValidate”的定义,并且找不到接受“ASP.page1_aspx”类型的第一个参数的扩展方法“cValidationServerValidate”(您是否缺少using指令或程序集引用?)?
<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);
      }
  }
}
<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;
}